Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9eee2f

Browse files
author
zongyanqi
committed
add 006 007 009 016 017 018 022 023 024 027
1 parentf93e55d commitc9eee2f

10 files changed

+472
-0
lines changed

‎006-ZigZag-Conversion.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/zigzag-conversion/description/
4+
* Difficulty:Medium
5+
*
6+
* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
7+
* (you may want to display this pattern in a fixed font for better legibility)
8+
*
9+
* P A H N
10+
* A P L S I I G
11+
* Y I R
12+
* And then read line by line: "PAHNAPLSIIGYIR"
13+
* Write the code that will take a string and make this conversion given a number of rows:
14+
* string convert(string text, int nRows);
15+
* convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
16+
*
17+
*/
18+
19+
/**
20+
*@param {string} s
21+
*@param {number} numRows
22+
*@return {string}
23+
*/
24+
varconvert=function(s,numRows){
25+
26+
vararr=[];
27+
for(vari=0;i<numRows;i++){
28+
arr[i]=[];
29+
}
30+
31+
varcnt=0;
32+
varlen=s.length;
33+
34+
while(cnt<len){
35+
for(vari=0;i<arr.length&&cnt<len;i++){
36+
arr[i].push(s[cnt++]);
37+
}
38+
for(vari=numRows-2;i>=1&&cnt<len;i--){
39+
arr[i].push(s[cnt++]);
40+
}
41+
}
42+
// console.log(arr);
43+
44+
returnarr.map(arr=>arr.join('')).join('')
45+
};
46+
47+
console.log(convert('PAYPALISHIRING',3));

‎007-Reverse-Integer.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-integer/description/
3+
* Difficulty:Easy
4+
*
5+
* Given a 32-bit signed integer, reverse digits of an integer.
6+
*
7+
* Example 1:
8+
* Input: 123
9+
* Output: 321
10+
* Example 2:
11+
* Input: -123
12+
* Output: -321
13+
* Example 3:
14+
* Input: 120
15+
* Output: 21
16+
* Note:
17+
* Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range.
18+
* For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
19+
*/
20+
21+
/**
22+
*@param {number} x
23+
*@return {number}
24+
*/
25+
varreverse=function(x){
26+
27+
varsign=x>=0 ?-1 :1;
28+
x=Math.abs(x);
29+
30+
varsum=0;
31+
while(x){
32+
sum=sum*10+x%10;
33+
x=Math.floor(x/10);
34+
}
35+
varret=sign*-1*sum;
36+
varmax=Math.pow(2,31)-1;
37+
varmin=-Math.pow(2,31);
38+
if(ret>max)return0;
39+
if(ret<min)return0;
40+
returnret;
41+
};
42+
43+
console.log(reverse(123)===321);
44+
console.log(reverse(-123)===-321);
45+
console.log(reverse(120)===21);
46+
console.log(reverse(1534236469)===0);
47+
console.log(reverse(-2147483412),reverse(-2147483412)===-2143847412);
48+
console.log(reverse(-2147483648),reverse(-2147483648)===0);
49+

‎009-Palindrome-Number.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* https://leetcode.com/problems/palindrome-number/description/
3+
* Difficulty:Easy
4+
*
5+
* Determine whether an integer is a palindrome. Do this without extra space.
6+
*/
7+
8+
/**
9+
*@param {number} x
10+
*@return {boolean}
11+
*/
12+
varisPalindrome=function(x){
13+
if(x<0)returnfalse;
14+
vart=x;
15+
x=Math.abs(x);
16+
varp=0;
17+
while(x){
18+
p=p*10+x%10;
19+
x=Math.floor(x/10);
20+
}
21+
// console.log(x, p);
22+
returnt===p;
23+
};
24+
25+
console.log(isPalindrome(-1)===false);
26+
console.log(isPalindrome(0)===true);
27+
console.log(isPalindrome(123)===false);
28+
console.log(isPalindrome(12321)===true);
29+
console.log(isPalindrome(1221)===true);
30+
console.log(isPalindrome(2222)===true);
31+

‎016-3Sum-Closest.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* https://leetcode.com/problems/3sum-closest/description/
3+
* Difficulty:Medium
4+
*
5+
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
6+
* Return the sum of the three integers. You may assume that each input would have exactly one solution.
7+
* For example, given array S = {-1 2 1 -4}, and target = 1.
8+
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
9+
*
10+
*/
11+
12+
/**
13+
*@param {number[]} nums
14+
*@param {number} target
15+
*@return {number}
16+
*/
17+
varthreeSumClosest=function(nums,target){
18+
19+
varans=nums[0]+nums[1]+nums[2];
20+
varlen=nums.length;
21+
22+
nums.sort((a,b)=>a<b ?-1 :(a>b) ?1 :0);
23+
24+
for(vari=0;i<len-2;i++){
25+
varj=i+1;
26+
vark=len-1;
27+
28+
while(j<k){
29+
varsum=nums[i]+nums[j]+nums[k];
30+
if(sum===target)returnsum;
31+
if(sum>target)k--;
32+
if(sum<target)j++;
33+
if(Math.abs(target-sum)<Math.abs(target-ans)){
34+
ans=sum;
35+
}
36+
}
37+
}
38+
returnans;
39+
40+
};
41+
42+
console.log(threeSumClosest([-1,2,1,-4],1));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
4+
* Difficulty:Medium
5+
*
6+
* Given a digit string, return all possible letter combinations that the number could represent.
7+
*
8+
* A mapping of digit to letters (just like on the telephone buttons) is given below.
9+
*
10+
* Input:Digit string "23"
11+
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
12+
*
13+
* Note:
14+
* Although the above answer is in lexicographical order, your answer could be in any order you want.
15+
*
16+
*/
17+
18+
/**
19+
*@param {string} digits
20+
*@return {string[]}
21+
*/
22+
varletterCombinations=function(digits){
23+
24+
if(!digits.length)return[];
25+
26+
varans=[''];
27+
varmap=['0','1','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz'];
28+
29+
for(vari=0;i<digits.length;i++){
30+
varstr=map[digits[i]];
31+
vartmp=[];
32+
for(varj=0;j<ans.length;j++){
33+
vart=ans[j];
34+
for(vark=0;k<str.length;k++){
35+
tmp.push(t+str[k]);
36+
}
37+
}
38+
ans=tmp;
39+
}
40+
41+
returnans;
42+
};
43+
44+
console.log(letterCombinations('23'));

