Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8934bd8

Browse files
committedAug 29, 2018
add scripts
1 parent d966f0c commit 8934bd8

5 files changed

+81
-12
lines changed
 
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} numbers
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
const twoSum = function(numbers, target) {
7+
const res = [];
8+
let remaining;
9+
let next = 0;
10+
for (let i = 0; i < numbers.length; i++) {
11+
remaining = target - numbers[i];
12+
next = i + 1;
13+
while (next < numbers.length && numbers[next] <= remaining) {
14+
if (numbers[next] === remaining) {
15+
res.push(i + 1, next + 1);
16+
break;
17+
}
18+
next += 1;
19+
}
20+
}
21+
22+
return res;
23+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const firstUniqChar = function(s) {
6+
const arr = [];
7+
const res = [];
8+
const hash = {};
9+
let tmp;
10+
let idx;
11+
for (let i = 0; i < s.length; i++) {
12+
tmp = s.charAt(i);
13+
if (hash.hasOwnProperty(tmp)) {
14+
idx = arr.indexOf(tmp);
15+
if (idx >= 0) {
16+
arr.splice(idx, 1);
17+
res.splice(idx, 1);
18+
}
19+
20+
hash[tmp] += 1;
21+
} else {
22+
arr.push(tmp);
23+
res.push(i);
24+
hash[tmp] = 1;
25+
}
26+
}
27+
return res[0] == null ? -1 : res[0];
28+
};

‎485-max-consecutive-ones.js

100644100755
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
* @return {number}
44
*/
55
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-
}
6+
let sum = 0,
7+
max = 0;
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
let temp = sum;
11+
sum += nums[i];
12+
if (temp === sum || i === nums.length - 1) {
13+
max = Math.max(sum, max);
14+
sum = 0;
1515
}
16-
17-
return max;
18-
}
16+
}
17+
18+
return max;
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} prices
3+
* @param {number} fee
4+
* @return {number}
5+
*/
6+
const maxProfit = function(prices, fee) {
7+
let cash = 0,
8+
hold = -prices[0];
9+
for (let i = 1; i < prices.length; i++) {
10+
cash = Math.max(cash, hold + prices[i] - fee);
11+
hold = Math.max(hold, cash - prices[i]);
12+
}
13+
return cash;
14+
};
15+
16+
console.log(maxProfit([1, 3, 2, 8, 4, 9], 2));

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### JavaScript solutions of LeetCode problems

0 commit comments

Comments
 (0)
Please sign in to comment.