Skip to content

Commit 39e96a4

Browse files
committed
first add
0 parents  commit 39e96a4

15 files changed

+757
-0
lines changed

#1/Solution1.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
int[] copy = Arrays.copyOf(nums, nums.length);
4+
quickSort(copy, 0, copy.length - 1);
5+
int i = 0;
6+
int j = copy.length - 1;
7+
while (i < j) {
8+
if (copy[i] + copy[j] == target) break;
9+
else if (copy[i] + copy[j] < target) i++;
10+
else j--;
11+
}
12+
i = findIndex(nums, copy[i], -1);
13+
j = findIndex(nums, copy[j], i);
14+
return new int[] {i, j};
15+
}
16+
17+
private int findIndex(int[] nums, int v, int exp) {
18+
int index = 0;
19+
for (int k = 0; k < nums.length; k++) {
20+
if (nums[k] == v && k != exp) {
21+
index = k;
22+
break;
23+
}
24+
}
25+
return index;
26+
}
27+
28+
private void quickSort(int[] nums, int a, int b) {
29+
if (a >= b) return;
30+
boolean m = true;
31+
int l = a;
32+
int r = b;
33+
while (l < r) {
34+
if (m) {
35+
while (nums[r] > nums[l] && l < r) {
36+
r--;
37+
}
38+
swap(nums, l, r);
39+
if (l < r) l++;
40+
m = false;
41+
} else{
42+
while (nums[r] > nums[l] && l < r) {
43+
l++;
44+
}
45+
swap(nums, l, r);
46+
if (l < r) r--;
47+
m = true;
48+
}
49+
}
50+
if (a < l - 1) quickSort(nums, a, l - 1);
51+
if (b > l + 1) quickSort(nums, l + 1, b);
52+
}
53+
54+
private void swap(int[] nums, int i, int j) {
55+
if (i == j) return;
56+
nums[i] = nums[i] ^ nums[j];
57+
nums[j] = nums[i] ^ nums[j];
58+
nums[i] = nums[i] ^ nums[j];
59+
}
60+
}

#1/SolutionBest.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> dict = new HashMap<>(nums.length / 2);
4+
for (int k = 0; k < nums.length; k++) {
5+
int coup = target - nums[k];
6+
if (dict.containsKey(coup)) {
7+
return new int[] {dict.get(coup), k};
8+
} else {
9+
dict.put(nums[k], k);
10+
}
11+
}
12+
return new int[] {-1, -1};
13+
}
14+
15+
}

#10/Solution1.java

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
class Solution {
2+
String str;
3+
Node[] nodes;
4+
int[] starNodeIndexes;
5+
int cursor;
6+
int nodeCursor;
7+
8+
class Node {
9+
protected char pattern;
10+
protected int matchIndex;
11+
12+
public Node(char pattern) {
13+
this.pattern = pattern;
14+
}
15+
16+
public boolean isMatch() {
17+
return str.charAt(matchIndex) == pattern;
18+
}
19+
public boolean isMatch(char c) {
20+
return c == pattern;
21+
}
22+
}
23+
24+
class DotNode extends Node {
25+
public DotNode(char pattern) {
26+
super(pattern);
27+
}
28+
@Override
29+
public boolean isMatch() {
30+
return true;
31+
}
32+
@Override
33+
public boolean isMatch(char c) {
34+
return true;
35+
}
36+
}
37+
38+
class StarNode extends Node {
39+
protected int matchLength;
40+
protected Node patternNode;
41+
public StarNode(char pattern) {
42+
super(pattern);
43+
}
44+
@Override
45+
public boolean isMatch() {
46+
int max = matchIndex + matchLength;
47+
for (int i = matchIndex; i < max; i++) {
48+
if (!patternNode.isMatch(str.charAt(matchIndex))) {
49+
return false;
50+
}
51+
}
52+
return true;
53+
}
54+
public int maxMatchLength(int allowMax) {
55+
int i = 0;
56+
for (; i < allowMax; i++) {
57+
if (!patternNode.isMatch(str.charAt(matchIndex + i))) {
58+
break;
59+
}
60+
}
61+
return i;
62+
}
63+
}
64+
65+
private void compile(String p) {
66+
List<Node> list = new ArrayList<>();
67+
int starCount = 0;
68+
for (int i = 0; i < p.length(); i++) {
69+
char reg = p.charAt(i);
70+
Node node;
71+
if (p.charAt(i) == '.') {
72+
node = new DotNode(reg);
73+
} else {
74+
node = new Node(reg);
75+
}
76+
if (i < p.length() - 1 && p.charAt(i + 1) == '*') {
77+
StarNode snode = new StarNode('*');
78+
snode.patternNode = node;
79+
list.add(snode);
80+
i++;
81+
starCount++;
82+
} else {
83+
list.add(node);
84+
}
85+
}
86+
nodes = list.toArray(new Node[list.size()]);
87+
starNodeIndexes = new int[starCount];
88+
starCount = 0;
89+
for (int i = 0; i < nodes.length; i++) {
90+
if (nodes[i] instanceof StarNode) {
91+
starNodeIndexes[starCount++] = i;
92+
}
93+
}
94+
}
95+
96+
public boolean isMatch(String s, String p) {
97+
if (s.length() == 0 && p.length() == 0) {
98+
return true;
99+
}
100+
if (s.length() > 0 && p.length() == 0) {
101+
return false;
102+
}
103+
str = s;
104+
105+
compile(p);
106+
107+
if (s.length() < nodes.length - starNodeIndexes.length
108+
|| (starNodeIndexes.length == 0 && s.length() != nodes.length)) {
109+
return false;
110+
}
111+
112+
nodeCursor = 0;
113+
cursor = 0;
114+
int starCount = 0;
115+
while (nodeCursor < nodes.length) {
116+
Node node = nodes[nodeCursor];
117+
if (node instanceof StarNode) {
118+
StarNode snode = (StarNode)node;
119+
snode.matchIndex = cursor;
120+
121+
int restCharLength = s.length() - cursor;
122+
int minLength = starNodeIndexes.length - starCount > 1 ? 0 : restCharLength - (nodes.length - nodeCursor - 1);
123+
int maxLength = restCharLength - (nodes.length - nodeCursor) + starNodeIndexes.length - starCount;
124+
125+
int length = snode.maxMatchLength(maxLength);
126+
127+
if (length >= minLength) {
128+
snode.matchLength = length;
129+
starCount++;
130+
nodeCursor++;
131+
cursor += length;
132+
continue;
133+
} else { // 进入后面的回退到上一个*的算法
134+
snode.matchLength = 0;
135+
}
136+
137+
} else {
138+
node.matchIndex = cursor;
139+
if (node.isMatch()) {
140+
cursor++;
141+
nodeCursor++;
142+
continue;
143+
}
144+
}
145+
if (starCount > 0) {
146+
// 回退到'*'匹配的位置,并且把*匹配的个数减一
147+
while (starCount > 0) {
148+
int iStarCount = starCount - 1;
149+
int iNodeCursor = starNodeIndexes[iStarCount];
150+
StarNode snode = (StarNode) nodes[iNodeCursor];
151+
int iCursor = snode.matchIndex;
152+
153+
int iRestCharLength = s.length() - iCursor + 1;
154+
int minLength = starNodeIndexes.length - iStarCount > 1 ? 0 : iRestCharLength - (nodes.length - iNodeCursor - 1);
155+
156+
if (snode.matchLength > minLength) {
157+
snode.matchLength = snode.matchLength - 1;
158+
nodeCursor = iNodeCursor + 1;
159+
cursor = iCursor + snode.matchLength;
160+
break;
161+
} else {
162+
starCount--;
163+
}
164+
}
165+
if (starCount > 0) {
166+
continue;
167+
}
168+
}
169+
return false;
170+
}
171+
return true;
172+
}
173+
}

