Skip to content

Commit 5bd74c2

Browse files
committed
rename for java
1 parent 40cab32 commit 5bd74c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2699
-0
lines changed

src/main/java/IQ0201.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.HashSet;
2+
3+
/**
4+
* 面试题:02.01 移除重复节点
5+
* Link:https://leetcode-cn.com/problems/remove-duplicate-node-lcci/
6+
* 思路:哈希排重复
7+
*/
8+
public class IQ0201 {
9+
10+
static class ListNode {
11+
int val;
12+
ListNode next;
13+
ListNode(int x) {
14+
val = x;
15+
next = null;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "ListNode{" +
21+
"val=" + val +
22+
", next=" + next +
23+
'}';
24+
}
25+
}
26+
27+
public static ListNode removeDuplicateNodes(ListNode head) {
28+
if (head == null) {
29+
return null;
30+
}
31+
HashSet<Integer> hashSet = new HashSet<>();
32+
hashSet.add(head.val);
33+
ListNode moveNode = head;
34+
while (moveNode.next != null) {
35+
ListNode cur = moveNode.next;
36+
if (hashSet.add(cur.val)) {
37+
moveNode = moveNode.next;
38+
} else {
39+
moveNode.next = moveNode.next.next;
40+
}
41+
}
42+
moveNode.next = null;
43+
return head;
44+
}
45+
46+
public static void main(String[] args) {
47+
ListNode n1 = new ListNode(1);
48+
ListNode n2 = new ListNode(2);
49+
ListNode n3 = new ListNode(3);
50+
ListNode n4 = new ListNode(2);
51+
ListNode n5 = new ListNode(1);
52+
n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5;
53+
54+
ListNode listNode = removeDuplicateNodes(n1);
55+
System.out.println(listNode);
56+
}
57+
}

src/main/java/IQ1001.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* LC#IQ10.01:合并排序数组
5+
* Link: https://leetcode-cn.com/problems/sorted-merge-lcci/
6+
* 思路1:合并后排序 (效率低)
7+
* 思路2:使用 k 从尾部开始往头插入元素(最优)
8+
*/
9+
public class IQ1001 {
10+
11+
public static void merge(int[] A, int m, int[] B, int n) {
12+
int k = m + n - 1;
13+
int i = m - 1;
14+
int j = n - 1;
15+
while (i >= 0 && j >= 0) {
16+
if (A[i] < B[j]) {
17+
A[k--] = B[j--];
18+
} else {
19+
A[k--] = A[i--];
20+
}
21+
}
22+
while (j >= 0) { A[k--] = B[j--]; }
23+
}
24+
25+
public static void main(String[] args) {
26+
// int[] nums1 = {1,2,3,0,0,0};
27+
// int[] nums2 = {2,5,6};
28+
int[] nums1 = {0};
29+
int[] nums2 = {1};
30+
merge(nums1, 0, nums2, 1);
31+
System.out.println(Arrays.toString(nums1));
32+
}
33+
}

src/main/java/PO18.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 剑指 Offer 18:删除链表的节点
3+
* Link:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
4+
* 这道题跟 LC#237 极度相似,但是相对复杂很多,这是这里 head 参数是头节点, LC#237 传入参数为当前节点
5+
* 思路:首选判断头指针为当前节点的情况,如果是头指针,处理起来就很方便
6+
* 接下来通过遍历链表,找到目标节点的上一个节点,既 currentPrevNode 引用
7+
* 处理 currentPrevNode 引用会出现两种情况:
8+
* 1 存在 next.next 节点,需要将 next 节点的更新为 next.next 节点,完成删除操作
9+
* 2 目标节点为最末尾的节点,要删除节点,为末尾节点,直接将当前引用的 next 引用删除即可 currentPrevNode.next = null
10+
*/
11+
public class PO18 {
12+
13+
static class ListNode {
14+
int val;
15+
ListNode next;
16+
ListNode(int x) {
17+
val = x;
18+
next = null;
19+
}
20+
}
21+
22+
public static ListNode deleteNode(ListNode head, int val) {
23+
if (head.val == val) {
24+
return head.next;
25+
}
26+
ListNode currentPrevNode = null;
27+
ListNode prev = head;
28+
while (prev.next != null) {
29+
if (prev.next.val == val) {
30+
currentPrevNode = prev;
31+
break;
32+
}
33+
prev = prev.next;
34+
}
35+
ListNode nextNode = currentPrevNode.next;
36+
if (nextNode.next == null) {
37+
currentPrevNode.next = null;
38+
} else {
39+
nextNode.val = nextNode.next.val;
40+
nextNode.next = nextNode.next.next;
41+
}
42+
return head;
43+
}
44+
45+
public static void main(String[] args) {
46+
ListNode n1 = new ListNode(1);
47+
ListNode n2 = new ListNode(2);
48+
ListNode n3 = new ListNode(3);
49+
n1.next = n2; n2.next = n3;
50+
51+
ListNode listNode = PO18.deleteNode(n1, 2);
52+
System.out.println(listNode);
53+
}
54+
}

