Skip to content

Commit 5615a99

Browse files
committed
作业
1 parent 9ca0f88 commit 5615a99

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

Week01/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 第一周学习笔记
22
## 时间复杂度
3-
![时间复杂度](./assets/images/complexity.png)
3+
![时间复杂度](https://github.com/Simona0769/algorithm010/blob/master/Week01/assets/images/complexity.png)
44
## 数组、链表、跳表的基本实现和特性
55
### Array 数组
66
* 实现:申请数组,在内存地址中申请一段连续的地址。每个地址都可以通过内存管理器直接访问
@@ -11,7 +11,7 @@
1111
### Linked List 链表
1212
* 实现
1313

14-
![Linked List](./assets/images/LinkedList.jpg)
14+
![Linked List](https://github.com/Simona0769/algorithm010/blob/master/Week01/assets/images/LinkedList.jpg)
1515

1616
```
1717
// js 单向链表实现

Week01/programming/3sum.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function(nums) {
6+
var result = []
7+
nums.sort(function(a, b) {
8+
return a - b ;
9+
})
10+
for(var k = 0; k < nums.length - 2; k ++) {
11+
if (nums[k] > 0) {
12+
break;
13+
}
14+
if (k > 0 && nums[k] === nums[k - 1]) continue;
15+
var i = k + 1, j = nums.length - 1;
16+
while (i < j) {
17+
var _result = nums[k] + nums[i] + nums[j];
18+
if (_result < 0) {
19+
while (i < j && nums[i] === nums[++i]);
20+
} else if (_result > 0) {
21+
while(i < j && nums[j] === nums[--j]);
22+
} else {
23+
result.push([nums[k], nums[i], nums[j]]);
24+
while (i < j && nums[i] === nums[++i]);
25+
while(i < j && nums[j] === nums[--j]);
26+
}
27+
}
28+
}
29+
return result
30+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* 我第一次的写法
10+
* @param {ListNode} l1
11+
* @param {ListNode} l2
12+
* @return {ListNode}
13+
*/
14+
var mergeTwoLists = function(l1, l2) {
15+
var currNode1 = l1;
16+
var currNode2 = l2;
17+
var l3 = new ListNode(0);
18+
var currNode3 = l3;
19+
while(currNode1 !== null && currNode2 !== null) {
20+
if (currNode1.val <= currNode2.val) {
21+
currNode3.next = new ListNode(currNode1.val);
22+
currNode1 = currNode1.next;
23+
} else {
24+
currNode3.next = new ListNode(currNode2.val);
25+
currNode2 = currNode2.next;
26+
}
27+
currNode3 = currNode3.next;
28+
}
29+
if (currNode1 !== null) {
30+
currNode3.next = new ListNode(currNode1.val);
31+
currNode3.next.next = currNode1.next;
32+
} else if (currNode2 !== null) {
33+
currNode3.next = new ListNode(currNode2.val);
34+
currNode3.next.next = currNode2.next;
35+
}
36+
return l3.next;
37+
};
38+
39+
/**
40+
* Definition for singly-linked list.
41+
* function ListNode(val, next) {
42+
* this.val = (val===undefined ? 0 : val)
43+
* this.next = (next===undefined ? null : next)
44+
* }
45+
*/
46+
/**
47+
* 官方
48+
* @param {ListNode} l1
49+
* @param {ListNode} l2
50+
* @return {ListNode}
51+
*/
52+
var mergeTwoLists1 = function(l1, l2) {
53+
if(l1 === null) {
54+
return l2;
55+
} else if (l2 === null) {
56+
return l1;
57+
} else if (l1.val < l2.val) {
58+
l1.next = mergeTwoLists1(l1.next, l2);
59+
return l1;
60+
} else {
61+
l2.next = mergeTwoLists1(l1, l2.next);
62+
return l2;
63+
}
64+
};

0 commit comments

Comments
 (0)