File tree 4 files changed +141
-0
lines changed
4 files changed +141
-0
lines changed Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 138. Copy List with Random Pointer
4
+ * 题意:链表除了next指针外,还有一个random指针。拷贝这个链表
5
+ * 难度:Medium
6
+ * 分类:Hash Table, LinkedList
7
+ * 思路:一种用HashTable,把源节点和复制的节点对应起来,空间复杂度为O(n)
8
+ * 把要复制的节点直接连接在该节点之后,操作完以后再分开
9
+ * Tips:和139做对比,因为需要每步骤返回值,所以递归的方法在这题更合适
10
+ */
11
+ public class lc138 {
12
+ class RandomListNode {
13
+ int label ;
14
+ RandomListNode next , random ;
15
+ RandomListNode (int x ) { this .label = x ; }
16
+ };
17
+ public RandomListNode copyRandomList (RandomListNode head ) {
18
+ if (head ==null ) return null ;
19
+ RandomListNode node = head ;
20
+ while (node !=null ){
21
+ RandomListNode temp = new RandomListNode (node .label );
22
+ temp .next = node .next ;
23
+ node .next = temp ;
24
+ node = temp .next ;
25
+ }
26
+ node = head ;
27
+ while (node !=null ){
28
+ if (node .random !=null ) node .next .random = node .random .next ; //注意判断是否为空
29
+ node = node .next .next ;
30
+ }
31
+ node = head ;
32
+ RandomListNode res = head .next ;
33
+ RandomListNode temp = head .next ;
34
+ while (temp .next !=null ){
35
+ node .next = temp .next ;
36
+ temp .next = node .next .next ;
37
+ node = node .next ;
38
+ temp = temp .next ;
39
+ }
40
+ node .next = null ; //别忘了最后一个节点连接给null
41
+ return res ;
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ package code ;
2
+
3
+ import java .util .HashMap ;
4
+
5
+ /*
6
+ * 149. Max Points on a Line
7
+ * 题意:给定多个点,问一条线上最多几个点
8
+ * 难度:Hard
9
+ * 分类:HashTable, Math
10
+ * 思路:每次选出一个点,与其他点匹配。因为斜率是小数,所以用两个整数(双层HashMap)来表示小数。时间O(n^2)
11
+ * Tips:
12
+ */
13
+ public class lc149 {
14
+ class Point {
15
+ int x ;
16
+ int y ;
17
+ Point () { x = 0 ; y = 0 ; }
18
+ Point (int a , int b ) { x = a ; y = b ; }
19
+ }
20
+ public int maxPoints (Point [] points ) {
21
+ if (points .length <=2 ) return points .length ;
22
+ int res = 0 ;
23
+ for (int i = 0 ; i < points .length -1 ; i ++) {
24
+ HashMap <Integer , HashMap <Integer , Integer >> hm = new HashMap <>();
25
+ int overlap = 1 ;
26
+ int max = 0 ;
27
+ for (int j = i +1 ; j < points .length ; j ++) {
28
+ int x = points [i ].x - points [j ].x ;
29
+ int y = points [i ].y - points [j ].y ;
30
+ if (x ==0 &&y ==0 ){ //重复点
31
+ overlap ++;
32
+ continue ;
33
+ }
34
+ int gcd = genGCD (x , y );
35
+ if (gcd !=0 ){ //注意,当一个值为0时,gcd==0
36
+ x = x /gcd ;
37
+ y = y /gcd ;
38
+ }
39
+ HashMap <Integer , Integer > hm2 = hm .getOrDefault (x , new HashMap <Integer , Integer >());
40
+ hm2 .put (y ,hm2 .getOrDefault (y ,0 )+1 );
41
+ hm .put (x ,hm2 );
42
+ max = Math .max (max , hm2 .get (y ));
43
+ }
44
+ res = Math .max (res , max +overlap ); //里边循环完了,才知道有几个overlap,所以在循环外加
45
+ }
46
+ return res ;
47
+ }
48
+
49
+ public int genGCD (int a , int b ){
50
+ if (b ==0 ) return a ; //注意和GCD不一样的是这没有比较大小,交换a,b。因为可能有负数
51
+ return genGCD (b , a %b );
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 150. Evaluate Reverse Polish Notation
4
+ * 题意:波兰表达式运算
5
+ * 难度:Medium
6
+ * 分类:Stack
7
+ * 思路:用栈即可
8
+ * Tips:
9
+ */
10
+ import java .util .Stack ;
11
+
12
+ public class lc150 {
13
+ public int evalRPN (String [] tokens ) {
14
+ if (tokens .length ==1 ) return Integer .valueOf (tokens [0 ]);
15
+ Stack <Integer > st = new Stack ();
16
+ String opration = "+-*/" ;
17
+ int res = 0 ;
18
+ for (int i = 0 ; i < tokens .length ; i ++) {
19
+ if (opration .contains (tokens [i ])){
20
+ int n2 = st .pop (); //注意n1,n2顺序,除法时两个数是有顺序的
21
+ int n1 = st .pop ();
22
+ System .out .println (n1 );
23
+ System .out .println (n2 );
24
+ if (tokens [i ].equals ("+" )){ //注意用equeals
25
+ res = n1 + n2 ;
26
+ }else if (tokens [i ].equals ("-" )){
27
+ res = n1 - n2 ;
28
+ }else if (tokens [i ].equals ("*" )){
29
+ res = n1 * n2 ;
30
+ }else if (tokens [i ].equals ("/" )){
31
+ res = n1 / n2 ;
32
+ }
33
+ st .push (res );
34
+ System .out .println (res );
35
+ }else {
36
+ st .push (Integer .valueOf (tokens [i ]));;
37
+ }
38
+ }
39
+ return res ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change @@ -92,13 +92,17 @@ Language: Java
92
92
| 131 [ Java] ( ./code/lc131.java )
93
93
| 134 [ Java] ( ./code/lc134.java )
94
94
| 136 [ Java] ( ./code/lc136.java )
95
+ | 138 [ Java] ( ./code/lc138.java )
95
96
| 139 [ Java] ( ./code/lc139.java )
97
+ | 140 [ Java] ( ./code/lc140.java )
96
98
| 141 [ Java] ( ./code/lc141.java )
97
99
| 142 [ Java] ( ./code/lc142.java )
98
100
| 144 [ Java] ( ./code/lc144.java )
99
101
| 145 [ Java] ( ./code/lc145.java )
100
102
| 146 [ Java] ( ./code/lc146.java )
101
103
| 148 [ Java] ( ./code/lc148.java )
104
+ | 149 [ Java] ( ./code/lc149.java )
105
+ | 150 [ Java] ( ./code/lc150.java )
102
106
| 152 [ Java] ( ./code/lc152.java )
103
107
| 155 [ Java] ( ./code/lc155.java )
104
108
| 160 [ Java] ( ./code/lc160.java )
You can’t perform that action at this time.
0 commit comments