src/main/java/PO22.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 剑指 Offer 22. 链表中倒数第k个节点
3+
* Link:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
4+
* 思路1(简单):先计算链表长度,算出 length - k 得出起始位置,直接返回 length - k 位置即可
5+
* 思路2:使用 former 指针首先向前走 k 步,然后同时移动 former, latter 指针,当 former 为 null 时返回 latter 指针即可
6+
*/
7+
public class PO22 {
8+
9+
static class ListNode {
10+
int val;
11+
ListNode next;
12+
ListNode(int x) {
13+
val = x;
14+
next = null;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "ListNode{" +
20+
"val=" + val +
21+
", next=" + next +
22+
'}';
23+
}
24+
}
25+
26+
public static ListNode getKthFromEnd(ListNode head, int k) {
27+
ListNode former = head;
28+
ListNode latter = head;
29+
for(int i = 0; i < k; i++) {
30+
former = former.next;
31+
}
32+
while(former != null) {
33+
former = former.next;
34+
latter = latter.next;
35+
}
36+
return latter;
37+
}
38+
39+
public static void main(String[] args) {
40+
ListNode n1 = new ListNode(1);
41+
ListNode n2 = new ListNode(2);
42+
ListNode n3 = new ListNode(3);
43+
ListNode n4 = new ListNode(4);
44+
ListNode n5 = new ListNode(5);
45+
n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5;
46+
47+
ListNode listNode = getKthFromEnd(n1, 2);
48+
System.out.println(listNode);
49+
}
50+
51+
}

src/main/java/PO3.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/**
5+
* LC#OF03: 数组中重复的数字
6+
* Link:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
7+
* 思路:哈希排重
8+
*/
9+
public class PO3 {
10+
11+
public static int findRepeatNumber(int[] nums) {
12+
Set<Integer> hashSet = new HashSet<>(nums.length);
13+
for (int num : nums) {
14+
if (hashSet.contains(num)) return num;
15+
hashSet.add(num);
16+
}
17+
return -1;
18+
}
19+
20+
public static void main(String[] args) {
21+
int[] nums = {2, 3, 1, 0, 2, 5, 3};
22+
int res = findRepeatNumber(nums);
23+
System.out.println(res);
24+
}
25+
}

src/main/java/PO52.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 剑指 Offer 52. 两个链表的第一个公共节点
3+
* Link:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
4+
* 思路1 相交链表:
5+
*/
6+
public class PO52 {
7+
8+
static class ListNode {
9+
int val;
10+
ListNode next;
11+
ListNode(int x) {
12+
val = x;
13+
next = null;
14+
}
15+
}
16+
17+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
18+
ListNode pA = headA;
19+
ListNode pB = headB;
20+
21+
while (pA != pB) {
22+
pA = pA == null ? headB : pA.next;
23+
pB = pB == null ? headA : pB.next;
24+
}
25+
return pA;
26+
}
27+
28+
}

src/main/java/PO6.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* 剑指 Offer 06:从尾到头打印链表
5+
* Link:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
6+
* 思路(最优解):1次遍历统计链表长度,倒序遍历将链表元素插入数组
7+
*/
8+
public class PO6 {
9+
10+
static class ListNode {
11+
int val;
12+
ListNode next;
13+
ListNode(int x) {
14+
val = x;
15+
next = null;
16+
}
17+
}
18+
19+
public static int[] reversePrint(ListNode head) {
20+
int count = 0;
21+
ListNode prev = head;
22+
while (prev != null) {
23+
count++;
24+
prev = prev.next;
25+
}
26+
27+
int[] result = new int[count];
28+
while (count != 0) {
29+
result[count - 1] = head.val;
30+
head = head.next;
31+
count--;
32+
}
33+
return result;
34+
}
35+
36+
public static void main(String[] args) {
37+
ListNode n1 = new ListNode(1);
38+
ListNode n2 = new ListNode(3);
39+
ListNode n3 = new ListNode(2);
40+
41+
n1.next = n2; n2.next = n3;
42+
int[] ints = PO6.reversePrint(n1);
43+
System.out.println(Arrays.toString(ints));
44+
}
45+
}

src/main/java/S1.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
/**
6+
* LC#1:Two Sum 两数之和
7+
* https://leetcode-cn.com/problems/two-sum/
8+
* 思路1:2次哈希求解:将数组放入散列表,遍历散列表,通过target-key得出匹配数组,并且判断非自身元素,返回数组,时间复杂度 O(n)
9+
* 思路2[runtime beats(最优解)]:1次哈希求解:在遍历 num 数组,如果哈希表不包含 target - num[i] ,则将 num[i], i 放入哈希表,如果包含则直接返回 [hash[num[i], i]] 下标
10+
*/
11+
public class S1 {
12+
13+
public static int[] twoSum(int[] nums, int target) {
14+
Map<Integer, Integer> hashMap = new HashMap<>();
15+
for (int i = 0; i < nums.length; i++) {
16+
// 如果包含目标值
17+
if(hashMap.containsKey(target - nums[i])) {
18+
return new int[]{hashMap.get(target - nums[i]), i};
19+
}
20+
hashMap.put(nums[i], i);
21+
}
22+
return null;
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] nums = {2, 7, 11, 15};
27+
int target = 9;
28+
int[] result = S1.twoSum(nums, target);
29+
System.out.println(Arrays.toString(result));
30+
31+
}
32+
}

src/main/java/S100.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* #100. Same Tree
3+
* https://leetcode-cn.com/problems/same-tree/
4+
*/
5+
public class S100 {
6+
7+
public static boolean isSameTree(TreeNode p, TreeNode q) {
8+
if(p == null && q == null) {
9+
return true;
10+
}
11+
12+
if(p != null && q != null && p.val == q.val) {
13+
//递归遍历左子树和右子树
14+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
15+
} else {
16+
return false;
17+
}
18+
}
19+
20+
public static class TreeNode {
21+
int val;
22+
TreeNode left;
23+
TreeNode right;
24+
public TreeNode(int val) {
25+
this.val = val;
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
TreeNode n1 = new TreeNode(1);
31+
n1.left = new TreeNode(2);
32+
TreeNode n2 = new TreeNode(1);
33+
n2.left = null;
34+
n2.right = new TreeNode(2);
35+
System.out.println("isTrue > " + isSameTree(n1, n2));
36+
37+
}
38+
}

0 commit comments

Comments
 (0)