Skip to content

Commit ee7bc56

Browse files
committed
190901
1 parent 6671381 commit ee7bc56

8 files changed

+189
-0
lines changed

136_Single Number.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int res = 0;
4+
for (Integer e : nums) res ^= e;
5+
return res;
6+
}
7+
}

190_Reverse Bits.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// 1.0
2+
public class Solution {
3+
// you need treat n as an unsigned value
4+
public int reverseBits(int n) {
5+
int res = 0;
6+
for (int i = 0; i < 32; i++) {
7+
res <<= 1;
8+
res |= (n & 1);
9+
n >>>= 1;
10+
}
11+
return res;
12+
}
13+
}
14+
15+
// 1.1 将 int 拆成 4 个 byte,然后缓存 byte 对应的比特位翻转,最后再拼接
16+
private static Map<Byte, Integer> cache = new HashMap<>();
17+
18+
public int reverseBits(int n) {
19+
int ret = 0;
20+
for (int i = 0; i < 4; i++) {
21+
ret <<= 8;
22+
ret |= reverseByte((byte) (n & 0b11111111));
23+
n >>= 8;
24+
}
25+
return ret;
26+
}
27+
28+
private int reverseByte(byte b) {
29+
if (cache.containsKey(b)) return cache.get(b);
30+
int ret = 0;
31+
byte t = b;
32+
for (int i = 0; i < 8; i++) {
33+
ret <<= 1;
34+
ret |= t & 1;
35+
t >>= 1;
36+
}
37+
cache.put(b, ret);
38+
return ret;
39+
}
40+
41+
// 1.2
42+
public class Solution {
43+
// you need treat n as an unsigned value
44+
public int reverseBits(int n) {
45+
int ret=n;
46+
ret = ret >>> 16 | ret<<16;
47+
ret = (ret & 0xff00ff00) >>> 8 | (ret & 0x00ff00ff) << 8;
48+
ret = (ret & 0xf0f0f0f0) >>> 4 | (ret & 0x0f0f0f0f) << 4;
49+
ret = (ret & 0xcccccccc) >>> 2 | (ret & 0x33333333) << 2;
50+
ret = (ret & 0xaaaaaaaa) >>> 1 | (ret & 0x55555555) << 1;
51+
return ret;
52+
}
53+
}

231_Power of Two.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public boolean isPowerOfTwo(int n) {
3+
return n > 0 && (n == (n & -n));
4+
return n > 0 && (n & (n - 1)) == 0;
5+
}
6+
}

260_Single Number III.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[] singleNumber(int[] nums) {
3+
int diff = 0;
4+
for (int num : nums) diff ^= num;
5+
diff &= -diff; // diff &= -diff 得到出 diff 最右侧不为 0 的位
6+
// int lastBit = (aXorb & (aXorb - 1)) ^ aXorb; // the last bit that a diffs b
7+
int[] res = new int[2];
8+
for (int num : nums) {
9+
if ((num & diff) == 0) res[0] ^= num;
10+
else res[1] ^= num;
11+
}
12+
return res;
13+
}
14+
}
15+
0101 d
16+
1010 1's comp
17+
1011 2's comp
18+
0001 last digit
19+
20+
0101 d
21+
0100 d-1
22+
0100 d & d-1
23+
0001 last digit

342_Power of Four.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean isPowerOfFour(int num) {
3+
return Integer.toString(num, 4).matches("10*");
4+
5+
return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
6+
// 4^n - 1 = (2^n + 1) * (2^n - 1), among (2^n-1), (2^n), (2^n+1), one of them must be a multiple of 3, and (2^n) cannot be the one, so (4^n - 1) % 3 == 0
7+
// only prove ((4-1)^n - 1)%3 ==0 is not enough, you also need to prove that (2^n-1)%3 != 0 when n is odd.
8+
// 2^n = (3-1)^n = C(n,0)3^n(-1)^0+....+(-1)^n.
9+
// 1.When 2^n is 4^n, which means n is even, in this case, (-1)^n==1 and (2^n-1)%3==0
10+
// 2.When 2&n is not 4^n, which means n is odd, in this case, (-1)^n=-1 and (2^n-1)%3==1;
11+
12+
return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num);
13+
}
14+
}

461_Hamming Distance.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int hammingDistance(int x, int y) {
3+
int temp = x ^ y, res = 0;
4+
while (temp > 0) {
5+
res += temp & 1;
6+
temp >>= 1;
7+
}
8+
return res;
9+
}
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 1.0 DP, O(n)/O(n)
2+
// Soldi=Max(Soldi−1,Holdi−1+Pricesi−fee); Holdi=Max(Holdi,Soldi−1−Pricesi)
3+
class Solution {
4+
public int maxProfit(int[] prices, int fee) {
5+
int n = prices.length;
6+
// if (n < 2) return 0;
7+
int[] sold = new int[n];
8+
int[] hold = new int[n];
9+
hold[0] = -prices[0];
10+
for (int i = 1; i < n; i++){
11+
sold[i] = Math.max(sold[i-1], hold[i-1] + prices[i] - fee);
12+
hold[i] = Math.max(hold[i-1], sold[i-1] - prices[i]);
13+
}
14+
return sold[n-1];
15+
}
16+
}
17+
18+
// 1.1 DP, O(n)/O(1)
19+
public int maxProfit(int[] prices, int fee) {
20+
int n = prices.length;
21+
int sold = 0;
22+
int hold = -prices[0];
23+
for (int i = 1; i < n; i++){
24+
int soldpre = sold;
25+
sold = Math.max(soldpre, hold + prices[i] - fee);
26+
hold = Math.max(hold, soldpre - prices[i]);
27+
}
28+
return sold;
29+
}
30+
31+
// 2.0 Greedy, O(n)/O(1)
32+
class Solution {
33+
public int maxProfit(int[] prices, int fee) {
34+
int profit=0, curProfit=0;
35+
int minP=prices[0], maxP=prices[0];
36+
for(int i=1;i<prices.length;i++){
37+
minP = Math.min(minP,prices[i]);
38+
maxP = Math.max(maxP,prices[i]);
39+
curProfit = Math.max(curProfit,prices[i]-minP-fee);
40+
if((maxP-prices[i])>=fee){//can just sell the stock at maxP day.
41+
profit += curProfit;
42+
curProfit = 0;
43+
minP = prices[i];
44+
maxP = prices[i];
45+
}
46+
}
47+
return profit + curProfit;
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 1.0 dfs
2+
class Solution {
3+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
4+
List<List<Integer>> res = new LinkedList<List<Integer>>();
5+
if (graph == null || graph.length == 0) return res;
6+
Stack<Integer> path = new Stack<Integer>();
7+
path.push(0);
8+
dfs(graph, 0, res, path);
9+
return res;
10+
}
11+
12+
void dfs(int[][] graph, int cur, List<List<Integer>> res, Stack<Integer> path) {
13+
int n = graph.length;
14+
if (cur == n - 1) {
15+
res.add(new ArrayList<Integer>(path));
16+
} else {
17+
for (int i = 0; i < graph[cur].length; i++) {
18+
path.push(graph[cur][i]);
19+
dfs(graph, graph[cur][i], res, path);
20+
path.pop();
21+
}
22+
}
23+
}
24+
}
25+
26+
//2.0 bfs
27+
//http://www.ciaoshen.com/algorithm/leetcode/2019/03/20/leetcode-all-paths-from-source-to-target.html

0 commit comments

Comments
 (0)