Skip to content

Commit 8924994

Browse files
committed
add 260, 485, 856 scripts.
1 parent a58fc6c commit 8924994

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

260-single-number-III.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const singleNumber = function(nums) {
6+
const hash = {}
7+
nums.forEach((el, idx) => {
8+
if(hash.hasOwnProperty(el)) {
9+
hash[el] += 1
10+
delete hash[el]
11+
} else {
12+
hash[el] = 1
13+
}
14+
})
15+
return Object.keys(hash).map(el => +el)
16+
};

485-max-consecutive-ones.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const findMaxConsecutiveOnes = function(nums) {
6+
let sum = 0, max = 0;
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
let temp = sum;
10+
sum += nums[i];
11+
if (temp === sum || i === nums.length - 1) {
12+
max = Math.max(sum, max);
13+
sum = 0;
14+
}
15+
}
16+
17+
return max;
18+
}

856-score-of-parentheses.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} S
3+
* @return {number}
4+
*/
5+
const scoreOfParentheses = function(S) {
6+
let res = 0, bal = 0;
7+
for(let i = 0; i < S.length; i++) {
8+
if(S.charAt(i) === '(') {
9+
bal += 1
10+
} else {
11+
bal -= 1
12+
if(S.charAt(i - 1) === '(') {
13+
res += 1 << bal
14+
}
15+
}
16+
}
17+
return res
18+
};

0 commit comments

Comments
 (0)