Skip to content

第一周作业#13 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Week_01/id_13/LeetCode_15_013.js
Original file line number Diff line number Diff line change
@@ -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(index<end)
{

let sum = nums[start]+nums[index] + nums[end];
if(sum == 0 )
{
let newArray = [nums[start],nums[index],nums[end]];
result.set(newArray.join(''),newArray);
while(nums[index]==nums[index+1])
{
index++;
}
index++;
continue;
}
if(sum >0)
{
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));
31 changes: 31 additions & 0 deletions Week_01/id_13/LeetCode_189_013.js
Original file line number Diff line number Diff line change
@@ -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 (start<end) {
let temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end --;
}
return nums;

}
let nums=[1,2,3,4,5,6,7];
let k = 3;
console.log(rotate(nums,k));
19 changes: 19 additions & 0 deletions Week_01/id_13/LeetCode_24_013.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//https://leetcode.com/problems/swap-nodes-in-pairs/

var swapPairs = function(head) {
if(!head || !head.next) return head;
let dummy = new ListNode(0);
dummy.next = head;
let current = dummy;
while (current.next && current.next.next)
{
let n = current.next;
let nn = current.next.next;
current.next.next = current.next.next.next;
current.next = nn;
current.next.next=n;
current = current.next.next

}
return dummy.next;
};
22 changes: 22 additions & 0 deletions Week_01/id_13/LeetCode_26_013.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} nums
* @return {number}
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
*/
var removeDuplicates = function(nums) {
if(nums.length==0) return nums;
let start = 0;
for (let index = 0; index < nums.length; index++) {
if(nums[start]!=nums[index])
{
start++;
nums[start] = nums[index];
}
}
return start;

};

let nums=[1,1,2];
console.log(removeDuplicates(nums));

43 changes: 43 additions & 0 deletions Week_01/id_13/LeetCode_42_013.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @param {number[]} height
* @return {number}
* https://leetcode.com/problems/trapping-rain-water/
*/
var trap = function(height) {
if (height.length<2) return 0;
let head = 0;
let tail = head+1;
let totalArea=0;
let temp=0;
while(tail<height.length)
{
temp = temp+height[tail-1];
if(height[tail]>height[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));
16 changes: 16 additions & 0 deletions Week_01/id_13/LeetCode_49_013.js
Original file line number Diff line number Diff line change
@@ -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));
24 changes: 24 additions & 0 deletions Week_01/id_13/LeetCode_50_013.js
Original file line number Diff line number Diff line change
@@ -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));