Skip to content

Commit bb0da02

Browse files
committed
add: Multiply Strings
1 parent f5eb551 commit bb0da02

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
1111
|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [JavaScript](./src/two-sum/res.js)|Easy|
1212
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [JavaScript](./src/add-two-numbers/res.js)|Medium|
1313
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [JavaScript](./src/longest-substring-without-repeating-characters/res.js) |Medium|
14+
|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [JavaScript](./src/longest-palindromic-substring/res.js) |Medium|
1415
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [JavaScript](./src/zigzag-conversion/res.js) |Medium|
1516
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [JavaScript](./src/reverse-integer/res.js)|Easy|
1617
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [JavaScript](./src/string-to-integer-atoi/res.js)|Medium|
@@ -25,6 +26,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2526
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [JavaScript](./src/divide-two-integers/res.js)|Medium|
2627
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
2728
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [JavaScript](./src/search-for-a-range/res.js)|Medium|
29+
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [JavaScript](./src/multiply-strings/res.js)|Medium|
2830
|46|[Permutations](https://leetcode.com/problems/permutations/) | [JavaScript](./src/permutations/res.js)|Medium|
2931
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js)|Medium|
3032
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-05-10 17:35:02
5+
*
6+
* @param {string} s
7+
* @return {string}
8+
*/
9+
let longestPalindrome = function(s) {
10+
let len = s.length,
11+
end = len-1,
12+
sarray = s.split('');
13+
14+
if(len<2) {
15+
return s;
16+
}
17+
18+
let resfrom = 0,
19+
resto = 0;
20+
for(let i=0; i<end; i++) {
21+
expandPalindrome(i,i);
22+
if (sarray[i] === sarray[i+1]) {
23+
expandPalindrome(i,i+1);
24+
}
25+
}
26+
27+
return s.substring(resfrom, resto+1);
28+
29+
function expandPalindrome(start, stop) {
30+
while (start>0 && stop<end) {
31+
if (sarray[start-1] === sarray[stop+1]) {
32+
start--;
33+
stop++;
34+
} else {
35+
break;
36+
}
37+
}
38+
39+
if (stop-start>resto-resfrom) {
40+
resfrom = start;
41+
resto = stop;
42+
}
43+
}
44+
};

src/multiply-strings/res.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-05-10 18:08:05
5+
*
6+
* @param {string} num1
7+
* @param {string} num2
8+
* @return {string}
9+
*/
10+
11+
let multiply = function(num1, num2) {
12+
let len1 = num1.length,
13+
len2 = num2.length,
14+
maxlen = len1 > len2 ? len1 : len2,
15+
res = [0],
16+
base = num1.split('').map(e => Number.parseInt(e)),
17+
shift = 0;
18+
19+
// console.log(base);
20+
for (let i = len2 - 1; i >= 0; i--) {
21+
let tmp = [], // 存储相乘和数组,倒序
22+
carrybit = 0,
23+
mpler = Number.parseInt(num2.charAt(i));
24+
25+
if (mpler === 0) {
26+
shift++;
27+
continue;
28+
}
29+
for (let j = len1 - 1; j >= 0; j--) {
30+
let prd = base[j] * mpler + carrybit;
31+
carrybit = Math.floor(prd / 10);
32+
tmp.push(prd % 10);
33+
}
34+
if (carrybit) tmp.push(carrybit);
35+
let tmplen = tmp.length + shift;
36+
// 补0操作
37+
for (let j = tmplen - res.length; j > 0; j--) {
38+
res.push(0);
39+
}
40+
41+
// 相加操作
42+
tmplen = tmp.length;
43+
carrybit = 0;
44+
for (let j = 0; j < tmplen; j++) {
45+
// console.log(res[j+shift], tmp[j], carrybit);
46+
let current = res[j + shift] + tmp[j] + carrybit;
47+
res[j + shift] = current % 10;
48+
// console.log(current);
49+
if (current > 9) {
50+
carrybit = 1;
51+
} else {
52+
carrybit = 0;
53+
}
54+
}
55+
if (carrybit) res.push(1);
56+
57+
// console.log(res, carrybit);
58+
shift++;
59+
}
60+
61+
while (res[res.length - 1] === 0 && res.length > 1) {
62+
res.pop();
63+
}
64+
65+
return res.reverse().join('');
66+
};

src/zigzag-conversion/res.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @date 2017-05-10 16:32:48
55
*
66
* Definition of Zigzag: https://en.wikipedia.org/wiki/Zigzag
7-
*
7+
* 本题的另一种解法: 每行元素之间下标大小相隔 2n-2, n=numRows
88
* @param {string} s
99
* @param {number} numRows
1010
* @return {string}

0 commit comments

Comments
 (0)