Skip to content

Commit 6095bd6

Browse files
author
Dream
committed
Merge remote-tracking branch 'origin/master'
2 parents ee2d02f + 410936f commit 6095bd6

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
|31|[Next Permutation](https://leetcode.com/problems/next-permutation/)| |Medium|
415415
|30|[Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/)| |Hard|
416416
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)| |Medium|
417-
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| |Easy|
417+
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| [js](./algorithms/implementStrstr/solution.js) |Easy|
418418
|27|[Remove Element](https://leetcode.com/problems/remove-element/)| [js](./algorithms/removeElement/removeElement.js) [python](./algorithms/removeElement/removeElement.python) |Easy|
419419
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [java](./algorithms/removeDuplicatesFromSortedArray/Solution.java), [js](./algorithms/removeDuplicatesFromSortedArray/Solution.js) |Easy|
420420
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| |Hard|
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 解法一:用 js 现成的 indexOf api
2+
var strStr = function(haystack, needle) {
3+
if(!needle) return 0;
4+
return haystack.indexOf(needle)
5+
};
6+
7+
8+
// TODO: 解法二:自己实现
9+
// var strStr = function(haystack, needle) {
10+
// // 不存在或者相等时,返回0
11+
// if (!needle || needle === haystack) return 0;
12+
// let h = 0, n = 0, index = 0;
13+
// while (h < haystack.length && haystack.length < needle.length) {
14+
// while (haystack[h] === needle[n] && n < needle.length) {
15+
// if (index === 0) {
16+
// index = h
17+
// }
18+
// n = n + 1;
19+
// }
20+
// if (n === needle.length) {
21+
// return index;
22+
// } else {
23+
// h = h + 1;
24+
// }
25+
// }
26+
// // 找不到或者要查找的字符串大于自己的时候,返回-1
27+
// return -1;
28+
// };
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var isPalindrome = function(x) {
2+
// 负数 0 10和10的倍数不可能为回文数
3+
if (x <= 0 || (!(x % 10) && x)) return false;
4+
let x2 = x, res = 0;
5+
while (x2) {
6+
res = res * 10 + x2 % 10;
7+
// x2 = ~~(x2 / 10);
8+
// ~~ 运算符处理大于32位数值时,为负数,可以换成Math.floor()
9+
x2 = Math.floor(x2 / 10);
10+
}
11+
12+
return res === x;
13+
};

algorithms/jcc-addTwoNumbers/index.html renamed to algorithms/longestCommonPrefix-jcc/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
<body>
1010

1111
</body>
12-
<script src="./addTwoNumbers.js"></script>
12+
<script src="./longestCommonPrefix.js"></script>
1313
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var longestCommonPrefix = function(strs) {
2+
if(strs.length == 0) return "";
3+
let str = strs[0]; // 取第一个用来做基准比较
4+
for(let i = 1; i<strs.length; i++) { // 不需要跟自己比较,从1开始
5+
let j = 0;
6+
for(;j<str.length && j<strs[i].length; j++) {
7+
if(str[j] != strs[i][j]) break; // 有不想等的就跳出循环
8+
}
9+
str = str.substr(0, j); // 截取相等的部分
10+
if(str === "") return str;
11+
}
12+
return str;
13+
};
14+
15+
console.log(longestCommonPrefix(["flower","flow","flight"]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class TwoCityScheduling {
2+
public int twoCitySchedCost(int[][] costs) {
3+
int sum = 0;
4+
int[] ar = new int[2000];
5+
for (int[] iArray : costs) {
6+
sum = sum + iArray[0];
7+
ar[iArray[1] - iArray[0] +1000]++;
8+
}
9+
int index = 0;
10+
int i = 0;
11+
while (i < costs.length / 2) {
12+
while (ar[index] > 0 && i<costs.length/2) {
13+
sum = sum + (index -1000);
14+
ar[index]--;
15+
i++;
16+
}
17+
index++;
18+
}
19+
return sum;
20+
}
21+
}

0 commit comments

Comments
 (0)