Skip to content

Commit c74de69

Browse files
author
Ysc
committed
init repo
1 parent 734adf3 commit c74de69

28 files changed

+1803
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
GenerateCodeTable.class
3+
GenerateCodeTable$TableRow.class
4+
gh-md-toc
5+
Solution.class

Java/0-Two Sum.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
E
2+
1517474043
3+
tags:
4+
5+
方法1:
6+
7+
8+
方法2:
9+
10+
```
11+
/*
12+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
13+
14+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
15+
16+
Example:
17+
18+
Given nums = [2, 7, 11, 15], target = 9,
19+
20+
Because nums[0] + nums[1] = 2 + 7 = 9,
21+
return [0, 1].
22+
*/
23+
24+
/*
25+
Thoughts:
26+
很简单的一个顺次判断。把j设定成i+1,只检查一轮。
27+
*/
28+
class Solution {
29+
public int[] twoSum(int[] nums, int target) {
30+
int x[] = new int[2];
31+
for (int i = 0; i<nums.length; i++){
32+
for(int j = i+1; j < nums.length; j++){
33+
if ( target == nums[i] + nums[j]){
34+
x[0] = i;
35+
x[1] = j;
36+
}
37+
}
38+
39+
}
40+
41+
return x;
42+
}
43+
}
44+
45+
/*
46+
47+
48+
*/
49+
50+
```

Java/1-Reverse Integer.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Easy Leetcode 7
2+
1517474043
3+
tags:
4+
5+
方法1:
6+
pop头部的第一个数字push到末尾
7+
pop the first digit of the input number and push it to the end.
8+
9+
方法2:
10+
把数字转换成字符串
11+
convert the input number into a string.
12+
注意有个大坑
13+
重点注意的是
14+
15+
Math.abs(Integer.MIN_VALUE); //-2147483648
16+
Math.abs(Long.MIN_VALUE); //-9223372036854775808
17+
18+
```
19+
/*
20+
21+
*/
22+
23+
/*
24+
方法1
25+
Thoughts:
26+
参考了别人的答案。
27+
*/
28+
29+
class Solution {
30+
public int reverse(int x) {
31+
32+
if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) return 0;
33+
34+
long rev = 0;
35+
int tmp = Math.abs(x); //不取绝对值也可以
36+
37+
while(tmp != 0){
38+
rev = (rev * 10) + tmp % 10;
39+
tmp /= 10;
40+
}
41+
42+
if(rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) return 0;
43+
44+
return (int)(x < 0 ? -rev : rev);
45+
}
46+
47+
}
48+
49+
/*
50+
方法2
51+
转换成字符串
52+
!!!注意有个大坑。math.abs
53+
*/
54+
55+
class Solution {
56+
public int reverse(int x) {
57+
58+
if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) return 0;
59+
60+
long rev = 0;
61+
String tmp = Long.toString(Math.abs((long)x));
62+
63+
String reverse = new StringBuffer(tmp).reverse().toString();
64+
65+
rev = Long.parseLong(reverse);
66+
67+
if( rev > (long)Integer.MAX_VALUE || rev < (long)Integer.MIN_VALUE
68+
) return 0;
69+
70+
return (int)(x < 0 ? -rev : rev);
71+
}
72+
73+
}
74+
75+
76+
```

