diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..26fac643 Binary files /dev/null and b/.DS_Store differ diff --git a/Week_01/.DS_Store b/Week_01/.DS_Store new file mode 100644 index 00000000..802dbbbc Binary files /dev/null and b/Week_01/.DS_Store differ diff --git "a/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.js" "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.js" new file mode 100644 index 00000000..fb1c2071 --- /dev/null +++ "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.js" @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=1 lang=javascript + * + * [1] 两数之和 + */ + +// @lc code=start +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + for (var i = 0; i < nums.length; i++) { + var dif = target - nums[i]; + // j = i + 1 的目的是减少重复计算和避免两个元素下标相同 + for (var j = i + 1; j < nums.length; j++) { + if(nums[j] == dif) + return [i,j]; + } + } +}; +// @lc code=end + + +var ppp = twoSum([11,2,7], 9) +console.log(ppp) diff --git "a/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214_1.js" "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214_1.js" new file mode 100644 index 00000000..a41318e5 --- /dev/null +++ "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214_1.js" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=1 lang=javascript + * + * [1] 两数之和 + */ + +// @lc code=start +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + let map = new Map(); + for (let i = 0; i < nums.length; i++) { + if (map.has(target - nums[i])) { + return [map.get(target - nums[i]), i] + } else { + map.set(nums[i], i) + } + } + return [] +}; +// @lc code=end + + +var ppp = twoSum([11, 2, 7, 6], 9) +console.log(ppp) diff --git "a/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.java" "b/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.java" new file mode 100644 index 00000000..ec06bae0 --- /dev/null +++ "b/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.java" @@ -0,0 +1,20 @@ +/* + * @lc app=leetcode.cn id=11 lang=java + * + * [11] 盛最多水的容器 + */ + +// @lc code=start +class Solution { + public int maxArea(int[] height) { + int max = 0; + for (int i = 0, j = height.length -1; i < j;) { + int minHeight = height[i] < height[j] ? height[i++] : height[j--]; + int area = (j - i + 1) * minHeight; + max = Math.max(area, max); + } + return max; + } +} +// @lc code=end + diff --git "a/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.js" "b/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.js" new file mode 100644 index 00000000..d1754031 --- /dev/null +++ "b/Week_01/11.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.js" @@ -0,0 +1,31 @@ +/* + * @lc app=leetcode.cn id=11 lang=javascript + * + * [11] 盛最多水的容器 + */ + +// @lc code=start +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function (height) { + let max = 0 + const getArea = (start, end, val1, val2) => { + let w = end - start + let h = Math.min(val1, val2) + return w * h + } + // 经典的所有两两组合情况 + for (let i = 0; i < height.length - 1; i++) { + for (let j = i + 1; j < height.length; j++) { + // 计算面基 + let area = getArea(i, j, height[i], height[j]) + max = Math.max(max, area) + } + } + return max +}; +// @lc code=end + +// console.log(maxArea([1,7,3,5,4])) diff --git "a/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.java" "b/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.java" new file mode 100644 index 00000000..374d34f9 --- /dev/null +++ "b/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.java" @@ -0,0 +1,24 @@ +class Solution { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + List> res = new ArrayList<>(); + for(int k = 0; k < nums.length - 2; k++){ + if(nums[k] > 0) break; + if(k > 0 && nums[k] == nums[k - 1]) continue; + int i = k + 1, j = nums.length - 1; + while(i < j){ + int sum = nums[k] + nums[i] + nums[j]; + if(sum < 0){ + while(i < j && nums[i] == nums[++i]); + } else if (sum > 0) { + while(i < j && nums[j] == nums[--j]); + } else { + res.add(new ArrayList(Arrays.asList(nums[k], nums[i], nums[j]))); + while(i < j && nums[i] == nums[++i]); + while(i < j && nums[j] == nums[--j]); + } + } + } + return res; + } +} diff --git "a/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.js" "b/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.js" new file mode 100644 index 00000000..29a670e0 --- /dev/null +++ "b/Week_01/15.\344\270\211\346\225\260\344\271\213\345\222\214.js" @@ -0,0 +1,49 @@ +/* + * @lc app=leetcode.cn id=15 lang=javascript + * + * [15] 三数之和 + */ + +// @lc code=start + +/** + * @param {number[]} nums + * @return {number[][]} + */ + +var threeSum = function (nums) { + let res = [] + + if(nums == null || nums.length < 3) return res; + + nums.sort((a, b) => a - b); // 排序 + + for (let k = 0; k < nums.length - 2; k++) { + // 因为排过序, 我们遍历的数据都应该是小于0的 + if (nums[k] > 0) break; + // 如果相邻两个数相同, 跳过. 不然会出现重复的组合 + if (k > 0 && nums[k] == nums[k - 1]) continue + // 左右之争 + let L = k + 1, R = nums.length - 1 + while (L < R) { + const sum = nums[k] + nums[L] + nums[R] + if (sum < 0) { + L++ + } else if (sum > 0) { + R-- + } else { + res.push([nums[k], nums[L], nums[R]]) + while (L a - b) + for (var i = 0; i < nums.length - 2; i++) { + const hashMap = new Map() + if (nums[i] > 0) break; + // 去重处理 + if (i > 0 && nums[i] == nums[i - 1]) continue + for (var j = i + 1; j < nums.length; j++) { + const dif = -(nums[i] + nums[j]) + // 去重处理 + // 因为hashMap是首次记录第二次才会push到数组,所以需要判断只有三次重复才能continue + if (j > i + 2 && nums[j] == nums[j - 1] && nums[j] == nums[j - 2]) + continue + if (hashMap.has(dif)) { + arr.push([nums[i], nums[hashMap.get(dif)], nums[j]]) + hashMap.delete(dif) + } + hashMap.set(nums[j], j) + } + } + return arr +}; +threeSum([-1, 0, 1, 2, -1, -4]) +threeSum([-2, 2, -1, 1]) + diff --git "a/Week_01/155.\346\234\200\345\260\217\346\240\210.js" "b/Week_01/155.\346\234\200\345\260\217\346\240\210.js" new file mode 100644 index 00000000..4346fc89 --- /dev/null +++ "b/Week_01/155.\346\234\200\345\260\217\346\240\210.js" @@ -0,0 +1,56 @@ +/* + * @lc app=leetcode.cn id=155 lang=javascript + * + * [155] 最小栈 + */ + +// @lc code=start +/** + * initialize your data structure here. + */ +var MinStack = function() { + this.stack = [] + this.minStack = [Infinity] +}; + +/** + * @param {number} x + * @return {void} + */ +MinStack.prototype.push = function(x) { + this.stack.push(x) + this.minStack.push(Math.min(x, this.minStack[this.minStack.length - 1])) +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function() { + this.stack.pop() + this.minStack.pop() +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function() { + return this.stack[this.stack.length - 1] +}; + +/** + * @return {number} + */ +MinStack.prototype.getMin = function() { + return this.minStack[this.minStack.length - 1] +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(x) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ +// @lc code=end + diff --git "a/Week_01/2.\344\270\244\346\225\260\347\233\270\345\212\240.js" "b/Week_01/2.\344\270\244\346\225\260\347\233\270\345\212\240.js" new file mode 100644 index 00000000..47f49f49 --- /dev/null +++ "b/Week_01/2.\344\270\244\346\225\260\347\233\270\345\212\240.js" @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=2 lang=javascript + * + * [2] 两数相加 + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var addTwoNumbers = function(l1, l2) { + +}; +// @lc code=end + diff --git "a/Week_01/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.js" "b/Week_01/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.js" new file mode 100644 index 00000000..26dd86cd --- /dev/null +++ "b/Week_01/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.js" @@ -0,0 +1,48 @@ +/* + * @lc app=leetcode.cn id=20 lang=javascript + * + * [20] 有效的括号 + */ + +// @lc code=start +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + + const n = s.length; + if (n % 2 === 1) { + return false; + } + let pairs = new Map([ + [')','('], + ['}','{'], + [']','['] + ]) + let r = true + let stk = [] // 栈 + s.split('').forEach(ch => { + // 如果已经存在对应的key + if(pairs.has(ch)) { + // 处理例外 + if (!stk.length || stk[stk.length - 1] !== pairs.get(ch)) { + // r=false + continue + // return false; + } + // 默认应该是pop + stk.pop() + } else { + stk.push(ch) + } + + }) + + // stk 栈为空的时候才能这样 + return r && !stk.length +}; +// @lc code=end + +console.log('isValid:', isValid('([}}])')) + diff --git "a/Week_01/283.\347\247\273\345\212\250\351\233\266.js" "b/Week_01/283.\347\247\273\345\212\250\351\233\266.js" new file mode 100644 index 00000000..751f3478 --- /dev/null +++ "b/Week_01/283.\347\247\273\345\212\250\351\233\266.js" @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=283 lang=javascript + * + * [283] 移动零 + */ + +// @lc code=start +/** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ +var moveZeroes = function(nums) { + let j = 0 + for (let i = 0; i < nums.length; i++) { + if (nums[i] !== 0) { + nums[j] = nums[i] + if(i != j) { + nums[i] = 0 + } + j++ + } + } +}; +// @lc code=end + + + diff --git "a/Week_01/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.js" "b/Week_01/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.js" new file mode 100644 index 00000000..0713594c --- /dev/null +++ "b/Week_01/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.js" @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=84 lang=javascript + * + * [84] 柱状图中最大的矩形 + */ + +// @lc code=start +/** + * @param {number[]} heights + * @return {number} + */ +var largestRectangleArea = function(heights) { + if(heights.length < 2) return heights[0] || 0 + if(heights.length === 2) return Math.min(heights[0], heights[1]) * 2 + // let left = 0, right = heights.length - 1 + let max = 0 + for (let i = 0; i < heights.length-2; i++) { + for (let j = i + 1; j < heights.length-1; j++) { + let area = Math.min(heights[i], heights[j]) * 2 + max = Math.max(max, area) + } + } + return max +}; +// @lc code=end + +largestRectangleArea([1,1]) \ No newline at end of file diff --git a/Week_01/List.java b/Week_01/List.java new file mode 100644 index 00000000..e69de29b diff --git a/Week_01/README.md b/Week_01/README.md index 50de3041..58bcd097 100644 --- a/Week_01/README.md +++ b/Week_01/README.md @@ -1 +1,39 @@ -学习笔记 \ No newline at end of file +学习笔记 + +### 数组 链表 跳表 + +https://www.notion.so/mactive/Day2-6ac425e5fb4245fe891dc752719eeadf + +### 三数之和的双指针 + +1. 先排序 +2. 锁定左侧的数据, 右侧的剩余数据双指针夹逼 +3. 因为有排序, + 1. 如果双指针之和小于左侧的数, 那么做指针向后移动,寻找更大的数, + 2. 如果大于左侧的数, 那么后面的指针往前移动 + + +### 移动非零元素 + +单指针标识法 + +j 代表下一个非0元素要放的位置, i 是无脑循环指针. j结束的时候就是非零之后的数据 + + +### LeeCode 11 盛水最多的容器 + +一维数组的坐标变换 + + +### LeeCode 70 爬楼梯 + +问题泛化, 找到最近重复子问题 + +- if else +- for +- while +- recursion + +Data Structure with Lego bricks + +用乐高零件表达数组, 链表等数据结构, 双指针等 \ No newline at end of file diff --git a/Week_01/jsbasic/break_a_loop.js b/Week_01/jsbasic/break_a_loop.js new file mode 100644 index 00000000..79738934 --- /dev/null +++ b/Week_01/jsbasic/break_a_loop.js @@ -0,0 +1,20 @@ +let count = 10 + +for (let index = 0; index < count; index++) { + + // 开启这个 return 失效 + if(index > 7) { + break + } + + // if(index > 8) { + // return + // } + + + if(index % 3 == 0) { + continue + } + + console.log('index:', index) +} diff --git a/Week_01/t1.js b/Week_01/t1.js new file mode 100644 index 00000000..210a3323 --- /dev/null +++ b/Week_01/t1.js @@ -0,0 +1,25 @@ +function copy(value) { + return JSON.parse(JSON.stringify(value)); +} + +function isArrayEqual(value1 = [], value2 = []) { + let hash = copy(value2); + if (value1.length === value2.length) { + for (let i = 0; i < value1.length; i++) { + const index = hash.indexOf(value1[i]); + if (index > -1) { + hash.splice(index, 1); + } else { + return false; + } + } + + return true; + } + + return false; +} + + +const res1 = isArrayEqual([1,2,3], [2,1,3]); // true +const res2 = isArrayEqual([1,2], [1,1]); // false \ No newline at end of file