‎018-4Sum.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* https://leetcode.com/problems/4sum/description/
3+
* Difficulty:Medium
4+
*
5+
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
6+
* Note: The solution set must not contain duplicate quadruplets.
7+
* For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
8+
* A solution set is:
9+
* [
10+
* [-1, 0, 0, 1],
11+
* [-2, -1, 1, 2],
12+
* [-2, 0, 0, 2]
13+
]
14+
*/
15+
16+
/**
17+
*@param {number[]} nums
18+
*@param {number} target
19+
*@return {number[][]}
20+
*/
21+
varfourSum=function(nums,target){
22+
varlen=nums.length;
23+
if(len<4)return[];
24+
nums.sort((a,b)=>a<b ?-1 :a>b ?1 :0);
25+
// console.log(nums);
26+
varans=[];
27+
for(vari=0;i<len-3;i++){
28+
29+
for(varj=i+1;j<len-2;j++){
30+
vark=j+1;
31+
varl=len-1;
32+
33+
while(k<l){
34+
varsum=nums[i]+nums[j]+nums[k]+nums[l];
35+
if(sum===target){
36+
ans.push([nums[i],nums[j],nums[k],nums[l]]);
37+
while(nums[l--]===nums[l]&&nums[k++]===nums[k]&&k<l);
38+
}
39+
elseif(sum<target)while(nums[k++]===nums[k]&&k<l);
40+
elsewhile(nums[l--]===nums[l]&&k<l);
41+
}
42+
while(nums[j]===nums[j+1])j++;
43+
}
44+
while(nums[i]===nums[i+1])i++;
45+
46+
}
47+
returnans;
48+
49+
};
50+
51+
console.log(fourSum([-5,-4,-2,-2,-2,-1,0,0,1],-9));

‎022-Generate-Parentheses.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/generate-parentheses/description/
3+
* Difficulty:Medium
4+
*
5+
* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
6+
* For example, given n = 3, a solution set is:
7+
* [
8+
* "((()))",
9+
* "(()())",
10+
* "(())()",
11+
* "()(())",
12+
* "()()()"
13+
* ]
14+
*/
15+
16+
/**
17+
*@param {number} n
18+
*@return {string[]}
19+
*/
20+
vargenerateParenthesis=function(n){
21+
22+
varans=[];
23+
helper(ans,'',0,0,n);
24+
returnans;
25+
};
26+
27+
functionhelper(ans,str,left,right,n){
28+
if(right===n)ans.push(str);
29+
if(left<n){
30+
helper(ans,str+'(',left+1,right,n);
31+
}
32+
if(right<left){
33+
helper(ans,str+')',left,right+1,n);
34+
}
35+
}
36+
37+
console.log(generateParenthesis(3));

‎023-Merge-k-Sorted-Lists.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* https://leetcode.com/problems/merge-k-sorted-lists/description/
3+
* Difficulty:Hard
4+
*
5+
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
6+
*/
7+
8+
9+
//Definition for singly-linked list.
10+
functionListNode(val){
11+
this.val=val;
12+
this.next=null;
13+
}
14+
15+
/**
16+
*@param {ListNode[]} lists
17+
*@return {ListNode}
18+
*/
19+
varmergeKLists=function(lists){
20+
returnlists.reduce((a,b)=>merge2lists(a,b),null)
21+
};
22+
23+
functionmerge2lists(a,b){
24+
if(!a&&!b)returnnull;
25+
if(!a)returnb;
26+
if(!b)returna;
27+
varh;
28+
if(a.val<b.val){
29+
h=a;
30+
a=a.next;
31+
}else{
32+
h=b;
33+
b=b.next;
34+
}
35+
vart=h;
36+
37+
while(a&&b){
38+
if(a.val<b.val){
39+
t.next=a;
40+
t=t.next;
41+
a=a.next;
42+
}else{
43+
t.next=b;
44+
t=t.next;
45+
b=b.next;
46+
}
47+
}
48+
if(a)t.next=a;
49+
if(b)t.next=b;
50+
returnh;
51+
52+
}
53+
54+
vara={
55+
val:1,
56+
next:{
57+
val:4,
58+
next:{
59+
val:7,
60+
next:null
61+
}
62+
}
63+
}
64+
varb={
65+
val:2,
66+
next:{
67+
val:8,
68+
next:{
69+
val:9,
70+
next:null
71+
}
72+
}
73+
}
74+
75+
varc={
76+
val:3,
77+
next:{
78+
val:10,
79+
next:null
80+
}
81+
}
82+
83+
// console.log(merge2lists(a, b));
84+
console.log(mergeKLists([a,b,c]))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp