-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.txt
279 lines (249 loc) · 7.58 KB
/
code.txt
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
277
278
279
1.ZZZZZZZZLO;ftrgggggggggggggggt求数组内子数组的最大和:
思路:申请2个临时变量,一个sum一个maxsum,maxsum
1.当所有数为非正数时,最大值则是最大的子数组和
2.从第一个是正数的值开始往后累加,并记录下累加得到的最大值
3.当累加的值出现负数,说明有效的子数组已经结束,开始下一组的累加和最大值比较,到数组结束求出
最大的和
int maxSubArray(int []num){
int sum = 0;
int maxSum = num[0];
for(int i = 0;i<num.length;i++){
if(maxSum<=0){
maxSum = maxSum<num[i] ? num[i] : maxSum ;
sum = maxSum > 0 ? maxSum : 0;
}else{
sum += num[i];
maxSum = maxSum > sum ? maxSum : sum;
if(sum<0) sum = 0;
}
}
return maxSum;
}
2.字符串转换成整数
思路:把字符串先换成字符数组,先判断符号,然后遍历字符数组获取字符,并计算组合成整数
public class Solution{
public int StrToInt(String str){
if(str.length == 0 || str.equals("")) return 0;
char []a =str.toCharArray();
int fuhao = 0;
if(a[0] == '-') fuhao =1;
int sum = 0;
for(int i=fuhao;i<a.length;i++){
if(a[i] == '+' ) continue;
if(a[i]<48 || a[i]>57) return 0;
sum = sum*10 +a[i] -48;
}
return fuhao == 0 ? sum : sum * -1 ;
}
}
如果要考虑各种溢出等情况,需要考虑所有可能的测试用例,使逻辑严谨
http://wiki.jikexueyuan.com/project/for-offer/question-forty-nine.html
3.快速排序的实现:
public int Partition(int n[],int left,int right){
int pivot = n[left];
while(left<right){
while(left<right && pivot<n[right]){
right--;
}
if(left<right) n[left]=n[right];
while(left<right && n[left]<pivot){
left++;
}
if(left<right) n[right]=n[left];
}
n[left] = pivot;
return left;
}
递归实现:
public static void QuickSort(int n[],int left,int right){
int dp;
if(left<right){
dp = Partition(n,left,right);
}
if(right>dp) QuickSort(n,dp+1,right);
if(left<dp) QuickSort(n,left,dp-1);
}
非递归实现:
public static void QuickSort(int n[],int left,int right){
if(a == null || left>right || left<0 || right<=0) return;
Stack<int> temp;
int i,j;
temp.push(right);//先存右边指针
temp.push(left);//再存左边指针
while(!temp.empty()){
i=temp.top();
temp.pop();
j=temp.top();
temp.pop();
if(i<j){
int k = Partition(n,i,j);
if(i<k){
temp.push(k-1);
temp.push(i);
}
if<j>k){
temp.push(j);
temp.push(k+1);
}
}
}
4.K路归并问题(已排序)
/**
* 解法1:逐个合并数组,时间复杂度O(n*k),n >> k
* 合并k个排序(升序)数组
* http://www.lintcode.com/zh-cn/problem/merge-k-sorted-arrays/
* @author yzwall
*/
class Solution {
public List<Integer> mergekSortedArrays(int[][] arrays) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (arrays == null || arrays.length == 0 || arrays[0].length == 0) {
return list;
}
if (arrays.length == 1) {
Arrays.sort(arrays[0]);
for (int num : arrays[0]) {
list.add(num);
}
return list;
}
int[] temp = mergeTwoArrays(arrays[0], arrays[1]);
for (int i = 2; i < arrays.length; i++) {
temp = mergeTwoArrays(temp, arrays[i]);
}
for (int num : temp) {
list.add(num);
}
return list;
}
private int[] mergeTwoArrays(int[] A, int[] B) {
if (A.length == 0 || B.length == 0) {
return new int[0];
}
int[] temp = new int[A.length + B.length];
int index = 0, i = 0, j = 0;
while (i < A.length && j < B.length) {
if (A[i] < B[j]) {
temp[index++] = A[i++];
} else {
temp[index++] = B[j++];
}
}
while (i < A.length) {
temp[index++] = A[i++];
}
while (j < B.length) {
temp[index++] = B[j++];
}
return temp;
}
}
/**
* 解法2:分治法K路归并,时间复杂度O(n logk)
* 合并k个排序(升序)数组
* http://www.lintcode.com/zh-cn/problem/merge-k-sorted-arrays/
* @author yzwall
*/
class Solution20 {
public List<Integer> mergekSortedArrays(int[][] arrays) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (arrays == null || arrays.length == 0 || arrays[0].length == 0) {
return list;
}
int[] ans = kMergeSort(arrays, 0, arrays.length - 1);
for (int num : ans) {
list.add(num);
}
return list;
}
// 分治递归深度为O(log k), 每层合并时间复杂度O(n)
private int[] kMergeSort(int[][] arrays, int start, int end) {
if (start >= end) {
return arrays[start];
}
int mid = start + (end - start) / 2;
int[] left = kMergeSort(arrays, start, mid);
int[] right = kMergeSort(arrays, mid + 1, end);
return mergeTwoArrays(left, right);
}
private int[] mergeTwoArrays(int[] A, int[] B) {
int[] temp = new int[A.length + B.length];
int index = 0, i = 0, j = 0;
while (i < A.length && j < B.length) {
if (A[i] < B[j]) {
temp[index++] = A[i++];
} else {
temp[index++] = B[j++];
}
}
while (i < A.length) {
temp[index++] = A[i++];
}
while (j < B.length) {
temp[index++] = B[j++];
}
return temp;
}
}
/**
* 解法3:最小堆实现K路归并,时间复杂度O(n logk)
* 合并k个排序(升序)数组
* http://www.lintcode.com/zh-cn/problem/merge-k-sorted-arrays/
* @author yzwall
*/
class Solution18 {
private class NewInteger {
int value, row, col;
public NewInteger(int value, int row, int col) {
this.value = value;
this.row = row;
this.col = col;
}
}
public List<Integer> mergekSortedArrays(int[][] arrays) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (arrays == null || arrays.length == 0 || arrays[0].length == 0) {
return list;
}
PriorityQueue<NewInteger> pq = new PriorityQueue<>(arrays.length, new Comparator<NewInteger>() {
public int compare(NewInteger o1, NewInteger o2) {
return o1.value < o2.value ? -1 : 1;
}
});
for (int i = 0; i < arrays.length; i++) {
pq.offer(new NewInteger(arrays[i][0], i, 0));
}
while (!pq.isEmpty()) {
NewInteger min = pq.poll();
if (min.col + 1 < arrays[min.row].length) {
pq.offer(new NewInteger(arrays[min.row][min.col + 1], min.row, min.col + 1));
}
list.add(min.value);
}
return list;
}
}
/**
* 解法4:暴力方法,将所有数组添加到List,统一排序,时间复杂度O(n*k + nlogn), n >> k
* 合并k个排序(升序)数组
* http://www.lintcode.com/zh-cn/problem/merge-k-sorted-arrays/
* @author yzwall
*/
class Solution19 {
public List<Integer> mergekSortedArrays(int[][] arrays) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (arrays == null || arrays.length == 0 || arrays[0].length == 0) {
return list;
}
for (int i = 0; i < arrays.length; i++) {
addToList(list, arrays[i]);
}
Collections.sort(list);
return list;
}
private void addToList(ArrayList<Integer>list, int[] nums) {
for (int num : nums) {
list.add(num);
}
}
}