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 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_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 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