Skip to content

Commit 9088b1a

Browse files
authored
Merge pull request #64 from limbowandering/master
add javaScript solutions
2 parents 08b2ca4 + 6e1eb5c commit 9088b1a

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//beat 88%
2+
const merge1 = function(nums1, m, nums2, n){
3+
const arr = nums1.slice(0,m);
4+
let i = 0, j = 0;
5+
let count = 0;
6+
while(i < m && j < n){
7+
if(arr[i] <= nums2[j]){
8+
nums1[count++] = arr[i++];
9+
}else{
10+
nums1[count++] = nums2[j++];
11+
}
12+
}
13+
while(i < m){
14+
nums1[count++] = arr[i++];
15+
}
16+
while(j < n){
17+
nums1[count++] = nums2[j++];
18+
}
19+
};
20+
21+
//beat 30%....
22+
const merge = function(nums1, m, nums2,n){
23+
let index = m + n - 1;
24+
let aindex = m - 1;
25+
let bindex = n - 1;
26+
while(aindex >= 0 && bindex >= 0){
27+
if(nums1[aindex] > nums2[bindex]){
28+
nums1[index--] = nums1[aindex--];
29+
}else{
30+
nums1[index--] = nums2[bindex--];
31+
}
32+
}
33+
while(aindex >= 0){
34+
nums1[index--] = nums1[aindex--];
35+
}
36+
while(bindex >= 0){
37+
nums1[index--] = nums2[bindex--];
38+
}
39+
};

solution/204.Count Primes/Solution.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 600ms多
2+
const countPrimes2 = function(n){
3+
let arr = [];
4+
let res = 0;
5+
for(let i = 2; i < n; i++){
6+
if(arr[i] === undefined){
7+
arr[i] = 1;
8+
res++;
9+
for(let j = 2; i * j < n; j++){
10+
arr[i * j] = 0;
11+
}
12+
}
13+
}
14+
return res;
15+
};
16+
17+
//200ms多
18+
const countPrimes = function(n){
19+
let arr = [];
20+
let res = 0;
21+
for(let i = 2; i < n; i++){
22+
if(arr[i] === undefined){
23+
arr[i] = 1;
24+
res++;
25+
for(let j = i; i * j < n; j++){
26+
arr[i * j] = 0;
27+
}
28+
}
29+
}
30+
return res;
31+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for isBadVersion()
3+
*
4+
* @param {integer} version number
5+
* @return {boolean} whether the version is bad
6+
* isBadVersion = function(version) {
7+
* ...
8+
* };
9+
*/
10+
11+
/**
12+
* @param {function} isBadVersion()
13+
* @return {function}
14+
*/
15+
const solution = function(isBadVersion) {
16+
/**
17+
* @param {integer} n Total versions
18+
* @return {integer} The first bad version
19+
*/
20+
return function(n) {
21+
if(n === 1) return n;
22+
let left = 1, right = n;
23+
while(left < right){
24+
let mid = left + Math.floor((right-left)/2);
25+
if(isBadVersion(mid)){
26+
if(mid === left) return mid;
27+
else right = mid;
28+
}else{
29+
if(isBadVersion(mid+1)) return mid+1;
30+
else left = mid + 1;
31+
}
32+
}
33+
};
34+
};

0 commit comments

Comments
 (0)