Skip to content

Commit fe5bcf2

Browse files
author
yanyichao
committed
Update 1、7、9、13、14、20
1 parent 5d95766 commit fe5bcf2

File tree

4 files changed

+119
-146
lines changed

4 files changed

+119
-146
lines changed

src/main/java/com/fishercoder/solutions/_1.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@
1515
*/
1616
public class _1 {
1717

18-
public static class Solution1 {
19-
public int[] twoSum(int[] nums, int target) {
20-
Map<Integer, Integer> map = new HashMap();
21-
int[] result = new int[2];
22-
for (int i = 0; i < nums.length; i++) {
23-
if (map.containsKey(target - nums[i])) {
24-
result[0] = map.get(target - nums[i]);
25-
result[1] = i;
26-
break;
27-
} else {
28-
map.put(nums[i], i);
29-
}
30-
}
31-
return result;
32-
}
33-
}
18+
public static class Solution1 {
19+
public int[] twoSum(int[] nums, int target) {
20+
Map<Integer, Integer> map = new HashMap<Integer, Integer>(nums.length);
21+
for (int i = 0; i < nums.length; i++) {
22+
if (map.containsKey(target - nums[i])) {
23+
int j = map.get(target - nums[i]);
24+
return new int[] { j, i };
25+
} else {
26+
map.put(nums[i], i);
27+
}
28+
}
29+
return null;
30+
}
31+
}
3432

3533
}

src/main/java/com/fishercoder/solutions/_14.java

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,31 @@
88

