Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita734e68

Browse files
committed
The Leetcode
The Leetcode
0 parents  commita734e68

File tree

66 files changed

+4675
-0
lines changed

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+
packageAlgorithms;
2+
3+
publicclassAddBinary {
4+
publicstaticvoidmain(String[]args) {
5+
AddBinaryab =newAddBinary();
6+
System.out.println(ab.addBinary("100","110010"));
7+
8+
return;
9+
10+
}
11+
12+
publicStringaddBinary(Stringa,Stringb) {
13+
intsLen =0;
14+
intlLen =0;
15+
intcarries =0;
16+
17+
Stringl =null;
18+
Strings =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+
StringBuilderrst =newStringBuilder();
32+
33+
inti =0;
34+
for (i =sLen -1;i >=0;i--) {
35+
intsum = (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+
returnrst.toString();
55+
}
56+
}
57+
58+
if (carries ==1) {
59+
rst.insert(0,'1');
60+
}
61+
62+
returnrst.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+
packageAlgorithms;
2+
3+
4+
5+
abstractpublicclassAg {
6+
7+
}

‎Anagrams.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packageAlgorithms;
2+
importjava.util.ArrayList;
3+
importjava.util.HashMap;
4+
5+
6+
publicclassAnagrams {
7+
publicstaticvoidmain(String[]args) {
8+
Anagramsan =newAnagrams();
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+
privateintgetHash(int[]count) {
15+
inthash =0;
16+
inta =378551;
17+
intb =63689;
18+
19+
// int a = 777877;
20+
// int b = 123451;
21+
for (intnum :count) {
22+
hash =hash *a +num;
23+
a =a *b;
24+
}
25+
returnhash;
26+
}
27+
28+
publicArrayList<String>anagrams(String[]strs) {
29+
ArrayList<String>result =newArrayList<String>();
30+
HashMap<Integer,ArrayList<String>>map =newHashMap<Integer,ArrayList<String>>();
31+
32+
for (Stringstr :strs) {
33+
int[]count =newint[26];
34+
for (inti =0;i <str.length();i++) {
35+
count[str.charAt(i) -'a']++;
36+
}
37+
38+
inthash =getHash(count);
39+
if (!map.containsKey(hash)) {
40+
map.put(hash,newArrayList<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+
returnresult;
53+
}
54+
55+
}

‎BinarySearch.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packageAlgorithms;
2+
3+
publicclassBinarySearch {
4+
publicstaticvoidmain(String[]strs) {
5+
BinarySearchbs =newBinarySearch();
6+
int[]num =newint[]{0,1,2,3,4,5};
7+
inttarget =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+
publicintbinarySearch(intnum[],inttarget) {
14+
if (num ==null ||num.length ==0) {
15+
return -1;
16+
}
17+
18+
intleft =0;
19+
intright =num.length -1;
20+
21+
Stringtest ="test";
22+
Stringt2 =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+
intmid =left + (right -left)/2;
38+
if (num[mid] >target) {
39+
right =mid;
40+
}elseif (num[mid] <target) {
41+
left =mid;
42+
}else {
43+
returnmid;
44+
}
45+
}
46+
47+
if (num[left] ==target) {
48+
returnleft;
49+
}elseif (num[right] ==target) {
50+
returnright;
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+
packageAlgorithms;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
6+
publicclassCombination2 {
7+
publicstaticvoidmain(String[]args) {
8+
ArrayList<Character>test =newArrayList<Character>();
9+
10+
test.add('c');
11+
test.add('b');
12+
13+
System.out.printf("Result: %s",test.toString());
14+
15+
Combination2cb =newCombination2();
16+
int[]num = {2,2,2};
17+
cb.combinationSum2(num,4);
18+
}
19+
20+
publicArrayList<ArrayList<Integer>>combinationSum2(int[]num,inttarget) {
21+
22+
// first we should sort the array.
23+
Arrays.sort(num);
24+
25+
ArrayList<ArrayList<Integer>>rst =newArrayList<ArrayList<Integer>>();
26+
ArrayList<Integer>path =newArrayList<Integer>();
27+
28+
cmbHelp(num,0,path,rst,target);
29+
30+
returnrst;
31+
}
32+
33+
publicvoidcmbHelp(int[]num,intindex,ArrayList<Integer>path,ArrayList<ArrayList<Integer>>rst,inttarget) {
34+
if (target ==0) {
35+
// add the current set into the result.
36+
rst.add(newArrayList<Integer>(path));
37+
}
38+
39+
for (inti =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+
packageAlgorithms;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
6+
publicclassCombinationSum{
7+
publicArrayList<ArrayList<Integer>>combine(intn,intk) {
8+
ArrayList<ArrayList<Integer>>rst =newArrayList<ArrayList<Integer>>();
9+
10+
help(n,0,newArrayList<Integer>(),rst,k);
11+
12+
returnrst;
13+
}
14+
15+
publicvoidhelp(intn,intindex,ArrayList<Integer>path,ArrayList<ArrayList<Integer>>rst,intk) {
16+
if (path.size() ==k) {
17+
rst.add(newArrayList<Integer>(path));
18+
return;
19+
}
20+
21+
for (inti =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+
publicstaticvoidmain(String[]args) {
32+
CombinationSumcs =newCombinationSum();
33+
34+
int[]num = {1,2};
35+
cs.combine(1,1);
36+
37+
38+
39+
40+
41+
}
42+
43+
publicArrayList<ArrayList<Integer>>combinationSum(int[]candidates,inttarget) {
44+
ArrayList<ArrayList<Integer>>rst =newArrayList<ArrayList<Integer>> ();
45+
Arrays.sort(candidates);
46+
47+
help(candidates,0,newArrayList<Integer>(),rst,target);
48+
49+
System.out.printf(rst.toString());
50+
51+
returnrst;
52+
}
53+
54+
publicvoidhelp(int[]cand,intindex,ArrayList<Integer>path,ArrayList<ArrayList<Integer>>rst,inttarget) {
55+
if (target ==0) {
56+
// add the current set into the result.
57+
rst.add(newArrayList<Integer>(path));
58+
return;
59+
}
60+
61+
intpre = -1;
62+
for (inti =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+
packageAlgorithms;
2+
3+
publicclassDivideTwoIntegers {
4+
publicstaticvoidmain(String[]args) {
5+
System.out.println(divide(-2147483648,1));
6+
}
7+
8+
publicstaticintdivide(intdividend,intdivisor) {
9+
longa =Math.abs((long)dividend);
10+
longb =Math.abs((long)divisor);
11+
intret =0;
12+
13+
while (a >=b) {
14+
longbTmp =b;
15+
intcnt =1;
16+
for (inti =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)

[8]ページ先頭

©2009-2025 Movatter.jp