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

Commit272cb1c

Browse files
author
zongyanqi
committed
format filenames
1 parentb768505 commit272cb1c

File tree

82 files changed

+2824
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2824
-259
lines changed

‎001-Two-Sum.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/description/
3+
* Difficulty:Easy
4+
*
5+
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
* Example:
8+
* Given nums = [2, 7, 11, 15], target = 9,
9+
* Because nums[0] + nums[1] = 2 + 7 = 9,
10+
* return [0, 1].
11+
*/
12+
13+
/**
14+
*@param {number[]} numbers
15+
*@param {number} target
16+
*@return {number[]}
17+
*/
18+
vartwoSum=function(numbers,target){
19+
20+
for(vari=0;i<numbers.length-1;i++){
21+
for(varj=i+1;j<numbers.length;j++){
22+
if(numbers[i]+numbers[j]===target)return[i,j];
23+
}
24+
}
25+
};
26+
27+
vartwoSum2=function(numbers,target){
28+
varmap={};
29+
for(vari=0;i<numbers.length;i++){
30+
varn=numbers[i];
31+
if(map[target-n]!==undefined){
32+
return[map[target-n],i];
33+
}else{
34+
map[n]=i;
35+
}
36+
}
37+
};
38+
39+
console.log(twoSum([2,11,15,7],9));
40+
console.log(twoSum2([2,7,11,15],9));
41+
console.log(twoSum2([2,7,11,15],26));
42+
console.log(twoSum2([2,7,11,15],26));
43+

‎002-Add-Two-Numbers.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/add-two-numbers/description/
3+
* Difficulty:Medium
4+
*
5+
* 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.
6+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
7+
* Example
8+
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
* Output: 7 -> 0 -> 8
10+
* Explanation: 342 + 465 = 807.
11+
*/
12+
13+
// Definition for singly-linked list.
14+
functionListNode(val){
15+
this.val=val;
16+
this.next=null;
17+
}
18+
19+
/**
20+
*@param {ListNode} l1
21+
*@param {ListNode} l2
22+
*@return {ListNode}
23+
*/
24+
varaddTwoNumbers=function(l1,l2){
25+
26+
varc=0;
27+
varret=newListNode(0);
28+
varcurr=ret;
29+
30+
while(l1||l2){
31+
vara=l1 ?l1.val :0;
32+
varb=l2 ?l2.val :0;
33+
varsum=a+b+c;
34+
c=Math.floor(sum/10);
35+
curr.next=newListNode(sum%10);
36+
if(l1){
37+
l1=l1.next;
38+
}
39+
if(l2){
40+
l2=l2.next;
41+
}
42+
curr=curr.next;
43+
}
44+
if(c){
45+
curr.next=newListNode(c);
46+
}
47+
48+
returnret.next;
49+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a string, find the length of the longest substring without repeating characters.
6+
* Examples:
7+
* Given "abcabcbb", the answer is "abc", which the length is 3.
8+
* Given "bbbbb", the answer is "b", with the length of 1.
9+
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
10+
*
11+
*/
12+
13+
/**
14+
*@param {string} s
15+
*@return {number}
16+
*/
17+
varlengthOfLongestSubstring=function(s){
18+
19+
varmax=0;
20+
vari=0;
21+
varj=0;
22+
varn=s.length;
23+
varmap={};
24+
25+
while(i<n&&j<n){
26+
if(map[s[j]]===undefined){
27+
map[s[j]]=1;
28+
j++;
29+
max=Math.max(max,j-i);
30+
}else{
31+
deletemap[s[i]];
32+
i++;
33+
}
34+
35+
}
36+
37+
returnmax;
38+
};
39+
40+
console.log(lengthOfLongestSubstring('c'),1);
41+
console.log(lengthOfLongestSubstring(''),0);
42+
console.log(lengthOfLongestSubstring('abcabcbb'),3);
43+
console.log(lengthOfLongestSubstring('bbbbb'),1);
44+
console.log(lengthOfLongestSubstring('pwwkew'),3);
45+
console.log(lengthOfLongestSubstring('xhhyccrcbdczkvzeeubynglxfdedshtpobqsdhufkzgwuhaabdzrlkosnuxibrxssnkxuhcggkecshdvkcmymdqbxolbfjtzyfw'),14);

‎004-Median-of-Two-Sorted-Arrays.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* https://leetcode.com/problems/median-of-two-sorted-arrays/description/
3+
* Difficulty:Hard
4+
*
5+
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
6+
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
*
8+
* Example 1:
9+
* nums1 = [1, 3]
10+
* nums2 = [2]
11+
* The median is 2.0
12+
*
13+
* Example 2:
14+
* nums1 = [1, 2]
15+
* nums2 = [3, 4]
16+
* The median is (2 + 3)/2 = 2.5
17+
* *
18+
*/
19+
20+
21+
functionkth(arr1,s1,n1,arr2,s2,n2,k){
22+
// console.log(arr1, s1, n1, arr2, s2, n2, k);
23+
// console.log('-----------');
24+
if(k<1||k>n1+n2)return-1;
25+
26+
if(n1>n2){
27+
returnkth(arr2,s2,n2,arr1,s1,n1,k);
28+
}
29+
30+
if(n1===0){
31+
returnarr2[s2+k-1];
32+
}
33+
34+
if(k===1){
35+
returnarr1[s1]<arr2[s2] ?arr1[s1] :arr2[s2];
36+
}
37+
38+
varnewK=k>>1;
39+
40+
if(n1<newK){
41+
newK=n1;
42+
}
43+
44+
if(arr1[s1+newK-1]<arr2[s2+newK-1]){
45+
returnkth(arr1,s1+newK,n1-newK,arr2,s2,n2,k-newK);
46+
}else{
47+
returnkth(arr1,s1,n1,arr2,s2+newK,n2-newK,k-newK);
48+
}
49+
50+
}
51+
52+
// var arr1 = [2, 3, 6, 7, 9];
53+
// var arr2 = [1, 4, 8, 10];
54+
// console.log([...arr1, ...arr2].sort(function (a, b) {
55+
// if (a > b) return 1;
56+
// if (a < b) return -1;
57+
// return 0;
58+
// }));
59+
//
60+
// console.log('=======');
61+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 1), 1);
62+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 2), 2);
63+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 3), 3);
64+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 4), 4);
65+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 5), 6);
66+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 6), 7);
67+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 7), 8);
68+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 8), 9);
69+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 9), 10);
70+
71+
/**
72+
*@param {number[]} nums1
73+
*@param {number[]} nums2
74+
*@return {number}
75+
*/
76+
varfindMedianSortedArrays=function(nums1,nums2){
77+
78+
varn1=nums1.length;
79+
varn2=nums2.length;
80+
81+
varmid=Math.floor((n1+n2)/2);
82+
if((n1+n2)%2===0){
83+
return(kth(nums1,0,n1,nums2,0,n2,mid)+kth(nums1,0,n1,nums2,0,n2,mid+1))/2;
84+
}else{
85+
returnkth(nums1,0,n1,nums2,0,n2,mid+1);
86+
}
87+
};
88+
89+
console.log(findMedianSortedArrays([1,3,4],[2,5]));
90+
console.log(findMedianSortedArrays([1,3,4],[2,5,6]));

