Skip to content

Commit a734e68

Browse files
committed
The Leetcode
The Leetcode
0 parents  commit a734e68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4675
-0
lines changed

AddBinary.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Algorithms;
2+
3+
public class AddBinary {
4+
public static void main(String[] args) {
5+
AddBinary ab = new AddBinary();
6+
System.out.println(ab.addBinary("100", "110010"));
7+
8+
return;
9+
10+
}
11+
12+
public String addBinary(String a, String b) {
13+
int sLen = 0;
14+
int lLen = 0;
15+
int carries = 0;
16+
17+
String l = null;
18+
String s = null;
19+
if (a.length() > b.length()) {
20+
sLen = b.length();
21+
lLen = a.length();
22+
l = a;
23+
s = b;
24+
} else {
25+
sLen = a.length();
26+
lLen = b.length();
27+
l = b;
28+
s = a;
29+
}
30+
31+
StringBuilder rst = new StringBuilder();
32+
33+
int i = 0;
34+
for (i = sLen - 1; i >= 0; i--) {
35+
int sum = (int)(s.charAt(i) - '0') + (int)(l.charAt(lLen - sLen + i) - '0') + carries;
36+
if (sum >= 2) {
37+
carries = 1;
38+
} else {
39+
carries = 0;
40+
}
41+
rst.insert(0, String.valueOf(sum%2));
42+
}
43+
44+
for (i = lLen - sLen - 1; i >= 0; i--) {
45+
if (carries == 1) {
46+
if (l.charAt(i) == '0') {
47+
carries = 0;
48+
rst.insert(0,'1');
49+
} else {
50+
rst.insert(0,'0');
51+
}
52+
} else {
53+
rst.insert(0, l, 0, i + 1);
54+
return rst.toString();
55+
}
56+
}
57+
58+
if (carries == 1) {
59+
rst.insert(0,'1');
60+
}
61+
62+
return rst.toString();
63+
}
64+
65+
}

Ag.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Algorithms;
2+
3+
4+
5+
abstract public class Ag {
6+
7+
}

Anagrams.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package Algorithms;
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
5+
6+
public class Anagrams {
7+
public static void main(String[] args) {
8+
Anagrams an = new Anagrams();
9+
String[] strs = {"car","dog","god","rac","mon","bac","acr","mea","cab", "abd", "adb"};
10+
//String[] strs = {"abc", "bac"};
11+
System.out.println(an.anagrams(strs).toString());
12+
}
13+
14+
private int getHash(int[] count) {
15+
int hash = 0;
16+
int a = 378551;
17+
int b = 63689;
18+
19+
// int a = 777877;
20+
// int b = 123451;
21+
for (int num : count) {
22+
hash = hash * a + num;
23+
a = a * b;
24+
}
25+
return hash;
26+
}
27+
28+
public ArrayList<String> anagrams(String[] strs) {
29+
ArrayList<String> result = new ArrayList<String>();
30+
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
31+
32+
for (String str : strs) {
33+
int[] count = new int[26];
34+
for (int i = 0; i < str.length(); i++) {
35+
count[str.charAt(i) - 'a']++;
36+
}
37+
38+
int hash = getHash(count);
39+
if (!map.containsKey(hash)) {
40+
map.put(hash, new ArrayList<String>());
41+
}
42+
43+
map.get(hash).add(str);
44+
}
45+
46+
for (ArrayList<String> tmp : map.values()) {
47+
if (tmp.size() > 1) {
48+
result.addAll(tmp);
49+
}
50+
}
51+
52+
return result;
53+
}
54+
55+
}

BinarySearch.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package Algorithms;
2+
3+
public class BinarySearch {
4+
public static void main(String[] strs) {
5+
BinarySearch bs = new BinarySearch();
6+
int[] num = new int[]{0,1,2,3,4,5};
7+
int target = 9;
8+
System.out.print(bs.binarySearch(num, target));
9+
//int[] num2 = new int[0];
10+
//System.out.print(bs.binarySearch(num2, target));
11+
}
12+
13+
public int binarySearch(int num[], int target) {
14+
if (num == null || num.length == 0) {
15+
return -1;
16+
}
17+
18+
int left = 0;
19+
int right = num.length - 1;
20+
21+
String test = "test";
22+
String t2 = test.substring(0,0);
23+
System.out.printf(test.substring(0,0));
24+
25+
// while (left <= right) {
26+
// int mid = left + (right - left)/2;
27+
// if (num[mid] > target) {
28+
// right = mid - 1;
29+
// } else if (num[mid] < target) {
30+
// left = mid + 1;
31+
// } else {
32+
// return mid;
33+
// }
34+
// }
35+
// [1, 2]
36+
while (left + 1 < right) {
37+
int mid = left + (right - left)/2;
38+
if (num[mid] > target) {
39+
right = mid;
40+
} else if (num[mid] < target) {
41+
left = mid;
42+
} else {
43+
return mid;
44+
}
45+
}
46+
47+
if (num[left] == target) {
48+
return left;
49+
} else if (num[right] == target) {
50+
return right;
51+
}
52+
53+
return -1;
54+
}
55+
}

