diff --git a/Week_01/id_13/LeetCode_15_013.js b/Week_01/id_13/LeetCode_15_013.js new file mode 100644 index 00000000..9940c88b --- /dev/null +++ b/Week_01/id_13/LeetCode_15_013.js @@ -0,0 +1,61 @@ +/** + * @param {number[]} nums + * @return {number[][]} + * https://leetcode.com/problems/3sum/ + */ + +var threeSum = function(nums) { + if(nums.length < 3) return []; + nums.sort(function(a,b){return a-b;}); + let start= 0; + let result = new Map(); + while(nums.length-1>start+1) + { + let end = nums.length-1; + let index = start+1; + while(index0) + { + while(nums[end]==nums[end-1]) + { + end --; + } + end = end-1; + } + else + { + while(nums[index]==nums[index+1]) + { + index++; + } + index++; + } + } + start = start+1; + } + let resultArray = new Array(); + result.forEach(element => { + resultArray.push(element); + + }); + + return resultArray; + +}; + +let nums = [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]; +console.log(threeSum(nums)); diff --git a/Week_01/id_13/LeetCode_189_013.js b/Week_01/id_13/LeetCode_189_013.js new file mode 100644 index 00000000..71991e81 --- /dev/null +++ b/Week_01/id_13/LeetCode_189_013.js @@ -0,0 +1,31 @@ +//https://leetcode.com/problems/rotate-array/ + +var rotate = function(nums, k) { + if(nums.length<2) return nums; + if(k>0) + { + k=k%nums.length; + reverse(nums,0,nums.length-1); + reverse(nums,0,k-1); + reverse(nums,k,nums.length-1); + } + return nums; + +}; + +function reverse(nums,start,end) +{ + if(!nums || !nums.length || start >=end) return ; + while (startheight[head]) + { + let a= (tail-head) * height[head] - temp; + totalArea += a; + temp=0; + head= tail; + } + tail ++; + } + head = height.length-1; + tail = head-1; + temp = 0; + while(tail >=0 ) + { + temp = temp+height[tail+1]; + if(height[tail]>=height[head]) + { + let a= (head-tail)*height[head]-temp; + totalArea += a; + temp=0; + head= tail; + } + tail --; + } + return totalArea; +}; + +let height=[2,0,2]; +console.log(trap(height)); diff --git a/Week_01/id_13/LeetCode_49_013.js b/Week_01/id_13/LeetCode_49_013.js new file mode 100644 index 00000000..68142166 --- /dev/null +++ b/Week_01/id_13/LeetCode_49_013.js @@ -0,0 +1,16 @@ +/** + * @param {string[]} strs + * @return {string[][]} + * https://leetcode.com/problems/group-anagrams/ + */ +var groupAnagrams = function(strs) { + let res = {}; + for (let str of strs) { + let tmp = str.split('').sort().join(''); + (res[tmp] == null) ? res[tmp] = [str] : res[tmp].push(str); + } + return Object.values(res); +}; + +let input= ["eat", "tea", "tan", "ate", "nat", "bat"]; +console.log(groupAnagrams(input)); diff --git a/Week_01/id_13/LeetCode_50_013.js b/Week_01/id_13/LeetCode_50_013.js new file mode 100644 index 00000000..9661b715 --- /dev/null +++ b/Week_01/id_13/LeetCode_50_013.js @@ -0,0 +1,24 @@ +/** + * @param {number} x + * @param {number} n + * @return {number} + * https://leetcode.com/problems/powx-n/ + */ + +var myPow = function(x, n) { + if(n == 1) return x; + if(n == 0) return 1; + if(n < 0) return 1/myPow(x,-n); + if(n%2==0) + { + let val= myPow(x,n/2); + return val*val; + } + else + { + let val = myPow(x,(n-1)/2); + return val*val*x; + } + +} +console.log(myPow(0.00001,2147483647));