-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from algorithm004-02/master
同步数据
- Loading branch information
Showing
3,625 changed files
with
163,400 additions
and
182 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** 双指针 posi 记录非零元素要插入位置 有三种解法 **/ | ||
|
||
/** | ||
* 第一种解法 置零 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes1 = function(nums) { | ||
let posi = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] !== 0) { | ||
nums[posi] = nums[i]; | ||
if (i != posi) { | ||
nums[i] = 0 | ||
} | ||
|
||
posi++; | ||
} | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** | ||
* 第二种解法 后置 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes2 = function(nums) { | ||
let posi = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] !== 0) { | ||
nums[posi] = nums[i]; | ||
posi++; | ||
} | ||
} | ||
|
||
for (let j = posi; j < nums.length; j++) { | ||
nums[j] = 0; | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** | ||
* 第三种解法 互换 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes3 = function(nums) { | ||
let posi = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] !== 0) { | ||
[nums[posi], nums[i]] = [nums[i], nums[posi]]; | ||
posi++; | ||
} | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** 统计为0数量count 通过count找出非零元素的位置 **/ | ||
/** | ||
* 第一种解法 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes4 = function(nums) { | ||
let count = 0; | ||
for (let i = 0; i < nums.length; i ++) { | ||
if (nums[i] === 0) { | ||
count++; | ||
} else if (count > 0) { | ||
nums[i - count] = nums[i]; | ||
nums[i] = 0; | ||
} | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** | ||
* 第二种解法 后置 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes5 = function(nums) { | ||
let count = 0; | ||
for (let i = 0; i < nums.length; i++ ) { | ||
if (nums[i] === 0) { | ||
count ++; | ||
} else if (count > 0) { | ||
nums[i - count] = nums[i]; | ||
} | ||
} | ||
|
||
for (let j = nums.length - count; j < nums.length; j ++) { | ||
nums[j] = 0; | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** | ||
* 第三种解法 互换 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes6 = function(nums) { | ||
let count = 0; | ||
for (let i = 0; i < nums.length; i ++ ) { | ||
if (nums[i] === 0) { | ||
count ++; | ||
} else if (count > 0) { | ||
[nums[i - count], nums[i]] = [nums[i], nums[i - count]]; | ||
} | ||
} | ||
|
||
return nums; | ||
} | ||
|
||
/** 利用数组的插入删除操作完成 如果语言数组内存结构空间连续的 时间复杂度将会O(n^2) **/ | ||
/** | ||
* 第一种解法 | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
const moveZeroes7 = function(nums) { | ||
let count = 0; | ||
for (let i = 0; i < nums.length - count; i ++) { | ||
if (nums[i] === 0) { | ||
nums.push(nums[i]); | ||
nums.splice(i, 1); | ||
|
||
i --; | ||
count ++; | ||
} | ||
} | ||
|
||
return nums; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
# NOTE | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** 环形列表 三种解法:1. 暴力解法:500ms; 2. Set数据结构判断, 3.快慢指针 */ | ||
/** | ||
* 暴力解法: 500ms | ||
* @param {Boolean} hasC | ||
*/ | ||
var hasCycle = function(head) { | ||
let hasC = false; | ||
let start = Date.now(); | ||
let cur = head; | ||
while(cur) { | ||
if (Date.now() - start > 500) { | ||
hasC = true; | ||
break; | ||
} | ||
cur = cur.next; | ||
} | ||
|
||
return hasC; | ||
}; | ||
|
||
/** | ||
* Set数据结构判断 | ||
* @param {Boolean} | ||
*/ | ||
var hasCycle2 = function(head) { | ||
let set = new Set(); | ||
let cur = head; | ||
while(cur) { | ||
if (!set.has(cur)) { | ||
set.add(cur); | ||
} else { | ||
return true; | ||
} | ||
|
||
cur = cur.next; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
/** | ||
* 快慢指针 | ||
* @param {Boolean} | ||
*/ | ||
var hasCycle3 = function(head) { | ||
let slow = head; | ||
let fast = head; | ||
|
||
while(slow && fast && fast.next) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
|
||
if (slow === fast) return true; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** 环形链表 II */ | ||
|
||
/** | ||
* 第一种解法 通过Set数据结构判断, 当第一个重复存在的节点为环境的入口节点 | ||
* 复杂度分析 时间复杂度O(n) 空间复杂度O(n) | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var detectCycle = function(head) { | ||
let cur = head; | ||
let set = new Set(); | ||
while(cur) { | ||
if (set.has(cur)) return cur; | ||
set.add(cur); | ||
cur = cur.next; | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* 第二种办法 快慢指针 | ||
* 时间复杂度O(n) 空间复杂度O(1) | ||
* 数学证明 2(x+n1*c+y)-x+n1*c+y=x+n1*c+y=n2*c,x+y=(n2-n1)*c | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var detectCycle2 = function(head) { | ||
let slow = head; | ||
let fast = head; | ||
let intersect = null; | ||
|
||
while(slow && fast && fast.next) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
|
||
if (fast === slow) { | ||
intersect = fast; | ||
break; | ||
} | ||
} | ||
|
||
if (intersect === null) return null; | ||
|
||
let pr1 = head; | ||
let pr2 = intersect; | ||
|
||
while(pr1 !== pr2) { | ||
pr1 = pr1.next; | ||
pr2 = pr2.next; | ||
} | ||
|
||
return pr1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** 第一种利用递归方法 */ | ||
/** | ||
* 核心原理 利用当前函数调用栈中存储存储值 进行翻转。 | ||
* 时间复杂度为O(n), 空间复杂度O(n), 因链表长度为n, 需要n函数栈存储空间 | ||
* @param {*} head | ||
*/ | ||
var reverseList = function(head) { | ||
if (head === null || head.next === null) return head; | ||
let p = reverseList(head.next); | ||
head.next.next = head; | ||
head.next = null; | ||
return p; | ||
}; | ||
|
||
/** | ||
* 第二种解法 时间复杂度O(n) 空间复杂度为O(1) | ||
* @param {*} head | ||
*/ | ||
var reverseList2 = function(head) { | ||
if (head === null) return head; | ||
|
||
let prev = null; | ||
let cur = head; | ||
|
||
while (true) { | ||
let next = cur.next; | ||
cur.next = prev; | ||
if (next === null) break; | ||
|
||
[prev, cur] = [cur, next]; | ||
} | ||
|
||
return cur; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** 递归方式进行调换 */ | ||
|
||
/** | ||
* 时间复杂度O(n) 空间复杂度O(n) | ||
* @param {*} head | ||
*/ | ||
var swapPairs = function(head) { | ||
if (head === null || head.next === null) return head; | ||
|
||
let next = head.next; | ||
head.next = swapPairs(next.next); | ||
next.next = head; | ||
|
||
return next; | ||
}; | ||
/** | ||
* 时间复杂度O(n) 空间复杂度O(1) | ||
*/ | ||
var swapPairs2 = function(head) { | ||
let self = {next: head}; | ||
let prev = self; | ||
|
||
while(prev.next && prev.next.next) { | ||
let a = prev.next; | ||
let b = prev.next.next; | ||
|
||
[prev.next, b.next, a.next] = [b, a, b.next]; | ||
|
||
prev = a; | ||
} | ||
|
||
return self.next; | ||
}; |
Oops, something went wrong.