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

Commitb517b1a

Browse files
author
Chihung Yu
committed
update
1 parentdbcd817 commitb517b1a

4 files changed

+64
-3
lines changed

‎15 3Sum.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var threeSum = function(nums) {
3333

3434
for(vari=0;i<len-2;i++){
3535

36-
if(i===0||nums[i]>nums[i-1]){// very important, same as line 40, remove duplicate as 111 will only run once 1-> rathertan 1 1 1
37-
target=0-nums[i];
36+
if(i===0||nums[i]>nums[i-1]){// very important, same as line 40, remove duplicate as 111 will only run once 1-> ratherthan 1 1 1
37+
vartarget=0-nums[i];
3838

3939
j=i+1;
4040
k=len-1;

‎158 Read N Characters Give Read4 II - Call Multiple Times.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,36 @@ var solution = function(read4) {
6363
returnnumChrRead;
6464
};
6565
};
66+
67+
varsolution=function(read4){
68+
/**
69+
*@param {character[]} buf Destination buffer
70+
*@param {number} n Number of characters to read
71+
*@return {number} The number of actual characters read
72+
*/
73+
varread4Buff=[];
74+
varread4NumCharRead=0;
75+
varread4Remain=0;
76+
77+
returnfunction(buf,n){
78+
varnumCharRead=0;
79+
80+
// Need to run read4 N times to get n char
81+
while(numCharRead<n){
82+
// If everything is already read in read4 buffer, re-read
83+
if(read4NumCharRead===read4Remain){
84+
read4NumCharRead=0;
85+
read4Remain=read4(read4Buff);
86+
}
87+
while(read4NumCharRead<read4Remain&&numCharRead<n){
88+
buf[numCharRead++]=read4Buff[read4NumCharRead++];
89+
}
90+
if(read4Remain<4){
91+
break;
92+
}
93+
94+
}
95+
96+
returnnumCharRead;
97+
};
98+
};

‎159 Longest Substring with At Most Two Disctinct Characters.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ var lengthOfLongestSubstringTwoDistinct = function(s) {
6464
if(map.size===2&&map.get(c)===undefined){
6565
varcurStr;
6666
if(i-start>maxLen){
67-
curStr=s.substring(start,i);
6867
maxLen=i-start;
6968
}
7069
varleftMost=s.length;

‎66 Plus One.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@
22
*@param {number[]} digits
33
*@return {number[]}
44
*/
5+
6+
// Time complexity : \mathcal{O}(N)O(N) since it's not more than one pass along the input list.
7+
8+
// Space complexity : \mathcal{O}(1)O(1).
9+
varplusOne=function(digits){
10+
varcarry=1;
11+
for(vari=digits.length-1;i>-1;i--){
12+
vard=digits[i];
13+
varsum=d+carry;
14+
if(sum===10){
15+
digits[i]=0;
16+
carry=1;
17+
}else{
18+
digits[i]=sum;
19+
carry=0;// can directly return since it will not trigger the carry unshift at the end.
20+
break;
21+
}
22+
}
23+
24+
if(carry===1){
25+
digits.unshift(carry);
26+
}
27+
28+
returndigits;
29+
};
30+
31+
32+
33+
534
varplusOne=function(digits){
635
for(vari=digits.length;i--;){
736
digits[i]=1+digits[i];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp