Skip to content

Commit 30436b5

Browse files
author
wb-hjk570755
committed
链表
1 parent 3dae4db commit 30436b5

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/10/2
10+
*/
11+
public class HammingWeight {
12+
13+
public int hammingWeight(int n) {
14+
int res = 0;
15+
while(n != 0) {
16+
res += n & 1;
17+
n >>>= 1;
18+
}
19+
return res;
20+
21+
22+
}
23+
}

src/com/blankj/myself/HasCycle_fast_slow_point.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public boolean hasCycle(ListNode head) {
3131
return false;
3232
}
3333

34-
class ListNode {
34+
static class ListNode {
3535
int val;
3636
ListNode next;
37-
ListNode(int x) {
37+
public ListNode(int x) {
3838
val = x;
3939
next = null;
4040
}

src/com/blankj/myself/KthLargest.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.blankj.myself;
2+
3+
import com.blankj.myself.InorderTraversal.TreeNode;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/2
12+
*/
13+
public class KthLargest {
14+
15+
int resp;
16+
int index;
17+
public int kthLargest(TreeNode root, int k) {
18+
index = k;
19+
dfs(root);
20+
return resp;
21+
}
22+
23+
public void dfs(TreeNode root){
24+
if(root==null){
25+
return ;
26+
}
27+
dfs(root.right);
28+
29+
if(--index==0){
30+
resp = root.val;
31+
}
32+
dfs(root.left);
33+
}
34+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.blankj.myself;
2+
3+
import java.util.Stack;
4+
5+
import com.blankj.myself.HasCycle_fast_slow_point.ListNode;
6+
7+
/**
8+
* Description:
9+
* Copyright: Copyright (c) 2012
10+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
11+
*
12+
* @author huangjk
13+
* @version 1.0 2020/10/2
14+
*/
15+
public class ReverseList {
16+
17+
public ListNode reverseList(ListNode head) {
18+
Stack<Integer> stack = new Stack();
19+
while(head!=null){
20+
stack.push(head.val);
21+
head = head.next;
22+
}
23+
ListNode listNode = new ListNode(stack.pop().intValue());
24+
while(!stack.isEmpty()){
25+
listNode.next = new ListNode(stack.pop().intValue());
26+
}
27+
return listNode;
28+
}
29+
30+
}

src/com/blankj/myself/Rotate.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/10/2
10+
*/
11+
public class Rotate {
12+
13+
public static void main(String[] args) {
14+
int[] a = {1,2,3,4,5,6,7};
15+
Rotate rotate = new Rotate();
16+
rotate.rotate(a,3);
17+
}
18+
19+
public void rotate(int[] nums, int k) {
20+
21+
for(int i=0;i<k;i++){
22+
int temp = nums[nums.length-1];
23+
for (int j=nums.length-1;j>=0;j--){
24+
if(j==0){
25+
nums[j] = temp;
26+
}else{
27+
nums[j] = nums[j-1];
28+
}
29+
}
30+
}
31+
//int[] temps = new int[k];
32+
//for(int i=nums.length-k;i<=nums.length-1;i++){
33+
// temps[i+k-nums.length]=nums[i];
34+
//}
35+
//for(int j=nums.length-1;j>=0;j--){
36+
// if(j>=k){
37+
// nums[j]=nums[j-k];
38+
// }else{
39+
// nums[j]=temps[j];
40+
// }
41+
//}
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)