Skip to content

Commit

Permalink
Merge pull request #1 from algorithm004-02/master
Browse files Browse the repository at this point in the history
同步数据
  • Loading branch information
pengjunzhen committed Dec 8, 2019
2 parents 4a32248 + 78fc533 commit e7518fa
Show file tree
Hide file tree
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.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 极客大学「算法训练营第四期 - 2 班」作业提交仓库

## 讲师课件下载地址

请大家通过该链接查看讲师课件并进行下载:链接:https://pan.baidu.com/s/1uoU_2toXHYw_eQOHMpogfQ 密码:94kd


## 仓库目录结构说明

Expand All @@ -17,11 +21,11 @@
5. <font color='red'> 务必按照Pull Request的备注形式和作业文件的命名进行提交,班主任会严格按照命名形式统计大家的作业。 </font>

### 学习总结的提交
1. 除代码作业外,我们要求学员每周提交一篇当周的学习感想,直接发一个独立的 issue 即可,issue 标题格式为`【学号-周】总结题目】`。例如`【089-week 03】二叉树的更多理解`是指学号`089`的学员提交的`第三周`的学习总结。可参考:https://github.com/algorithm004-class-02/algorithm004-02/issues/1
1. 除代码作业外,我们要求学员每周提交一篇当周的学习感想,直接发一个独立的 issue 即可,issue 标题格式为`【学号-周】总结题目】`。例如`【089-week 03】二叉树的更多理解`是指学号`089`的学员提交的`第三周`的学习总结。可参考:https://github.com/algorithm004-03/algorithm004-03/issues/1

### Review 与点评
1. 我们要求学员每周至少Review并点评其他5位同学的作业,点评直接回复在代码作业或学习总结下面都可。

## 注意事项
1. **作业公布地址:** 我们会在置顶的 issue 中公布当周的算法练习题以及其他注意事项。
2. 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。
2. 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。从来没用过github的学员看这里的git_quick_guide,或许会对你有帮助奥。https://github.com/algorithm004-01/algorithm004-01/tree/master/Utils
141 changes: 141 additions & 0 deletions Week 01/id_002/LeetCode_283_002.js
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;
}
3 changes: 0 additions & 3 deletions Week 01/id_002/NOTE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# NOTE



57 changes: 57 additions & 0 deletions Week 01/id_002/leetCode_141_002.js
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;
};
53 changes: 53 additions & 0 deletions Week 01/id_002/leetCode_142_002.js
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;
};
34 changes: 34 additions & 0 deletions Week 01/id_002/leetCode_206_002.js
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;
};
33 changes: 33 additions & 0 deletions Week 01/id_002/leetCode_24_002.js
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;
};

0 comments on commit e7518fa

Please sign in to comment.