Skip to content

Commit be97ede

Browse files
committed
add more solution
1 parent 523e7df commit be97ede

10 files changed

+195
-0
lines changed

021_merge_two_sorted_lists.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const mergeLists = function (attr1, attr2) {
2+
3+
};

026_remove_duplicate_from_array.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const removeDuplicates = function(nums) {
2+
let len = 0;
3+
for (let i = 0; i<nums.length; i++) {
4+
if (nums[i] !== nums[i-1]) {
5+
nums[len++] = nums[i]
6+
}
7+
}
8+
return len;
9+
};
10+
11+
12+
const nums = [1,1,2];
13+
14+
console.log(removeDuplicates(nums));

027_remove_element.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const removeElement = function(nums, val) {
2+
for (var i = 0; i < nums.length; i += 1) {
3+
if (nums[i] === val) {
4+
nums.splice(i, 1);
5+
i--;
6+
}
7+
}
8+
return nums
9+
};
10+
11+
const nums = [3,2,2,3];
12+
13+
console.log(removeElement(nums, 3));

028_implement_strStr.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function(haystack, needle) {
7+
const needleLen = needle.length;
8+
const stringLen = haystack.length;
9+
for (let i =0; i<stringLen; i++) {
10+
let sub = haystack[i];
11+
for (let j=1; j<needleLen; j++) {
12+
sub += haystack[i+j];
13+
}
14+
15+
if (sub === needle) {
16+
return i;
17+
}
18+
}
19+
20+
return needle ? -1 : 0;
21+
};
22+
23+
24+
/**
25+
* This solution is much more faster
26+
* @param haystack
27+
* @param needle
28+
* @returns {number}
29+
*/
30+
const strStr2 = function (haystack, needle) {
31+
if(needle) {
32+
var match = new RegExp(needle).exec(haystack);
33+
return match ? match.index : -1;
34+
} else {
35+
return 0;
36+
}
37+
};

035_search_insert_position.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
const searchInsert = function(nums, target) {
7+
for (let i = 0; i<nums.length; i++) {
8+
if (nums[i]>=target) {
9+
return i;
10+
}
11+
}
12+
13+
return nums.length;
14+
};
15+
16+
17+
const nums = [1,3,5,6];
18+
19+
console.log(searchInsert(nums, 7));

038_count_and_say.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const countAndSay = function (n) {
2+
let str = '1';
3+
while (n > 1) {
4+
let num = 1;
5+
let temp = '';
6+
for (let i = 0; i < str.length; i++) {
7+
if (str[i] === str[i+1]) {
8+
num ++;
9+
} else{
10+
temp += (num + '' + str[i]);
11+
num = 1;
12+
}
13+
}
14+
15+
str = temp;
16+
n--;
17+
}
18+
19+
return str;
20+
};
21+
22+
console.log(countAndSay(10));

053_maximum_subarray.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* //todo Hard
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
const maxSubArray = function(nums) {
7+
let max = nums[0];
8+
let total = nums[0];
9+
for (let i = 1; i<nums.length; i ++) {
10+
total = Math.max(nums[i], total + nums[i]);
11+
max = Math.max(max, total);
12+
console.log(total + ' | ' + max);
13+
}
14+
15+
return max;
16+
};
17+
18+
const maxSubArray2 = function(nums) {
19+
let max = nums[0];
20+
let total = nums[0];
21+
const result = [nums[0]];
22+
23+
for (let i = 1; i<nums.length; i ++) {
24+
}
25+
26+
return max;
27+
};
28+
29+
const nums = [-2,1,-3,4,-1,2,1,-5,4];
30+
31+
console.log(maxSubArray(nums));

058_length_of_last_word.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const lengthOfLastWord = function(s) {
6+
const arr = s.trim().split(' ');
7+
return arr[arr.length-1].length;
8+
};
9+
10+
const str = "a ";
11+
12+
console.log(lengthOfLastWord(str));

066_plus_one.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} digits
3+
* @return {number[]}
4+
*/
5+
const plusOne = function(digits) {
6+
7+
for (let i = (digits.length-1); i>=0; i--) {
8+
const lastDigit = digits[i];
9+
if (lastDigit !==9) {
10+
digits[i]++;
11+
return digits;
12+
} else {
13+
digits[i] = 0
14+
}
15+
}
16+
return [1,...digits];
17+
}
18+
19+
20+
const num = [0];
21+
22+
console.log(plusOne(num));

088_merge_sorted_array.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* a = b--, assign b'value to a, and b minutes 1
3+
* a = --b, b minutes 1 firstly, and then assign value to a
4+
*/
5+
6+
const merge = function(nums1, m, nums2, n) {
7+
let len = (m--) + (n--);
8+
while (n >= 0) {
9+
nums1[--len] = nums1[m] >= nums2[n] ? nums1[m--] : nums2[n--];
10+
}
11+
};
12+
13+
14+
15+
const nums1 = [1,2,3];
16+
const nums2 = [2,5,6];
17+
18+
const m = 3;
19+
const n = 3;
20+
merge(nums1, m, nums2, n);
21+
22+
console.log(nums1);

0 commit comments

Comments
 (0)