Combination2.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Algorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class Combination2 {
7+
public static void main(String[] args) {
8+
ArrayList<Character> test = new ArrayList<Character>();
9+
10+
test.add('c');
11+
test.add('b');
12+
13+
System.out.printf("Result: %s", test.toString());
14+
15+
Combination2 cb = new Combination2();
16+
int[] num = {2,2,2};
17+
cb.combinationSum2(num, 4);
18+
}
19+
20+
public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
21+
22+
// first we should sort the array.
23+
Arrays.sort(num);
24+
25+
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
26+
ArrayList<Integer> path = new ArrayList<Integer>();
27+
28+
cmbHelp(num, 0, path, rst, target);
29+
30+
return rst;
31+
}
32+
33+
public void cmbHelp(int[] num, int index, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> rst, int target) {
34+
if (target == 0) {
35+
// add the current set into the result.
36+
rst.add(new ArrayList<Integer>(path));
37+
}
38+
39+
for (int i = index; i < num.length; i++) {
40+
if (num[i] > target) {
41+
// don't need to add new element;
42+
return;
43+
}
44+
45+
if (i >= 1 &&
46+
num[i] == num[i - 1] &&
47+
(path.size() == 0 || path.get(path.size() - 1) != num[i])) {
48+
continue;
49+
}
50+
51+
path.add(num[i]);
52+
cmbHelp(num, i + 1, path, rst, target - num[i]);
53+
path.remove(path.size() - 1);
54+
}
55+
56+
return;
57+
}
58+
59+
}

CombinationSum.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package Algorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class CombinationSum{
7+
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
8+
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
9+
10+
help(n, 0, new ArrayList<Integer>(), rst, k);
11+
12+
return rst;
13+
}
14+
15+
public void help(int n, int index, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> rst, int k) {
16+
if (path.size() == k) {
17+
rst.add(new ArrayList<Integer>(path));
18+
return;
19+
}
20+
21+
for (int i = index; i < n; i++) {
22+
path.add(i + 1);
23+
help(n, i + 1, path, rst, k);
24+
path.remove(path.size() - 1);
25+
}
26+
27+
return;
28+
}
29+
30+
31+
public static void main(String[] args) {
32+
CombinationSum cs = new CombinationSum();
33+
34+
int[] num = {1,2};
35+
cs.combine(1, 1);
36+
37+
38+
39+
40+
41+
}
42+
43+
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
44+
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>> ();
45+
Arrays.sort(candidates);
46+
47+
help(candidates, 0, new ArrayList<Integer>(), rst, target);
48+
49+
System.out.printf(rst.toString());
50+
51+
return rst;
52+
}
53+
54+
public void help(int[] cand, int index, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> rst, int target) {
55+
if (target == 0) {
56+
// add the current set into the result.
57+
rst.add(new ArrayList<Integer>(path));
58+
return;
59+
}
60+
61+
int pre = -1;
62+
for (int i = index; i < cand.length; i++) {
63+
if (cand[i] > target) {
64+
// because the sequence is ascending, so we don't need to go on.
65+
break;
66+
}
67+
68+
if (cand[i] == pre)
69+
70+
path.add(cand[i]);
71+
help(cand, index, path, rst, target - cand[i]);
72+
path.remove(path.size() - 1);
73+
}
74+
75+
return;
76+
}
77+
78+
}

DivideTwoIntegers.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Algorithms;
2+
3+
public class DivideTwoIntegers {
4+
public static void main(String[] args) {
5+
System.out.println(divide(-2147483648, 1));
6+
}
7+
8+
public static int divide(int dividend, int divisor) {
9+
long a = Math.abs((long)dividend);
10+
long b = Math.abs((long)divisor);
11+
int ret = 0;
12+
13+
while (a >= b) {
14+
long bTmp = b;
15+
int cnt = 1;
16+
for (int i = 1; a >= bTmp; cnt <<= 1, bTmp <<= 1) {
17+
a -= bTmp;
18+
ret += cnt;
19+
}
20+
}
21+
22+
return ((dividend > 0) ^ (divisor > 0)) ? -ret: ret;
23+
}
24+
}

0 commit comments

Comments
 (0)