Skip to content

Commit 46d94b1

Browse files
committed
week 01, home works
1 parent 4f00c05 commit 46d94b1

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

Week_01/id_13/groupAnagrams.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function(strs) {
6+
let res = {};
7+
for (let str of strs) {
8+
let tmp = str.split('').sort().join('');
9+
(res[tmp] == null) ? res[tmp] = [str] : res[tmp].push(str);
10+
}
11+
return Object.values(res);
12+
};
13+
14+
let input= ["eat", "tea", "tan", "ate", "nat", "bat"];
15+
console.log(groupAnagrams(input));

Week_01/id_13/myPow.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} x
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
7+
var myPow = function(x, n) {
8+
if(n == 1) return x;
9+
if(n == 0) return 1;
10+
if(n < 0) return 1/myPow(x,-n);
11+
if(n%2==0)
12+
{
13+
let val= myPow(x,n/2);
14+
return val*val;
15+
}
16+
else
17+
{
18+
let val = myPow(x,(n-1)/2);
19+
return val*val*x;
20+
}
21+
22+
}
23+
console.log(myPow(0.00001,2147483647));

Week_01/id_13/removeDuplicates.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var removeDuplicates = function(nums) {
6+
if(nums.length==0) return nums;
7+
let start = 0;
8+
for (let index = 0; index < nums.length; index++) {
9+
if(nums[start]!=nums[index])
10+
{
11+
start++;
12+
nums[start] = nums[index];
13+
}
14+
}
15+
16+
//return nums ;
17+
return start;
18+
19+
};
20+
21+
let nums=[1,1,2];
22+
console.log(removeDuplicates(nums));
23+

Week_01/id_13/rotate.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var rotate = function(nums, k) {
2+
if(nums.length<2) return nums;
3+
if(k>0)
4+
{
5+
k=k%nums.length;
6+
reverse(nums,0,nums.length-1);
7+
reverse(nums,0,k-1);
8+
reverse(nums,k,nums.length-1);
9+
}
10+
return nums;
11+
12+
};
13+
14+
function reverse(nums,start,end)
15+
{
16+
if(!nums || !nums.length || start >=end) return ;
17+
while (start<end) {
18+
let temp = nums[start];
19+
nums[start] = nums[end];
20+
nums[end] = temp;
21+
start++;
22+
end --;
23+
}
24+
return nums;
25+
26+
}
27+
let nums=[1,2,3,4,5,6,7];
28+
let k = 3;
29+
console.log(rotate(nums,k));

Week_01/id_13/swapPairs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var swapPairs = function(head) {
2+
if(!head || !head.next) return head;
3+
let dummy = new ListNode(0);
4+
dummy.next = head;
5+
let current = dummy;
6+
while (current.next && current.next.next)
7+
{
8+
let n = current.next;
9+
let nn = current.next.next;
10+
current.next.next = current.next.next.next;
11+
current.next = nn;
12+
current.next.next=n;
13+
current = current.next.next
14+
15+
}
16+
return dummy.next;
17+
};

Week_01/id_13/threeSum.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
var threeSum = function(nums) {
7+
if(nums.length < 3) return [];
8+
nums.sort(function(a,b){return a-b;});
9+
let start= 0;
10+
let result = new Map();
11+
while(nums.length-1>start+1)
12+
{
13+
let end = nums.length-1;
14+
let index = start+1;
15+
while(index<end)
16+
{
17+
18+
let sum = nums[start]+nums[index] + nums[end];
19+
if(sum == 0 )
20+
{
21+
let newArray = [nums[start],nums[index],nums[end]];
22+
result.set(newArray.join(''),newArray);
23+
while(nums[index]==nums[index+1])
24+
{
25+
index++;
26+
}
27+
index++;
28+
continue;
29+
}
30+
if(sum >0)
31+
{
32+
while(nums[end]==nums[end-1])
33+
{
34+
end --;
35+
}
36+
end = end-1;
37+
}
38+
else
39+
{
40+
while(nums[index]==nums[index+1])
41+
{
42+
index++;
43+
}
44+
index++;
45+
}
46+
}
47+
start = start+1;
48+
}
49+
let resultArray = new Array();
50+
result.forEach(element => {
51+
resultArray.push(element);
52+
53+
});
54+
55+
return resultArray;
56+
57+
};
58+
59+
let nums = [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6];
60+
console.log(threeSum(nums));

Week_01/id_13/trap.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var trap = function(height) {
6+
if (height.length<2) return 0;
7+
let head = 0;
8+
let tail = head+1;
9+
let totalArea=0;
10+
let temp=0;
11+
while(tail<height.length)
12+
{
13+
temp = temp+height[tail-1];
14+
if(height[tail]>height[head])
15+
{
16+
let a= (tail-head) * height[head] - temp;
17+
totalArea += a;
18+
temp=0;
19+
head= tail;
20+
}
21+
tail ++;
22+
}
23+
head = height.length-1;
24+
tail = head-1;
25+
temp = 0;
26+
while(tail >=0 )
27+
{
28+
temp = temp+height[tail+1];
29+
if(height[tail]>=height[head])
30+
{
31+
let a= (head-tail)*height[head]-temp;
32+
totalArea += a;
33+
temp=0;
34+
head= tail;
35+
}
36+
tail --;
37+
}
38+
return totalArea;
39+
};
40+
41+
let height=[2,0,2];
42+
console.log(trap(height));

0 commit comments

Comments
 (0)