‎005-Longest-Palindromic-Substring.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindromic-substring/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a string s, find the longest palindromic substring in s.
6+
* You may assume that the maximum length of s is 1000.
7+
*
8+
* Example:
9+
* Input: "babad"
10+
* Output: "bab"
11+
* Note: "aba" is also a valid answer.
12+
*
13+
* Example:
14+
* Input: "cbbd"
15+
* Output: "bb"
16+
*/
17+
/**
18+
*@param {string} s
19+
*@return {string}
20+
*/
21+
varlongestPalindrome=function(s){
22+
vara=newDate();
23+
varn=s.length;
24+
varres='';
25+
vardp=[];
26+
while(dp.push(newArray(n).fill(-1))<n);
27+
// console.log(dp);
28+
29+
for(vari=n-1;i>=0;i--){
30+
for(varj=i;j<n;j++){
31+
dp[i][j]=s[i]===s[j]&&((j-i<3)||dp[i+1][j-1]);
32+
if(dp[i][j]===undefined){
33+
console.log(i,j,s[i],s[j],dp[i+1][j-1])
34+
}
35+
if(dp[i][j]){
36+
vartmp=s.substring(i,j+1);
37+
if(tmp.length>res.length)res=tmp;
38+
}
39+
40+
}
41+
}
42+
// console.log(dp);
43+
console.log(newDate()-a);
44+
45+
returnres;
46+
};
47+
48+
// console.log(isPalindrome(s, 1, 3));
49+
// console.log(longestPalindrome('babad'));
50+
// console.log(longestPalindrome(''));
51+
// console.log(longestPalindrome('a'));
52+
// console.log(longestPalindrome('aabbbbbb'));
53+
console.log(longestPalindrome("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));

‎011-Container-With-Most-Water.js

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎014-Longest-Common-Prefix.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* https://leetcode.com/problems/longest-common-prefix/
3+
* Difficulty:Easy
4+
*
5+
* Write a function to find the longest common prefix string amongst an array of strings.
6+
*/
7+
8+
/**
9+
*@param {string[]} strs
10+
*@return {string}
11+
*/
12+
varlongestCommonPrefix=function(strs){
13+
14+
varm=strs.length;
15+
if(!m)return'';
16+
17+
varmin=Infinity;
18+
varminIndex=-1;
19+
for(vari=0;i<strs.length;i++){
20+
if(strs[i].length<min){
21+
min=strs[i].length;
22+
minIndex=i;
23+
}
24+
}
25+
26+
vars=strs[minIndex];
27+
28+
for(i=0;i<s.length;i++){
29+
varch=strs[0][i];
30+
varsame=true;
31+
for(varj=1;j<m;j++){
32+
if(strs[j][i]!==ch){
33+
same=false;
34+
break;
35+
}
36+
37+
}
38+
if(!same)break;
39+
40+
}
41+
42+
returns.substr(0,i);
43+
44+
};
45+
46+
console.log(longestCommonPrefix([
47+
'abdc',
48+
'abc123',
49+
'abc234'
50+
]));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp