From e9c4ada0af8e1cf4712737538ad8a74d6952bb61 Mon Sep 17 00:00:00 2001 From: "wendy.gu" Date: Fri, 18 Oct 2019 12:02:16 +0800 Subject: [PATCH 1/3] LeetCode_26_256.js --- Week 01/id_256/LeetCode_26_256.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Week 01/id_256/LeetCode_26_256.js diff --git a/Week 01/id_256/LeetCode_26_256.js b/Week 01/id_256/LeetCode_26_256.js new file mode 100644 index 000000000..e9fdce544 --- /dev/null +++ b/Week 01/id_256/LeetCode_26_256.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function(nums) { + var i = 0; + nums.forEach(function (elem) { + if (elem !== nums[i]) { + nums[++i] = elem; + } + }); + return nums.length && i + 1; +}; \ No newline at end of file From 9a94d6f145dc9744b4bf8e93b20eeba0bd354eb9 Mon Sep 17 00:00:00 2001 From: "wendy.gu" Date: Sat, 19 Oct 2019 16:00:26 +0800 Subject: [PATCH 2/3] LeetCode_189_256.js --- Week 01/id_256/LeetCode_189_256.js | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Week 01/id_256/LeetCode_189_256.js diff --git a/Week 01/id_256/LeetCode_189_256.js b/Week 01/id_256/LeetCode_189_256.js new file mode 100644 index 000000000..9b58928c7 --- /dev/null +++ b/Week 01/id_256/LeetCode_189_256.js @@ -0,0 +1,54 @@ +/* + * @lc app=leetcode.cn id=189 lang=javascript + * + * [189] 旋转数组 + */ + +// @lc code=start +/** + * @param {number[]} nums + * @param {number} k + * @return {void} Do not return anything, modify nums in-place instead. + */ +var rotate = function(nums, k) { + // 暴力解 + // if(nums.length === 0 || nums.length === k) { + // return nums; + // } + // while (k > 0) { + // let previous = nums[nums.length - 1]; + // for (let i = 0; i < nums.length; i++) { + // let temp = nums[i]; + // nums[i] = previous; + // previous = temp; + // } + // k--; + // } + // return nums; + + // 绝起反转 + // let len = nums.length; + // k = k % len; + // if (k == len || len == 1 || len == 0) return nums; + // nums = nums.splice(len - k, k).concat(nums); + // return nums; + + // 分段截取 + let n = nums.length; + k %= n; + let tmp = 0; + if (n == 1 || k == n) return; + myRevese(0, n - 1); + myRevese(0, k - 1); + myRevese(k, n - 1); + function myRevese(start, end) { + while (start < end) { + tmp = nums[start]; + nums[start] = nums[end]; + nums[end] = tmp; + start++; + end--; + } + } +}; +// @lc code=end From bd9793f42544a5831b4cc312e96db2fee7c10298 Mon Sep 17 00:00:00 2001 From: "wendy.gu" Date: Sun, 20 Oct 2019 00:23:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=85=B6=E4=BB=96=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E9=A2=98=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week 01/id_256/LeetCode_1_256.js | 28 ++++++++++++++++++++++ Week 01/id_256/LeetCode_283_256.js | 37 ++++++++++++++++++++++++++++++ Week 01/id_256/LeetCode_88_256.js | 34 +++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 Week 01/id_256/LeetCode_1_256.js create mode 100644 Week 01/id_256/LeetCode_283_256.js create mode 100644 Week 01/id_256/LeetCode_88_256.js diff --git a/Week 01/id_256/LeetCode_1_256.js b/Week 01/id_256/LeetCode_1_256.js new file mode 100644 index 000000000..0db981ab0 --- /dev/null +++ b/Week 01/id_256/LeetCode_1_256.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 = {}; + let res = []; + for (let i = 0; i < nums.length; i++) { + let tmp = target - nums[i]; + if(map[tmp+''] != undefined) { + res = [map[tmp+''], i]; + break; + } else { + map[nums[i]+''] = i; + } + } + return res; +}; +// @lc code=end + diff --git a/Week 01/id_256/LeetCode_283_256.js b/Week 01/id_256/LeetCode_283_256.js new file mode 100644 index 000000000..22ece743e --- /dev/null +++ b/Week 01/id_256/LeetCode_283_256.js @@ -0,0 +1,37 @@ +/* + * @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 pos = 0; + // for (let i = 0; i < nums.length; i++) { + // if (nums[i] != 0) { + // nums[pos++] = nums[i]; + // } + // } + // while( pos < nums.length) { + // nums[pos++] = 0; + // } + // + //方法二 + let pos = 0; + for (let i = 0; i < nums.length; i++) { + if (nums[i] != 0) { + if (i != pos) { + nums[pos] = nums[i]; + nums[i] = 0; + } + pos++; + } + } + }; + // @lc code=end + \ No newline at end of file diff --git a/Week 01/id_256/LeetCode_88_256.js b/Week 01/id_256/LeetCode_88_256.js new file mode 100644 index 000000000..1fff378fb --- /dev/null +++ b/Week 01/id_256/LeetCode_88_256.js @@ -0,0 +1,34 @@ +/* + * @lc app=leetcode.cn id=88 lang=javascript + * + * [88] 合并两个有序数组 + */ + +// @lc code=start +/** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ +var merge = function(nums1, m, nums2, n) { + //splice截取 合并 排序 + // nums1.splice(m, nums1.length - m); + // nums2.splice(n, nums2.length - n); + // Object.assign(nums1, [...nums1, ...nums2]).sort((a, b) => a - b); + + // 得用数组有序 + let pos = m + n - 1; + while (n > 0) { + if (m > 0 && nums1[m - 1] > nums2[n - 1]) { + nums1[pos--] = nums1[m - 1]; + m--; + } else { + nums1[pos--] = nums2[n - 1]; + n--; + } + } + }; + // @lc code=end + \ No newline at end of file