Skip to content

Commit 2c67b91

Browse files
committed
算法题添加
1 parent 48a8bef commit 2c67b91

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
https://leetcode.com/problems/merge-sorted-array/
2+
3+
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
4+
5+
Note:
6+
7+
* The number of elements initialized in nums1 and nums2 are m and n respectively.
8+
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
9+
Example:
10+
```
11+
Input:
12+
nums1 = [1,2,3,0,0,0], m = 3
13+
nums2 = [2,5,6], n = 3
14+
15+
Output: [1,2,2,3,5,6]
16+
```
17+
18+
归并排序的合并操作
19+
20+
```java
21+
22+
class Solution {
23+
public void merge(int A[], int m, int B[], int n) {
24+
int i=m-1;
25+
int j=n-1;
26+
int k = m+n-1;
27+
while(i >=0 && j>=0)
28+
{
29+
if(A[i] > B[j])
30+
A[k--] = A[i--];
31+
else
32+
A[k--] = B[j--];
33+
}
34+
while(j>=0)
35+
A[k--] = B[j--];
36+
}
37+
}
38+
```

MD/在线编程-第k大个数.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
https://leetcode.com/problems/kth-largest-element-in-an-array/
2+
3+
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
4+
5+
Note:
6+
7+
* The number of elements initialized in nums1 and nums2 are m and n respectively.
8+
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
9+
Example:
10+
```
11+
Input:
12+
nums1 = [1,2,3,0,0,0], m = 3
13+
nums2 = [2,5,6], n = 3
14+
15+
Output: [1,2,2,3,5,6]
16+
```
17+
18+
快排的每一次排序就是找到一个第N大的数,修改快排的递归逻辑。算法复杂度为O(N)。
19+
20+
```java
21+
class Solution {
22+
public int findKthLargest(int[] nums, int k) {
23+
return sort(nums, 0, nums.length-1, k);
24+
}
25+
26+
private int sort(int[] nums, int left, int right, int k){
27+
int base = nums[left];
28+
int leftt = left;
29+
int rightt = right;
30+
while(leftt < rightt){
31+
while(leftt < rightt && nums[rightt] > base){
32+
rightt--;
33+
}
34+
if(leftt < rightt){
35+
int temp = nums[rightt];
36+
nums[rightt] = nums[leftt];
37+
nums[leftt] = temp;
38+
leftt++;
39+
}
40+
while(leftt < rightt && nums[leftt] < base){
41+
leftt++;
42+
}
43+
if(leftt < rightt){
44+
int temp = nums[rightt];
45+
nums[rightt] = nums[leftt];
46+
nums[leftt] = temp;
47+
rightt--;
48+
}
49+
}
50+
int rank = nums.length - leftt;
51+
if(rank == k){
52+
return nums[leftt];
53+
}else if(rank<k){
54+
return sort(nums, left, leftt-1, k);
55+
}else{
56+
return sort(nums, leftt+1, right, k);
57+
}
58+
}
59+
}
60+
```

MD/在线编程-链表相加.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
https://leetcode.com/problems/add-two-numbers/
2+
3+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
4+
5+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
7+
Example:
8+
```
9+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
10+
Output: 7 -> 0 -> 8
11+
Explanation: 342 + 465 = 807.
12+
```
13+
14+
对于这个问题还有一个很好的方法:
15+
16+
1、将两个链表逆序,这样就可以依次得到从低到高位的数字
17+
18+
2、同步遍历两个逆序后链表,相加生成新链表,同时关注进位
19+
20+
3、当两个链表都遍历完成后,关注进位。
21+
22+
4、将两个逆序的链表再逆序一遍,调整回去
23+
24+
25+
```java
26+
public class ListNode {
27+
int val;
28+
ListNode next;
29+
ListNode(int x) { val = x; }
30+
}
31+
32+
public class Solution {
33+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
34+
ListNode listNode= new ListNode(0);
35+
ListNode p = listNode;
36+
int sum = 0;
37+
38+
while (l1 != null || l2 != null || sum != 0) {
39+
if (l1 != null) {
40+
sum += l1.val;
41+
l1 = l1.next;
42+
}
43+
if (l2 != null) {
44+
sum += l2.val;
45+
l2 = l2.next;
46+
}
47+
p.next = new ListNode(sum % 10);
48+
sum = sum / 10;
49+
p = p.next;
50+
}
51+
return listNode.next;
52+
}
53+
}
54+
```

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ PS:除开知识点,一定要准备好以下套路:
4545
* [服务容错保护](http://sjyuan.cc/service-fault-tolerant-protected-with-hytrix/)
4646
### 在线编程
4747
* [二叉树反转](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-二叉树反转.md)
48+
* [链表相加](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-链表相加.md)
4849
* [LRU淘汰算法](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-LRU淘汰算法.md)
49-
* [连续子数组最大和](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-连续子数组最大和.md)
50+
* [连续子数组最大和](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-合并有序数组.md)
51+
* [合并有序数组](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-连续子数组最大和.md)
5052
* [快速排序](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-快速排序.md)
53+
* [第k大个数](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-第k大个数.md)
5154
* [交替打印奇偶数](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-交替打印奇偶数.md)
5255
* [排序打印关键字](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程-排序打印关键字.md)
5356
### Linux

0 commit comments

Comments
 (0)