#15/Solution.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
List<List<Integer>> list = new ArrayList<>();
4+
if (nums.length < 2) return list;
5+
6+
quickSort(nums, 0, nums.length - 1);
7+
8+
9+
int m1 = nums[0] + nums[1];
10+
int m2 = nums[nums.length - 2] + nums[nums.length - 1];
11+
int end = nums.length - 1;
12+
while(end > 0 && nums[end] + m1 > 0) end--;
13+
int start = 0;
14+
while(start < end && nums[start] + m2 < 0) start++;
15+
16+
for (int left = start; left < end - 1; left++) {
17+
if (left > 0 && nums[left - 1] == nums[left]) continue;
18+
int middle = left + 1;
19+
int right = end;
20+
int exp = - nums[left];
21+
while (middle < right) {
22+
int sum = nums[middle] + nums[right];
23+
if (sum == exp) {
24+
List<Integer> item = new ArrayList<>();
25+
item.add(nums[left]);
26+
item.add(nums[middle]);
27+
item.add(nums[right]);
28+
list.add(item);
29+
while (middle + 1 < right && nums[middle] == nums[middle + 1]) middle++;
30+
while (right - 1 > middle && nums[right] == nums[right - 1]) right--;
31+
middle++;
32+
right--;
33+
} else if (sum < exp) {
34+
while (middle + 1 < right && nums[middle] == nums[middle + 1]) middle++;
35+
middle++;
36+
} else {
37+
while (right - 1 > middle && nums[right] == nums[right - 1]) right--;
38+
right--;
39+
}
40+
}
41+
42+
}
43+
44+
return list;
45+
}
46+
47+
48+
private void quickSort(int[] nums, int a, int b) {
49+
if (a >= b) return;
50+
boolean m = true;
51+
int l = a;
52+
int r = b;
53+
while (l < r) {
54+
if (m) {
55+
while (nums[r] > nums[l] && l < r) r--;
56+
swap(nums, l, r);
57+
if (l < r) l++;
58+
m = false;
59+
} else{
60+
while (nums[r] > nums[l] && l < r) l++;
61+
swap(nums, l, r);
62+
if (l < r) r--;
63+
m = true;
64+
}
65+
}
66+
if (a < l - 1) quickSort(nums, a, l - 1);
67+
if (b > l + 1) quickSort(nums, l + 1, b);
68+
}
69+
70+
private void swap(int[] nums, int i, int j) {
71+
if (i == j) return;
72+
nums[i] = nums[i] ^ nums[j];
73+
nums[j] = nums[i] ^ nums[j];
74+
nums[i] = nums[i] ^ nums[j];
75+
}
76+
77+
}

0 commit comments

Comments
 (0)