-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
203 changed files
with
4,974 additions
and
4,988 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +0,0 @@ | ||
public class Solution { | ||
public int[] twoSum(int[] numbers, int target) { | ||
int[] res = new int[2]; | ||
Map map = new HashMap(); | ||
for (int i = 0; i < numbers.length; i++) { | ||
map.put(numbers[i], i); | ||
} | ||
for (int i = 0; i < numbers.length; i++) { | ||
int gap = target - numbers[i]; | ||
if (map.get(gap) != null && (int) map.get(gap) != i) { | ||
res[0] = i + 1; | ||
res[1] = (int) map.get(gap) + 1; | ||
break; | ||
} | ||
} | ||
return res; | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
public class Solution { | ||
public int reverse(int x) { | ||
String str=String.valueOf(x); | ||
String r=""; | ||
int re=0; | ||
if (x<0) { | ||
for (int i = str.length()-1; i >0 ; i--) { | ||
r+=str.substring(i,i+1); | ||
} | ||
r="-"+r; | ||
}else { | ||
for (int i = str.length()-1; i >-1 ; i--) { | ||
r+=str.substring(i,i+1); | ||
} | ||
} | ||
try { | ||
re=Integer.parseInt(r); | ||
} catch (Exception e) { | ||
} | ||
return re; | ||
String r=""; | ||
int re=0; | ||
if (x<0) { | ||
for (int i = str.length()-1; i >0 ; i--) { | ||
r+=str.substring(i,i+1); | ||
} | ||
r="-"+r; | ||
}else { | ||
for (int i = str.length()-1; i >-1 ; i--) { | ||
r+=str.substring(i,i+1); | ||
} | ||
} | ||
try { | ||
re=Integer.parseInt(r); | ||
} catch (Exception e) { | ||
} | ||
return re; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
public class Solution { | ||
public boolean isPalindrome(int x) { | ||
if (x < 0) { | ||
return false; | ||
} | ||
int n = x; | ||
int a = 0; | ||
int p = 1; | ||
while (x != 0) { | ||
x /= 10; | ||
a++; | ||
p *= 10; | ||
if (a==1) { | ||
p/=10; | ||
} | ||
} | ||
int b = a / 2; | ||
if (b == 0) { | ||
return true; | ||
} | ||
if (x < 0) { | ||
return false; | ||
} | ||
int n = x; | ||
int a = 0; | ||
int p = 1; | ||
while (x != 0) { | ||
x /= 10; | ||
a++; | ||
p *= 10; | ||
if (a==1) { | ||
p/=10; | ||
} | ||
} | ||
int b = a / 2; | ||
if (b == 0) { | ||
return true; | ||
} | ||
|
||
for (int i = 0; i < b; i++) { | ||
int begin = n / p; | ||
int end = n % 10; | ||
if (begin != end) { | ||
return false; | ||
} else { | ||
n = n - (begin * p); | ||
p /= 100; | ||
n = (n - end) / 10; | ||
} | ||
} | ||
return true; | ||
} | ||
for (int i = 0; i < b; i++) { | ||
int begin = n / p; | ||
int end = n % 10; | ||
if (begin != end) { | ||
return false; | ||
} else { | ||
n = n - (begin * p); | ||
p /= 100; | ||
n = (n - end) / 10; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
86 changes: 43 additions & 43 deletions
86
Algorithms/010. Regular Expression Matching/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
public class Solution { | ||
public boolean isMatch(String s, String p) { | ||
if (p.length()==0) { | ||
return s.length()==0; | ||
} | ||
return s.length()==0; | ||
} | ||
if (s.length()==0) { | ||
if (p.length()%2!=0) { | ||
return false; | ||
} | ||
char[] c=p.toCharArray(); | ||
for (int i = 1; i < c.length; i+=2) { | ||
if (c[i]!='*') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (p.length()%2!=0) { | ||
return false; | ||
} | ||
char[] c=p.toCharArray(); | ||
for (int i = 1; i < c.length; i+=2) { | ||
if (c[i]!='*') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
int pl=p.length(); | ||
int sl=s.length(); | ||
String ep=p.substring(pl-1,pl); | ||
if (ep.equals("*")) { | ||
if (p.substring(pl-2,pl-1).equals(".")) { | ||
for (int i = sl-1; i >=0-1; i--) { | ||
if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}else { | ||
String k=p.substring(pl-2,pl-1); | ||
for (int i = sl-1; i >=0; i--) { | ||
String t=s.substring(i,i+1); | ||
if (t.equals(k)) { | ||
if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { | ||
return true; | ||
} | ||
}else { | ||
return isMatch(s.substring(0, i+1),p.substring(0, pl-2)); | ||
} | ||
} | ||
return isMatch("",p.substring(0, pl-2)); | ||
} | ||
}else if(ep.equals(".")){ | ||
return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); | ||
}else { | ||
String es=s.substring(sl-1,sl); | ||
if (ep.equals(es)) { | ||
return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); | ||
} | ||
return false; | ||
} | ||
if (p.substring(pl-2,pl-1).equals(".")) { | ||
for (int i = sl-1; i >=0-1; i--) { | ||
if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}else { | ||
String k=p.substring(pl-2,pl-1); | ||
for (int i = sl-1; i >=0; i--) { | ||
String t=s.substring(i,i+1); | ||
if (t.equals(k)) { | ||
if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { | ||
return true; | ||
} | ||
}else { | ||
return isMatch(s.substring(0, i+1),p.substring(0, pl-2)); | ||
} | ||
} | ||
return isMatch("",p.substring(0, pl-2)); | ||
} | ||
}else if(ep.equals(".")){ | ||
return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); | ||
}else { | ||
String es=s.substring(sl-1,sl); | ||
if (ep.equals(es)) { | ||
return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.