Skip to content

Commit b517b1a

Browse files
author
Chihung Yu
committed
update
1 parent dbcd817 commit b517b1a

File tree

4 files changed

+64
-3
lines changed

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(var i = 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-> rather tan 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-> rather than 1 1 1
37+
var target = 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
return numChrRead;
6464
};
6565
};
66+
67+
var solution = 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+
var read4Buff = [];
74+
var read4NumCharRead = 0;
75+
var read4Remain = 0;
76+
77+
return function(buf, n) {
78+
var numCharRead = 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+
return numCharRead;
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
var curStr;
6666
if (i - start > maxLen) {
67-
curStr = s.substring(start, i);
6867
maxLen = i - start;
6968
}
7069
var leftMost = 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+
var plusOne = function(digits) {
10+
var carry = 1;
11+
for(var i = digits.length - 1; i > -1; i--) {
12+
var d = digits[i];
13+
var sum = 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+
return digits;
29+
};
30+
31+
32+
33+
534
var plusOne = function(digits) {
635
for(var i = digits.length; i--;){
736
digits[i] = 1 + digits[i];

0 commit comments

Comments
 (0)