Skip to content

Commit 69f14c6

Browse files
author
谭佳胜
committed
submit
1 parent b8f7e40 commit 69f14c6

File tree

28 files changed

+53
-0
lines changed

28 files changed

+53
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

227. Basic Calculator II/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const operate = function(a, b, operator) {
2+
switch (operator) {
3+
case '+':
4+
return a + b;
5+
case '-':
6+
return a - b;
7+
case '*':
8+
return a * b;
9+
case '/':
10+
return parseInt(a / b);
11+
default:
12+
return;
13+
}
14+
}
15+
const calculate = function(s) {
16+
const reg = /^(\s?\-?\d+\s?)(\+|\-|\*|\/)(\s?\d+\s?)(\+|\-|\*|\/)?(\s?\d+)?/;
17+
const operatorReg = /\+|\-|\*|\//;
18+
19+
while (+s !== +s) {
20+
s = s.replace(reg, (match, $1, $2, $3, $4, $5) => {
21+
if (!$4 && !$5) {
22+
return operate(+$1, +$3, $2);
23+
} else {
24+
if (/\*|\//.test($4) && !/\*|\//.test($2)) {
25+
return `${$1}${$2}${operate(+$3, +$5, $4)}`;
26+
} else {
27+
return `${operate(+$1, +$3, $2)}${$4}${$5}`;
28+
}
29+
}
30+
});
31+
}
32+
33+
return +s;
34+
};

258. Add Digits/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const addDigits = function(num) {
2+
while (num >= 10) {
3+
num = String(num).split('').reduce((pre, cur) => Number(pre) + Number(cur))
4+
}
5+
6+
return num;
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const removeDuplicates = function(nums) {
2+
let count = 0;
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (nums[i] !== nums[i + 1]) {
6+
count++;
7+
nums[count] = nums[i + 1]
8+
}
9+
}
10+
11+
return count;
12+
};

0 commit comments

Comments
 (0)