Java/10-Valid Palindrome.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Easy Leetcode 125
2+
1517474043
3+
tags: String
4+
##题目不难但是容易timeexceed需要考虑如何降低时间复杂度
5+
方法1:
6+
自己做的会超时
7+
8+
方法2:
9+
网上的参考非常直观的方法把string倒过来
10+
11+
方法3:
12+
最主流的方法two pointers从两头往中间比较
13+
14+
方法4:
15+
也是一个很巧妙的办法通过建立一个char的array来avoid大小写的check
16+
```
17+
/*
18+
19+
*/
20+
21+
/*
22+
方法1
23+
Thoughts:
24+
25+
###
26+
*/
27+
class Solution {
28+
public boolean isNum(char x) {
29+
if(48<= x && x<=57 ) return true;
30+
return false;
31+
}
32+
public boolean isAlpha(char x) {
33+
if(65<= x && x<=90 ) return true;
34+
if(97<= x && x<=122 ) return true;
35+
return false;
36+
}
37+
public boolean isEqual(char x, char y) {
38+
if(isNum(x)&&isNum(y)){
39+
return x==y;
40+
}
41+
if(isAlpha(x)&&isAlpha(y)){
42+
if(x ==y || x-y==32 || y-x==32 ){
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
public boolean isPalindrome(String s) {
50+
boolean flag = true;
51+
if(s.length() ==0) return true;
52+
String ss = "";
53+
for (int i = 0; i < s.length(); i++) {
54+
char x = s.charAt(i);
55+
if(isAlpha(x)||isNum(x)) ss += ""+x;
56+
}
57+
if(ss.length() == 0 || ss.length() ==1) return true;
58+
for (int i = 0; i < ss.length()/2; i++) {
59+
char x = ss.charAt(i);
60+
char y = ss.charAt(ss.length()-i-1);
61+
if(!isEqual(x,y)){
62+
flag = false;
63+
break;
64+
}
65+
}
66+
return flag;
67+
68+
69+
}
70+
}
71+
72+
73+
/*
74+
方法2
75+
非常直观的方法
76+
*/
77+
class Solution {
78+
public boolean isPalindrome(String s) {
79+
String actual = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
80+
String rev = new StringBuffer(actual).reverse().toString();
81+
return actual.equals(rev);
82+
}
83+
}
84+
/*
85+
方法3
86+
two pointers
87+
*/
88+
89+
public class Solution {
90+
public boolean isPalindrome(String s) {
91+
if (s.isEmpty()) {
92+
return true;
93+
}
94+
int head = 0, tail = s.length() - 1;
95+
char cHead, cTail;
96+
while(head <= tail) {
97+
cHead = s.charAt(head);
98+
cTail = s.charAt(tail);
99+
if (!Character.isLetterOrDigit(cHead)) {
100+
head++;
101+
} else if(!Character.isLetterOrDigit(cTail)) {
102+
tail--;
103+
} else {
104+
if (Character.toLowerCase(cHead) != Character.toLowerCase(cTail)) {
105+
return false;
106+
}
107+
head++;
108+
tail--;
109+
}
110+
}
111+
112+
return true;
113+
}
114+
}
115+
/*
116+
方法4
117+
118+
*/
119+
public class Solution {
120+
private static final char[]charMap = new char[256];
121+
static{
122+
for(int i=0;i<10;i++){
123+
charMap[i+'0'] = (char)(1+i); // numeric
124+
}
125+
for(int i=0;i<26;i++){
126+
charMap[i+'a'] = charMap[i+'A'] = (char)(11+i); //alphabetic, ignore cases
127+
}
128+
}
129+
public boolean isPalindrome(String s) {
130+
char[]pChars = s.toCharArray();
131+
int start = 0,end=pChars.length-1;
132+
char cS,cE;
133+
while(start<end){
134+
cS = charMap[pChars[start]];
135+
cE = charMap[pChars[end]];
136+
if(cS!=0 && cE!=0){
137+
if(cS!=cE)return false;
138+
start++;
139+
end--;
140+
}else{
141+
if(cS==0)start++;
142+
if(cE==0)end--;
143+
}
144+
}
145+
return true;
146+
}
147+
}
148+
149+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Easy Leetcode 345
2+
1517474043
3+
tags: String
4+
##题目不难.
5+
方法1:
6+
自己做的会超时
7+
8+
方法2:
9+
网上的参考巧妙地用indexOf来判断是否含有vowel
10+
11+
12+
```
13+
/*
14+
15+
*/
16+
17+
/*
18+
方法1
19+
基本的two pointers
20+
21+
###
22+
*/
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
class Solution {
26+
public String reverseVowels(String s) {
27+
HashMap<Character,String> vowel = new HashMap();
28+
vowel.put('a', "1");
29+
vowel.put('e', "1");
30+
vowel.put('i', "1");
31+
vowel.put('o', "1");
32+
vowel.put('u', "1");
33+
vowel.put('A', "1");
34+
vowel.put('E', "1");
35+
vowel.put('I', "1");
36+
vowel.put('O', "1");
37+
vowel.put('U', "1");
38+
39+
if (s.length() == 0) return "";
40+
char tmp;
41+
char[] ss = s.toCharArray();
42+
int head = 0, tail = ss.length-1;
43+
for (int i = 0; head < tail; i++) {
44+
if(vowel.containsKey(ss[head]) &&vowel.containsKey(ss[tail]) ) {
45+
tmp = ss[head];
46+
ss[head] = ss[tail];
47+
ss[tail] = tmp;
48+
head++;tail--;
49+
}
50+
if(vowel.containsKey(ss[head])&& !vowel.containsKey(ss[tail]) ){
51+
tail--;
52+
}
53+
if(!vowel.containsKey(ss[head])&& vowel.containsKey(ss[tail]) ){
54+
head++;
55+
}
56+
if(!vowel.containsKey(ss[head])&& !vowel.containsKey(ss[tail]) ){
57+
head++;tail--;
58+
}
59+
60+
61+
}
62+
String res = new String(ss);
63+
return res;
64+
}
65+
}
66+
67+
68+
/*
69+
方法2
70+
用indexOf来判断是否含有vowel
71+
*/
72+
public class Solution {
73+
public String reverseVowels(String s) {
74+
StringBuilder sb = new StringBuilder();
75+
int j = s.length() - 1;
76+
for (int i = 0; i < s.length(); i++)
77+
{
78+
if ("AEIOUaeiou".indexOf(s.charAt(i)) != -1)
79+
{
80+
while (j >= 0 && "AEIOUaeiou".indexOf(s.charAt(j)) == -1)
81+
{
82+
j--;
83+
}
84+
sb.append(s.charAt(j));
85+
j--;
86+
}
87+
else
88+
sb.append(s.charAt(i));
89+
}
90+
return sb.toString();
91+
}
92+
}
93+
94+
95+
96+
97+
98+
99+
```

0 commit comments

Comments
 (0)