-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
448 lines (415 loc) · 12.1 KB
/
Main.java
File metadata and controls
448 lines (415 loc) · 12.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
class SolutionJZ22 {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>() ;
ArrayList<Integer> arrayList=new ArrayList<>();
if(root==null)
return arrayList;
TreeNode p=root;
queue.offer(root);
while(!queue.isEmpty()){
p=queue.poll();
arrayList.add(p.val);
if(p.left!=null)
queue.offer(p.left);
if(p.right!=null)
queue.offer(p.right);
}
return arrayList;
}
}
class SolutionJZ23 {
public boolean VerifySquenceOfBST(int [] sequence) {
int len = sequence.length;
if(len==0) return false;
int root = sequence[len-1];
int i=0;
for(;i<len-1;i++){
if(sequence[i]>root) break;
}
int j=i;
for(;j<len-1;j++){
if(sequence[j]<root) return false;
}
boolean left=true,right=true;
if(i>0) left=VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,i));
if(i<len-1) right=VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,len-1));
return left && right;
}
}
class SolutionJZ24 {
private ArrayList<ArrayList<Integer>> result=new ArrayList<>();
private ArrayList<Integer> path = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null)
return result;
path.add(root.val);
target=target-root.val;
if(target==0&&root.left==null&&root.right==null)
result.add(new ArrayList<Integer>(path));
//因为在每一次的递归中,我们使用的是相同的result引用
FindPath(root.left,target);
FindPath(root.right,target);
path.remove(path.size()-1);
return result;
}
}
class SolutionJZ25 {
public RandomListNode Clone(RandomListNode pHead) {
cloneNodes(pHead);
connectSibling(pHead);
return reconnectNode(pHead);
}
//将每个结点进行复制,并进行连接
void cloneNodes(RandomListNode pHead){
RandomListNode pNode=pHead;
while (pNode!=null){
RandomListNode pCloned=new RandomListNode(pNode.label);
pCloned.label=pNode.label;
pCloned.next=pNode.next;
pCloned.random=null;
pNode.next=pCloned;
pNode=pCloned.next;
}
}
//进行随机结点的拷贝,利用cloneNodes的链表
void connectSibling(RandomListNode pHead){
RandomListNode pNode=pHead;
while(pNode!=null){
RandomListNode pCloned=pNode.next;
if(pNode.random!=null){
pCloned.random=pNode.random.next;
}
pNode=pCloned.next;
}
}
//将connectSibling的链表进行分离,奇数位置结点和偶数位置结点
RandomListNode reconnectNode(RandomListNode pHead){
RandomListNode pNode=pHead;//需进行分离的链表
RandomListNode pClonedHead=null;//复制链表的头节点
RandomListNode pClonedNode=null;//复制链表的临时结点(可以看作尾结点,进行尾插法)
if(pNode!=null){
pClonedHead=pClonedNode=pNode.next;
pNode.next=pClonedNode.next;
pNode=pNode.next;
}
while (pNode!=null){
pClonedNode.next=pNode.next;
pClonedNode=pClonedNode.next;
pNode.next=pClonedNode.next;
pNode=pNode.next;
}
return pClonedHead;
}
}
class SolutionJZ26 {
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null){
return null;
}
ArrayList<TreeNode> list = new ArrayList<>();
addList(pRootOfTree, list);
return changeList(list);
}
//中序遍历,在list中按遍历顺序保存
public void addList(TreeNode pRootOfTree, ArrayList<TreeNode> list){
if(pRootOfTree.left != null){
addList(pRootOfTree.left, list);
}
list.add(pRootOfTree);
if(pRootOfTree.right != null){
addList(pRootOfTree.right, list);
}
}
//遍历list,修改指针
public TreeNode changeList(ArrayList<TreeNode> list){
for(int i = 0; i < list.size() - 1; i++){
list.get(i).right = list.get(i + 1);
list.get(i + 1).left = list.get(i);
}
return list.get(0);
}
}
class SolutionJZ28 {
public int MoreThanHalfNum_Solution(int [] array) {
int[] temp=new int[array.length];
int i;
for(i=0;i<temp.length;i++){
temp[i]=0;
}
for(i=0;i<array.length;i++){
temp[array[i]]++;
}
for(i=0;i<temp.length;i++){
if(temp[i]>(array.length/2)){
return i;
}
}
return 0;
}
}
/**
* 未完成
*/
class SolutionJZ32 {
public String PrintMinNumber(int [] numbers) {
int length=numbers.length;
String[] string=new String[length];
int i;
for(i=0;i<string.length;i++){
string[i]=Integer.toString(numbers[i]);
}
Arrays.sort(string);
String s=new String();
for(i=string.length-1;i>=0;i--)
s=s+string[i];
return s;
}
}
class SolutionJZ36 {
/*
暴力求解方法
*/
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode h1=pHead1,h2=pHead2;
while(h1!=null){
while (h2!=null){
if(h2==h1)
return h1;
h2=h2.next;
}
h2=pHead2;
h1=h1.next;
}
return null;
}
/*
双指针法:可以看作将两个链表拼接起来,长度都为a+b
leetcode160
*/
public ListNode FindFirstCommonNode2(ListNode pHead1, ListNode pHead2) {
ListNode h1=pHead1,h2=pHead2;
while(h1!=h2){
h1=h1!=null?h1.next:pHead2;
h2=h2!=null?h2.next:pHead1;
}
return h1;
}
}
class SolutionJZ38 {
public int TreeDepth(TreeNode root) {
if(root==null)
return 0;
return (TreeDepth(root.left)>TreeDepth(root.right)?TreeDepth(root.left):TreeDepth(root.right))+1;
}
}
class SolutionJZ46 {
/**
* 模拟法
* @param n
* @param m
* @return
*/
public int LastRemaining_Solution(int n, int m) {
ArrayList<Integer> arrayList=new ArrayList<>();
int index=-1;
for(int i=0;i<n;i++){
arrayList.add(i);
}
if(arrayList.isEmpty())
return -1;
while (arrayList.size()!=1){
for(int i=0;i<m;i++){
index=index+1;
if(index==arrayList.size())
index=0;
}
arrayList.remove(index);
index=index-1;
}
return arrayList.get(0);
}
}
class SolutionJZ47 {
/*
使用“递归”=for,“短路”=if
*/
public int Sum_Solution(int n) {
int sum=n;
boolean t=(n>0)&&((sum+=Sum_Solution(n--))>0);
return sum;
}
}
class SolutionJZ48 {
public int Add(int num1, int num2) {
int sum, carry;
do {
sum = num1 ^ num2;
carry = (num1 & num2) << 1;
num1 = sum;
num2 = carry;
}
while (num2 != 0);
return sum;
}
}
class SolutionJZ50 {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
// 返回任意重复的一个值,赋值duplication[0],这是题目要求
public boolean duplicate(int numbers[],int length,int [] duplication) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (numbers[j] == numbers[i]) {
duplication[0] = numbers[i];
return true;
}
}
}
return false;
}
}
class SolutionJZ51 {
/*
方法:
暴力求解
*/
public int[] multiply(int[] A) {
int len=A.length;
int[] b=new int[len];
int i;
for(i=0;i<len;i++){
b[i]=1;
}
for(i=1;i<len;i++){
b[0]=b[0]*A[i];
}
for(i=0;i<len-1;i++){
b[len-1]=b[len-1]*A[i];
}
for(i=1;i<len-1;i++){
for(int j=0;j<len;j++){
if(j==i)
continue;
b[i]=b[i]*A[j];
}
}
return b;
}
/*
方法:把数组B看成一个矩阵来创建
*/
}
class SolutionJZ55 {
public ListNode EntryNodeOfLoop(ListNode pHead) {
ListNode fast=pHead,slow=pHead;
while((fast!=null)&&(fast.next!=null)){
fast=fast.next.next;
slow=slow.next;
if(fast==slow)
break;
}
if(fast==null||fast.next==null)
return null;
fast=pHead;
while(fast!=slow){
fast=fast.next;
slow=slow.next;
}
return fast;
}
}
class SolutionJZ57 {
/*
根据中序遍历的规则,当结点存在右子树的时候,中序遍历的下一个结点为右子树的最左节点。
但是当节点不存在右子树的时候,中序遍历的下一个结点必定为该节点的父辈节点。但是究竟是
哪一辈呢?根据中序遍历特性,左父结点一定已经被中序遍历访问过,所以下一个结点一定是在
父节点路径上的第一个右父节点。
*/
public TreeLinkNode GetNext(TreeLinkNode pNode){
if(pNode == null) return null;
if(pNode.right != null){
pNode = pNode.right;
while(pNode.left != null);
pNode = pNode.left;
return pNode;
}
while(pNode.next != null){
if(pNode.next.left == pNode)
return pNode.next;
pNode = pNode.next;
}
return null;
}
}
class SolutionJZ59 {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result=new ArrayList<>();
TreeNode head=pRoot;
int level=0;
Queue<TreeNode> q=new LinkedList<>();
if(head!=null)
q.add(head);
while(!q.isEmpty()){
int sz=q.size();
TreeNode treeNode;
ArrayList<Integer> temp=new ArrayList<>();
while(sz!=0){
treeNode=q.peek();
q.poll();
temp.add(treeNode.val);
if(treeNode.left!=null)
q.add(treeNode.left);
if(treeNode.right!=null)
q.add(treeNode.right);
sz--;
}
if(level%2!=0)
Collections.reverse(temp);
result.add(temp);
level++;
}
return result;
}
}