├── README.md └── solutions ├── 0001 Two Sum └── 1 Two Sum.js ├── 0002 Add Two Numbers └── 2 Add Two Numbers.js ├── 0011 Container With Most Water ├── 11 Container With Most Water.js └── readme.md ├── 0015 3Sum └── 15 3Sum.js ├── 0021 Merge Two Sorted Lists └── 21 Merge Two Sorted Lists.js ├── 0026 Remove Duplicates from Sorted Array ├── 26 Remove Duplicates from Sorted Array.js └── readme.md ├── 0053 Maximum Subarray ├── 53 Maximum Subarray.js └── readme.md ├── 0066 Plus One └── 66 Plus One.js ├── 0075 Sort Colors ├── 75 Sort Colors.js └── readme.md ├── 0088 Merge Sorted Array └── 88 Merge Sorted Array.js ├── 0118 Pascal's Triangle ├── 118 Pascal's Triangle.js └── readme.md ├── 0121 Best Time to Buy and Sell Stock ├── 121 Best Time to Buy and Sell Stock.js └── readme.md ├── 0122 Best Time to Buy and Sell Stock II └── 122 Best Time to Buy and Sell Stock II.js ├── 0136 Single Number ├── 136 Single Number.js └── readme.md ├── 0169 Majority Element └── 169 Majority Element.js ├── 0189 Rotate Array └── 189 Rotate Array.js ├── 0202 Happy Number └── 202 Happy Number.js ├── 0204 Count Primes └── 204 Count Primes.js ├── 0217 Contains Duplicate ├── 217 Contains Duplicate.js └── readme.md ├── 0242 Valid Anagram └── 242 Valid Anagram.js ├── 0268 Missing Number ├── 268 Missing Number.js └── readme.md ├── 0283 Move Zeroes └── 283 Move Zeroes.js ├── 0350 Intersection of Two Arrays II └── 350 Intersection of Two Arrays II.js └── 0387 First Unique Character in a String └── 387 First Unique Character in a String.js /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Solutions with JavaScript 2 | 3 | **24** solved 4 | 5 | ### Almost every solution beats more than 98% of the others in runtime. 6 | 7 | 8 | | # | Problems | Solutions | Difficulty | 9 | |:---:|:---:|:---:|:---:| 10 | | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Solution](https://github.com/etheranl/leetcode/blob/master/solutions/0001%20Two%20Sum/1%20Two%20Sum.js) | Easy | 11 | | 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0002%20Add%20Two%20Numbers) | Easy | 12 | | 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0011%20Container%20With%20Most%20Water) | Easy | 13 | | 11 | [3Sum](https://leetcode.com/problems/3sum/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0015%203Sum) | Easy | 14 | | 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0021%20Merge%20Two%20Sorted%20Lists) | Easy | 15 | | 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0026%20Remove%20Duplicates%20from%20Sorted%20Array) | Easy | 16 | | 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0053%20Maximum%20Subarray) | Easy | 17 | | 66 | [Plus One](https://leetcode.com/problems/plus-one/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0066%20Plus%20One) | Easy | 18 | | 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0075%20Sort%20Colors) | Medium | 19 | | 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0088%20Merge%20Sorted%20Array) | Easy | 20 | | 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0118%20Pascal's%20Triangle) | Easy | 21 | | 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0121%20Best%20Time%20to%20Buy%20and%20Sell%20Stock) | Easy | 22 | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0121%20Best%20Time%20to%20Buy%20and%20Sell%20Stock) | Easy | 23 | | 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0136%20Single%20Number) | Easy | 24 | | 169 | [Majority Element](https://leetcode.com/problems/majority-element/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0169%20Majority%20Element) | Easy | 25 | | 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0189%20Rotate%20Array) | Easy | 26 | | 202 | [Happy Number](https://leetcode.com/problems/happy-number/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0202%20Happy%20Number) | Easy | 27 | | 204 | [Count Primes](https://leetcode.com/problems/count-primes/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0204%20Count%20Primes) | Easy | 28 | | 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0217%20Contains%20Duplicate) | Easy | 29 | | 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0242%20Valid%20Anagram) | Easy | 30 | | 268 | [Missing Number](https://leetcode.com/problems/missing-number/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0268%20Missing%20Number) | Easy | 31 | | 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0283%20Move%20Zeroes) | Easy | 32 | | 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0350%20Intersection%20of%20Two%20Arrays%20II) | Easy | 33 | | 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/description/) | [Solution](https://github.com/etheranl/leetcode/tree/master/solutions/0387%20First%20Unique%20Character%20in%20a%20String) | Easy | 34 | -------------------------------------------------------------------------------- /solutions/0001 Two Sum/1 Two Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl(haoran) 3 | * Given an array of integers, return indices of the two numbers such that they add up to a specific target. 4 | * You may assume that each input would have exactly one solution, and you may not use the same element twice. 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @param {number} target 10 | * @return {number[]} 11 | */ 12 | var twoSum = function (nums, target) { 13 | let map = {}; 14 | let result = [] 15 | nums.some((num, i) => { 16 | if (map[num] !== undefined) { 17 | result = [map[num], i] 18 | return true 19 | } 20 | map[target - num] = i; 21 | return false 22 | }) 23 | return result; 24 | }; 25 | console.log(twoSum([1, 3, 4], 7)); -------------------------------------------------------------------------------- /solutions/0002 Add Two Numbers/2 Add Two Numbers.js: -------------------------------------------------------------------------------- 1 | // You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 2 | 3 | // You may assume the two numbers do not contain any leading zero, except the number 0 itself. 4 | 5 | // Example 6 | 7 | // Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 8 | // Output: 7 -> 0 -> 8 9 | // Explanation: 342 + 465 = 807. 10 | 11 | /** 12 | * @param {ListNode} l1 13 | * @param {ListNode} l2 14 | * @return {ListNode} 15 | */ 16 | 17 | var addTwoNumbers = function (l1, l2) { 18 | let result = null, 19 | add = 0, 20 | p1 = l1, 21 | p2 = l2, 22 | pointer = null, 23 | node = null 24 | while (p1 || p2) { 25 | let p1Val = 0, p2Val = 0, val = 0; 26 | if (p1) { 27 | p1Val = p1.val 28 | p1 = p1.next 29 | } 30 | if (p2) { 31 | p2Val = p2.val 32 | p2 = p2.next 33 | } 34 | val = p1Val + p2Val + add 35 | add = ~~(val / 10) 36 | node = new ListNode(add ? (val - add * 10) : val); 37 | pointer ? pointer.next = node : result = node 38 | pointer = node 39 | } 40 | if (add) { 41 | pointer.next = new ListNode(add) 42 | } 43 | return result 44 | }; -------------------------------------------------------------------------------- /solutions/0011 Container With Most Water/11 Container With Most Water.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/container-with-most-water/description/ 5 | */ 6 | 7 | 8 | /** 9 | * @param {number[]} height 10 | * @return {number} 11 | */ 12 | var maxArea = function (height) { 13 | let start = 0, 14 | end = height.length - 1, 15 | area = 0; 16 | while (start !== end) { 17 | if (height[start] > height[end]) { 18 | area = Math.max(area, height[end] * (end - start)) 19 | end-- 20 | continue 21 | } 22 | area = Math.max(area, height[start] * (end - start)) 23 | start++ 24 | } 25 | return area 26 | }; 27 | 28 | console.log(maxArea([2,1,10,5])); -------------------------------------------------------------------------------- /solutions/0011 Container With Most Water/readme.md: -------------------------------------------------------------------------------- 1 | Two pointer are set at the head and the tail, and the smaller one moves toward the other. After moving, compare the current area with the previous area, keep the greater. Repeat this step until the pointer coincides -------------------------------------------------------------------------------- /solutions/0015 3Sum/15 3Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/3sum/description/ 5 | */ 6 | 7 | 8 | /** 9 | * @param {number[]} nums 10 | * @return {number[][]} 11 | */ 12 | var threeSum = function (nums) { 13 | let result = [] 14 | nums.sort((a, b) => a - b) 15 | for (let i = 0; i < nums.length - 2; i++) { 16 | let target = -nums[i], 17 | start = i + 1, 18 | end = nums.length - 1, 19 | mark = false 20 | while (start !== end) { 21 | let count = start + end 22 | if (mark) break; 23 | switch (true) { 24 | case count > target: 25 | end-- 26 | break; 27 | case count < target: 28 | start++ 29 | break; 30 | default: 31 | mark = true 32 | result.push([nums[i], start, end]) 33 | break; 34 | } 35 | } 36 | } 37 | console.log(result); 38 | return result 39 | }; 40 | 41 | 42 | threeSum([-1, 0, 1, 2, -1, -4, 2, -5, 4]) -------------------------------------------------------------------------------- /solutions/0021 Merge Two Sorted Lists/21 Merge Two Sorted Lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/merge-two-sorted-lists/description/ 5 | */ 6 | 7 | /** 8 | * Definition for singly-linked list. 9 | * function ListNode(val) { 10 | * this.val = val; 11 | * this.next = null; 12 | * } 13 | */ 14 | /** 15 | * @param {ListNode} l1 16 | * @param {ListNode} l2 17 | * @return {ListNode} 18 | */ 19 | var mergeTwoLists = function (l1, l2) { 20 | let newList = new ListNode(Math.min(l2.val, l1.val)) 21 | while (l2.val < l1.val) { 22 | newList.next = 23 | } 24 | }; 25 | 26 | mergeTwoLists({ val: 1, next: { val: 2, next: { val: 3 } } }, { val: 2, next: { val: 3, next: { val: 4 } } }) 27 | 28 | function ListNode(val) { 29 | this.val = val; 30 | this.next = null; 31 | } -------------------------------------------------------------------------------- /solutions/0026 Remove Duplicates from Sorted Array/26 Remove Duplicates from Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {number} 10 | */ 11 | var removeDuplicates = function (nums) { 12 | let map = {}, 13 | count = 0; 14 | nums.forEach((item, index) => { 15 | if (map[item] === undefined) { 16 | map[item] = true 17 | nums[count] = item; 18 | count++ 19 | } 20 | }) 21 | return count 22 | } 23 | 24 | console.log(removeDuplicates([1,1,3,1])) -------------------------------------------------------------------------------- /solutions/0026 Remove Duplicates from Sorted Array/readme.md: -------------------------------------------------------------------------------- 1 | #### 这一题直接实现比较简单,但是要想性能超过绝大部分的答案,需要注意的是题中案例描述为`with the first x elements of nums` 2 | #### 所以实际上只要保证数组前x个元素正确,并且返回的length正确即可。比如针对[1,1,3,1],将nums处理为[1,3,3,1],return值为2,是完全符合要求的 -------------------------------------------------------------------------------- /solutions/0053 Maximum Subarray/53 Maximum Subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/maximum-subarray/description/ 5 | */ 6 | 7 | 8 | /** 9 | * @param {number[]} nums 10 | * @return {number} 11 | */ 12 | var maxSubArray = function (nums) { 13 | if (!nums.length) return null 14 | let local = nums[0] 15 | let result = nums[0] 16 | for (let i = 1; i < nums.length; i++) { 17 | local = Math.max(nums[i], local + nums[i]) 18 | result = Math.max(local, result) 19 | } 20 | return result 21 | } 22 | 23 | console.log(maxSubArray([-3, 1, 3, -2, 3])); -------------------------------------------------------------------------------- /solutions/0053 Maximum Subarray/readme.md: -------------------------------------------------------------------------------- 1 | #### -------------------------------------------------------------------------------- /solutions/0066 Plus One/66 Plus One.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/plus-one/description/ 5 | */ 6 | 7 | 8 | /** 9 | * @param {number[]} digits 10 | * @return {number[]} 11 | */ 12 | var plusOne = function (digits) { 13 | for (i = digits.length - 1; i >= 0;) { 14 | if (digits[i] === 9) { 15 | if (i == 0) { 16 | digits[i] = 0 17 | digits.unshift(1) 18 | break 19 | } 20 | digits[i] = 0 21 | i-- 22 | continue 23 | } 24 | digits[i] += 1 25 | break 26 | } 27 | return digits 28 | }; 29 | 30 | console.log(plusOne([9, 9, 9, 9])); -------------------------------------------------------------------------------- /solutions/0075 Sort Colors/75 Sort Colors.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {void} Do not return anything, modify nums in-place instead. 4 | */ 5 | var sortColors = function (nums) { 6 | let left = -1; 7 | let right = nums.length; 8 | let i = 0; 9 | while (i < right) { 10 | switch (nums[i]) { 11 | case 0: 12 | swap(nums, i++, ++left) 13 | break; 14 | case 1: 15 | i++; 16 | break; 17 | default: 18 | swap(nums, i, --right) 19 | break; 20 | } 21 | } 22 | }; 23 | 24 | function swap(arr, p1, p2) { 25 | let temp = arr[p1]; 26 | arr[p1] = arr[p2]; 27 | arr[p2] = temp; 28 | } -------------------------------------------------------------------------------- /solutions/0075 Sort Colors/readme.md: -------------------------------------------------------------------------------- 1 | 三路快排 -------------------------------------------------------------------------------- /solutions/0088 Merge Sorted Array/88 Merge Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums1 3 | * @param {number} m 4 | * @param {number[]} nums2 5 | * @param {number} n 6 | * @return {void} Do not return anything, modify nums1 in-place instead. 7 | */ 8 | var merge = function (nums1, m, nums2, n) { 9 | let pointer = 0 10 | nums1.splice(m, nums1.length) 11 | nums2.forEach((num) => { 12 | while (num > nums1[pointer]) { 13 | pointer++ 14 | } 15 | nums1.splice(pointer, 0, num) 16 | pointer++ 17 | }) 18 | } 19 | 20 | merge([0], 0, [1], 1) 21 | -------------------------------------------------------------------------------- /solutions/0118 Pascal's Triangle/118 Pascal's Triangle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/pascals-triangle/description/ 5 | */ 6 | 7 | // [ 8 | // [1], 9 | // [1,1], 10 | // [1,2,1], 11 | // [1,3,3,1], 12 | // [1,4,6,4,1] 13 | // ] 14 | 15 | 16 | /** 17 | * @param {number} numRows 18 | * @return {number[][]} 19 | */ 20 | var generate = function (numRows) { 21 | let result = [[1]] 22 | if (numRows === 0) { // 如果输入0则直接返回[] 23 | return [] 24 | } 25 | for (let i = 1; i < numRows; i++) { 26 | let curArr = [1] // 初始化当前项数组 27 | for (let j = 1; j <= i; j++) { 28 | if (j > i / 2) { // 对称 29 | curArr.push(curArr[i - j]) 30 | } else { 31 | curArr.push(result[i - 1][j - 1] + result[i - 1][j]) // 计算第j项,为上一个数组的[j - 1]和j项之和 32 | } 33 | } 34 | result.push(curArr) 35 | } 36 | return result 37 | }; 38 | 39 | generate(5) -------------------------------------------------------------------------------- /solutions/0118 Pascal's Triangle/readme.md: -------------------------------------------------------------------------------- 1 | 需要注意的是数值是对称的,不需要完整计算每一项 -------------------------------------------------------------------------------- /solutions/0121 Best Time to Buy and Sell Stock/121 Best Time to Buy and Sell Stock.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} prices 9 | * @return {number} 10 | */ 11 | var maxProfit = function (prices) { 12 | let profit = 0, 13 | min = prices[0] 14 | for (let i = 1; i < prices.length; i++) { 15 | let num = prices[i] 16 | min = Math.min(min, num); // 记录最小值 17 | profit = Math.max(profit, num - min); // 拿最小值之后可能产生的最大利润和已经产生的最大利润做比较 18 | } 19 | return profit 20 | }; 21 | 22 | console.log(maxProfit([7, 2, 4, 6, 1, 3, 8])); 23 | -------------------------------------------------------------------------------- /solutions/0121 Best Time to Buy and Sell Stock/readme.md: -------------------------------------------------------------------------------- 1 | 思路:需要一个变量记录最小值,并在之后的过程中比较当前值和最小值之差。并用之差与之前的最大利润比较,大者为较大的利润。 -------------------------------------------------------------------------------- /solutions/0122 Best Time to Buy and Sell Stock II/122 Best Time to Buy and Sell Stock II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} prices 3 | * @return {number} 4 | */ 5 | var maxProfit = function (prices) { 6 | let profit = 0 7 | for (let i = 0; i < prices.length; i++) { 8 | if (prices[i + 1] > prices[i]) { 9 | profit += prices[i + 1] - prices[i] 10 | } 11 | } 12 | return profit 13 | }; 14 | 15 | maxProfit([1, 3, 2, 3, 4, 6]) -------------------------------------------------------------------------------- /solutions/0136 Single Number/136 Single Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc https://leetcode.com/problems/single-number/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {number} 10 | */ 11 | var singleNumber = function (nums) { 12 | let result = 0; 13 | nums.forEach((num) => { 14 | result ^= num 15 | }) 16 | return result 17 | } 18 | 19 | 20 | console.log(singleNumber([2, 1, 2, 1, 3])); 21 | 22 | -------------------------------------------------------------------------------- /solutions/0136 Single Number/readme.md: -------------------------------------------------------------------------------- 1 | use the feature of XOR : 2 | n^n = 0 -------------------------------------------------------------------------------- /solutions/0169 Majority Element/169 Majority Element.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc https://leetcode.com/problems/majority-element/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {number} 10 | */ 11 | 12 | 13 | // solution one 14 | // var majorityElement = function (nums) { 15 | // nums.sort(); 16 | // return nums[~~(nums.length / 2)]; 17 | // }; 18 | 19 | // solution two 20 | var majorityElement = function (nums) { 21 | let map = {}, 22 | result = 0, 23 | len = nums.length / 2; 24 | nums.some((num) => { 25 | if (map[num] === undefined) { 26 | map[num] = 1 27 | } else { 28 | map[num]++ 29 | } 30 | if (map[num] > len) { 31 | result = num 32 | return true 33 | } 34 | return false 35 | }) 36 | return result 37 | }; 38 | 39 | 40 | majorityElement([1, 2, 3, 4, 2, 3, 3]) -------------------------------------------------------------------------------- /solutions/0189 Rotate Array/189 Rotate Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/rotate-array/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @param {number} k 10 | * @return {void} Do not return anything, modify nums in-place instead. 11 | */ 12 | 13 | // beats 100% 14 | var rotate = function (nums, k) { 15 | let len = nums.length; 16 | let arr = nums.splice(len - k, len - 1) 17 | nums.unshift(...arr) 18 | }; 19 | 20 | 21 | rotate([1, 2, 3, 4, 5, 6, 7], 3) -------------------------------------------------------------------------------- /solutions/0202 Happy Number/202 Happy Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/happy-number/description/ 5 | */ 6 | 7 | /** 8 | * @param {number} n 9 | * @return {boolean} 10 | */ 11 | var isHappy = function (n) { 12 | let number = n 13 | let map = {} 14 | while (!map[number]) { 15 | let arr = (number + '').split('') 16 | if (number == 1) return true 17 | map[number] = true 18 | number = 0; 19 | arr.forEach((num) => { 20 | number += num * num 21 | }) 22 | } 23 | return false 24 | }; 25 | 26 | console.log(isHappy(19)); 27 | 28 | 29 | -------------------------------------------------------------------------------- /solutions/0204 Count Primes/204 Count Primes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/count-primes/description/ 5 | */ 6 | 7 | /** 8 | * @param {number} n 9 | * @return {number} 10 | */ 11 | var countPrimes = function (n) { 12 | let map = new Array(n) 13 | , a = Math.sqrt(n); 14 | let result = 0; 15 | for (let i = 2; i <= a; i++) { 16 | if (!map[i]) { 17 | for (var j = i * i; j < n; j += i) { 18 | map[j] = true; 19 | } 20 | } 21 | } 22 | for (let i = 2; i < n; i++) { 23 | if (!map[i]) result++; 24 | } 25 | return result; 26 | }; 27 | 28 | console.log(countPrimes(19)); 29 | -------------------------------------------------------------------------------- /solutions/0217 Contains Duplicate/217 Contains Duplicate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/contains-duplicate/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {boolean} 10 | */ 11 | var containsDuplicate = function (nums) { 12 | let set = new Set(nums) 13 | return nums.length !== set.size 14 | }; 15 | 16 | containsDuplicate([1, 3, 4, 1]) -------------------------------------------------------------------------------- /solutions/0217 Contains Duplicate/readme.md: -------------------------------------------------------------------------------- 1 | We can use the non-repeat feature of Set,and compare `Set.size()` and `num.length`.If two value is not equal, mean there are repeat elements. -------------------------------------------------------------------------------- /solutions/0242 Valid Anagram/242 Valid Anagram.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/valid-anagram/description/ 5 | */ 6 | 7 | /** 8 | * @param {string} s 9 | * @param {string} t 10 | * @return {boolean} 11 | */ 12 | var isAnagram = function (s, t) { 13 | let sArr = s.split(''), 14 | tArr = t.split(''), 15 | sMap = {}, 16 | tMap = {}; 17 | if (sArr.length !== tArr.length) return false 18 | sArr.forEach((letter, index) => { 19 | if (sMap[letter] === undefined) { 20 | sMap[letter] = 1 21 | } else { 22 | sMap[letter]++ 23 | } 24 | if (tMap[tArr[index]] === undefined) { 25 | tMap[tArr[index]] = 1 26 | } else { 27 | tMap[tArr[index]]++ 28 | } 29 | }) 30 | return Object.keys(sMap).every((num) => { 31 | return sMap[num] === tMap[num]; 32 | }) 33 | }; 34 | 35 | console.log(isAnagram('sbf', 'bsa')); 36 | -------------------------------------------------------------------------------- /solutions/0268 Missing Number/268 Missing Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/missing-number/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {number} 10 | */ 11 | var missingNumber = function (nums) { 12 | return nums.length * (nums.length + 1) / 2 - nums.reduce((realTotal,sum)=> realTotal += sum) 13 | } 14 | 15 | console.log(missingNumber([0, 2])); 16 | -------------------------------------------------------------------------------- /solutions/0268 Missing Number/readme.md: -------------------------------------------------------------------------------- 1 | If there is no missing number in this array, the sum is `n*(n+1)/2`. 2 | So we can get the answer by using the sum of the complete array minus the sum of the current array. -------------------------------------------------------------------------------- /solutions/0283 Move Zeroes/283 Move Zeroes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/move-zeroes/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {void} Do not return anything, modify nums in-place instead. 10 | */ 11 | var moveZeroes = function (nums) { 12 | let zeros = []; 13 | for (let i = 0; i < nums.length;) { 14 | if (nums[i] === 0) { 15 | nums.splice(i, 1) 16 | zeros.push(0) 17 | continue 18 | } 19 | i++ 20 | } 21 | nums.splice(nums.length, 0, ...zeros) 22 | }; 23 | 24 | 25 | moveZeroes([0, 0, 1]) 26 | -------------------------------------------------------------------------------- /solutions/0350 Intersection of Two Arrays II/350 Intersection of Two Arrays II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 5 | */ 6 | 7 | /** 8 | * @param {number[]} nums1 9 | * @param {number[]} nums2 10 | * @return {number[]} 11 | */ 12 | var intersect = function (nums1, nums2) { 13 | let map1 = {}, 14 | map2 = {}, 15 | result = []; 16 | nums1.forEach((num) => { 17 | if (map1[num] === undefined) { 18 | map1[num] = 1 19 | } else { 20 | map1[num]++ 21 | } 22 | }) 23 | nums2.forEach((num) => { 24 | if (map2[num] === undefined) { 25 | map2[num] = 1 26 | } else { 27 | map2[num]++ 28 | } 29 | }) 30 | Object.keys(map1).forEach((item, index) => { 31 | while (map1[item] && map2[item]) { 32 | result.push(+item) 33 | map1[item]-- 34 | map2[item]-- 35 | } 36 | }) 37 | return result 38 | }; 39 | 40 | console.log(intersect([1, 3, 4], [3, 4])); -------------------------------------------------------------------------------- /solutions/0387 First Unique Character in a String/387 First Unique Character in a String.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author etheranl-haoran 3 | * @email imhaoran.wang@foxmail.com 4 | * @desc see:https://leetcode.com/problems/first-unique-character-in-a-string/description/ 5 | */ 6 | 7 | /** 8 | * @param {string} s 9 | * @return {number} 10 | */ 11 | var firstUniqChar = function (s) { 12 | for (let i = 0; i < s.length; i++) { 13 | let item = s[i]; 14 | if (s.lastIndexOf(item) === s.indexOf(item)){ 15 | return i; 16 | } 17 | } 18 | return -1; 19 | }; 20 | 21 | console.log(firstUniqChar('leetcode')); --------------------------------------------------------------------------------