99
public class _14 {
1010

11-
public static class Solution1 {
12-
public String longestCommonPrefix(String[] strs) {
13-
if (strs.length == 0) {
14-
return "";
15-
}
11+
public static class Solution1 {
12+
public String longestCommonPrefix(String[] strs) {
13+
if (strs == null || strs.length == 0) {
14+
return "";
15+
}
16+
if (strs.length == 1) {
17+
return strs[0];
18+
}
19+
Character c;
20+
for (int i = 0;; i++) {// 可以增加i < strs[0].length()条件跳出循环,以防止程序错误时死循环
21+
c = null;
22+
for (String str : strs) {
23+
if (str == null || str.length() < i + 1) {
24+
return str + "";
25+
}
26+
if (c == null) {
27+
c = str.charAt(i);
28+
} else {
29+
if (!c.equals(str.charAt(i))) {
30+
return str.substring(0, i) + "";
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
1637

17-
int i = 0;
18-
String prefix = "";
19-
String result;
20-
boolean broken = false;
21-
while (true) {
22-
i++;
23-
result = prefix;
24-
if (i > strs[0].length()) {
25-
break;//this will break out the while loop
26-
}
27-
prefix = strs[0].substring(0, i);
28-
for (String word : strs) {
29-
if (i > word.length() || !word.startsWith(prefix)) {
30-
broken = true;
31-
break;//this will only break out of the for loop
32-
}
33-
}
34-
if (broken) {
35-
break;//this will break out the while loop
36-
}
37-
}
38-
return result;
39-
}
40-
}
41-
42-
public static class Solution2 {
43-
//horizontal scan
44-
public String longestCommonPrefix(String[] strs) {
45-
if (strs.length == 0) {
46-
return "";
47-
}
48-
String prefix = strs[0];
49-
for (int i = 1; i < strs.length; i++) {
50-
while (strs[i].indexOf(prefix) != 0) {
51-
prefix = prefix.substring(0, prefix.length() - 1);
52-
if (prefix.isEmpty()) {
53-
return "";
54-
}
55-
}
56-
}
57-
return prefix;
58-
}
59-
}
60-
61-
public static class Solution3 {
62-
//vertical scan
63-
public String longestCommonPrefix(String[] strs) {
64-
if (strs.length == 0) {
65-
return "";
66-
}
67-
for (int i = 0; i < strs[0].length(); i++) {
68-
char c = strs[0].charAt(i);
69-
for (int j = 1; j < strs.length; j++) {
70-
if (i == strs[j].length() || strs[j].charAt(i) != c) {
71-
return strs[0].substring(0, i);
72-
}
73-
}
74-
}
75-
return strs[0];
76-
}
77-
}
7838
}
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayDeque;
4-
import java.util.Deque;
5-
63
/**
74
* 20. Valid Parentheses
85
*
96
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
107
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.*/
118
public class _20 {
129

13-
public boolean isValid(String s) {
14-
Deque<Character> stack = new ArrayDeque<>();
15-
for (int i = 0; i < s.length(); i++) {
16-
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
17-
stack.push(s.charAt(i));
18-
} else {
19-
if (stack.isEmpty()) {
20-
return false;
21-
} else {
22-
if (stack.peek() == '(' && s.charAt(i) != ')') {
23-
return false;
24-
} else if (stack.peek() == '{' && s.charAt(i) != '}') {
25-
return false;
26-
} else if (stack.peek() == '[' && s.charAt(i) != ']') {
27-
return false;
28-
}
29-
stack.pop();
30-
}
31-
}
32-
}
33-
return stack.isEmpty();
34-
}
10+
public boolean isValid(String s) {
11+
char[] stack = new char[s.length()];
12+
int head = 0;
13+
for (char c : s.toCharArray()) {
14+
switch (c) {
15+
case '[':
16+
case '(':
17+
case '{':
18+
stack[head++] = c; // push
19+
break;
20+
case ']':
21+
if (head != 0 && stack[--head] == '[') { // pop
22+
break;
23+
} else {
24+
return false;
25+
}
26+
case ')':
27+
if (head != 0 && stack[--head] == '(') {
28+
break;
29+
} else {
30+
return false;
31+
}
32+
case '}':
33+
if (head != 0 && stack[--head] == '{') {
34+
break;
35+
} else {
36+
return false;
37+
}
38+
default:
39+
break;
40+
}
41+
}
42+
return head == 0;
43+
}
3544
}

src/main/java/com/fishercoder/solutions/_9.java

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,51 @@
1717
*/
1818
public class _9 {
1919

20-
public static class Solution1 {
21-
public boolean isPalindrome(int x) {
22-
if (x == 0) {
23-
return true;
24-
}
25-
if (x < 0) {
26-
return false;
27-
}
28-
int rev = 0;
29-
int tmp = x;
30-
while (tmp != 0) {
31-
rev *= 10;
32-
rev += tmp % 10;
33-
tmp /= 10;
34-
}
35-
return rev == x;
36-
}
37-
}
20+
public static class Solution1 {
21+
public boolean isPalindrome(int x) {
22+
if (x == 0) {
23+
return true;
24+
}
25+
if (x < 0) {
26+
return false;
27+
}
28+
long rev = 0;
29+
int tmp = x;
30+
while (tmp != 0) {
31+
rev *= 10;
32+
rev += tmp % 10;
33+
tmp /= 10;
34+
}
35+
return rev == (long) x;
36+
}
37+
}
3838

39-
/**credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow
40-
* reversing only half and then compare if they're equal.*/
41-
public static class Solution2 {
42-
public boolean isPalindrome(int x) {
43-
if (x < 0) {
44-
return false;
45-
} else if (x == 0) {
46-
return true;
47-
} else if (x % 10 == 0) {
48-
return false;
49-
}
50-
int reversed = 0;
51-
while (x > reversed) {
52-
int digit = x % 10;
53-
reversed *= 10;
54-
reversed += digit;
55-
x /= 10;
56-
}
57-
return (x == reversed || x == reversed / 10);
58-
}
59-
}
39+
/**credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow
40+
* reversing only half and then compare if they're equal.*/
41+
public static class Solution2 {
42+
public boolean isPalindrome(int x) {
43+
if (x < 0) {
44+
return false;
45+
}
46+
if (x / 10 == 0) {
47+
return true;
48+
}
49+
50+
int length = 1;
51+
while (x / length >= 10) {
52+
length *= 10;
53+
}
54+
55+
while (x > 0) {
56+
if (x / length != x % 10) {
57+
return false;
58+
}
59+
x = (x % length) / 10;
60+
length /= 100;
61+
}
62+
63+
return true;
64+
}
65+
}
6066

6167
}

0 commit comments

Comments
 (0)