-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
276 lines (223 loc) · 6.9 KB
/
Solution.java
File metadata and controls
276 lines (223 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.algorithms;
import java.util.Arrays;
/**
* Created by xifeng.yang on 2020/1/28
*/
public class Solution {
public static void main(String[] args) {
}
public int missingNumber(int[] nums) {
int n = nums.length + 1;
int sum = n * (n + 1) / 2;
int left = sum;
for (int i = 0; i < nums.length; i++) {
left -= nums[i];
}
return left;
}
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
int left = (m + n + 1) / 2;
int right = (m + n + 2) / 2;
return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
}
/**
* 找到nums1和nums2合并后从某个起始位置开始的第k个元素.
*
* @param nums1
* @param start1 nums1的起始位置
* @param nums2
* @param start2 nums2的起始位置
* @param k
* @return
*/
public int findKth(int[] nums1, int start1, int[] nums2, int start2, int k) {
if (start1 >= nums1.length) {
return nums2[start2 + k - 1];
}
if (start2 >= nums2.length) {
return nums1[start1 + k - 1];
}
if (k == 1) {
return Math.min(nums1[start1], nums2[start2]);
}
int midVal1 = (start1 + k / 2 - 1 < nums1.length) ? nums1[start1 + k / 2 - 1] : Integer.MAX_VALUE;
int midVal2 = (start2 + k / 2 - 1 < nums2.length) ? nums2[start2 + k / 2 - 1] : Integer.MAX_VALUE;
if (midVal1 < midVal2) {
return findKth(nums1, start1 + k / 2, nums2, start2, k - k / 2);
} else {
return findKth(nums1, start1, nums2, start2 + k / 2, k - k / 2);
}
}
public String convert(String s, int numRows) {
if (s == null) {
return null;
}
if (numRows == 1) {
return s;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < s.length(); j++) {
if ((j % (numRows * 2 - 2) == i) || (j % (numRows * 2 - 2) == (numRows * 2 - 2) - i)) {
stringBuilder.append(s.charAt(j));
}
}
}
return stringBuilder.toString();
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode result = (l1.val <= l2.val) ? l1 : l2;
ListNode cur = result;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
cur = cur.next;
} else {
cur.next = l2;
l2 = l2.next;
cur = cur.next;
}
}
if (l1 != null) {
cur.next = l1;
} else {
cur.next = l2;
}
return result;
}
public static void merge(int[] nums1, int m, int[] nums2, int n) {
System.out.println("merge...");
if (nums1 == null || nums2 == null) {
return;
}
int len = m;
for (int i = 0; i < n; i++) {
System.out.println("i = " + i);
int j = len - 1;
while (j >= 0) {
System.out.println("j = " + j);
if (nums1[j] >= nums2[i]) {
nums1[j + 1] = nums1[j];
j--;
} else {
break;
}
}
nums1[j + 1] = nums2[i];
len += 1;
}
}
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for (int i = 1; i < m; i++) {
pre = pre.next;
}
head = pre.next;
ListNode current;
//head始终指向第m个元素, prev始终指向head的前一个元素. 如果m=1,则prev指向dummy.
//子链表[m..n]起始状态下, 队首为head, 前一个元素为prev.
for (int i = m; i < n; i++) {
//将head的后一个元素移动到队首, head相应后移一位.
current = head.next;
head.next = current.next;
current.next = pre.next;
pre.next = current;
}
return dummy.next;
}
public ListNode reverseBetween2(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for (int i = 1; i < m; i++) {
pre = pre.next;
}
head = pre.next;
ListNode current = head;
ListNode prev = null;
for (int i = m; i <= n; i++) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
pre.next = prev;
head.next = current;
return dummy.next;
}
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
Arrays.sort(nums);
int[] lookup = new int[nums.length];
lookup[0] = 1;
for (int i = 1; i < nums.length; i++) {
int maxValue = 1;
for (int k = 0; k < i; k++) {
if (nums[i] == nums[k] + 1) {
maxValue = Integer.max(maxValue, lookup[k] + 1);
}
}
lookup[i] = maxValue;
}
int max = 0;
for (int i = 0; i < lookup.length; i++) {
if (max < lookup[i]) {
max = lookup[i];
}
}
return max;
}
public static int myAtoi(String str) {
if (str == null) {
return 0;
}
String content = str.trim();
if (content.length() == 0) {
return 0;
}
int bol = 1;
int ans = 0;
char[] cdhr = content.toCharArray();
int i = 0;
if (cdhr[0] == '-') {
bol = -1;
i = i + 1;
} else if (cdhr[0] == '+') {
i = i + 1;
}
for (; i < content.length(); i++) {
if (48 > content.charAt(i) || content.charAt(i) > 57) {
break;
}
if (ans * bol > Integer.MAX_VALUE / 10 || ans * bol == Integer.MAX_VALUE / 10 && (cdhr[i] - 48) > 7) {
return Integer.MAX_VALUE;
}
if (ans * bol < Integer.MIN_VALUE / 10 || ans * bol == Integer.MIN_VALUE / 10 && (cdhr[i] - 48) > 8) {
return Integer.MIN_VALUE;
}
ans = ans * 10 + (cdhr[i] - 48);
}
ans = ans * bol;
return ans;
}
}