Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6655211

Browse files
solution IQ1001 and PO3 by java
1 parent 78c46fb commit 6655211

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
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 InterviewQuestions1001 {
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/PointerOffer3.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 PointerOffer3 {
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /