Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea59335

Browse files
update 5 leetcode
1 parent 764629c commit ea59335

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

‎leetcode/191.number-of-1-bits.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode id=191 lang=javascript
3+
*
4+
* [191] Number of 1 Bits
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 按位与的思路,一直翻转翻转
10+
* @param {number} n - a positive integer
11+
* @return {number}
12+
*/
13+
var hammingWeight = function(n) {
14+
let res = 0;
15+
while (n) {
16+
n &= n - 1;
17+
res++;
18+
}
19+
return res;
20+
};
21+
// @lc code=end

‎leetcode/205.isomorphic-strings.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=205 lang=javascript
3+
*
4+
* [205] Isomorphic Strings
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 维护哈希表,属性值对
10+
* 如果不匹配,就返回 false
11+
* @param {string} s
12+
* @param {string} t
13+
* @return {boolean}
14+
*/
15+
var isIsomorphic = function(s, t) {
16+
let mapS = {};
17+
let mapT = {};
18+
for (let i = 0; i < s.length; i++) {
19+
const si = s[i];
20+
const ti = t[i];
21+
// "badc" "baba" 因为有这种场景,所以需要保存两者的键值对来比较
22+
if ((mapS[si] && mapS[si] !== ti) || (mapT[ti] && mapT[ti] !== si)) {
23+
return false;
24+
}
25+
mapS[si] = ti;
26+
mapT[ti] = si;
27+
}
28+
return true;
29+
};
30+
// @lc code=end

‎leetcode/231.power-of-two.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=231 lang=javascript
3+
*
4+
* [231] Power of Two
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} n
10+
* @return {boolean}
11+
*/
12+
var isPowerOfTwo = function(n) {
13+
if (n <= 1) {
14+
return n >= 0 ? !!n : false;
15+
}
16+
17+
while (n > 1) {
18+
if (n % 2 !== 0) {
19+
return false;
20+
}
21+
n /= 2;
22+
}
23+
return true;
24+
};
25+
26+
/**
27+
* 按位与的思路,二进制不同位都是 1 才会是 1
28+
* 而 2 的幂的二进制除了首位是 1 ,其他位都是 0
29+
* @param {*} n
30+
* @returns
31+
*/
32+
var isPowerOfTwoBetter = function(n) {
33+
return n > 0 && (n & (n - 1)) === 0;
34+
};
35+
36+
// @lc code=end

‎leetcode/258.add-digits.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=258 lang=javascript
3+
*
4+
* [258] Add Digits
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} num
10+
* @return {number}
11+
*/
12+
var addDigits = function(num) {
13+
num = num.toString(); // 38
14+
let sum = 0;
15+
// 如果超过一位则继续往下走
16+
while (num.length > 1) {
17+
// 叠加位数
18+
for (let s of num) {
19+
sum += +s;
20+
console.info('sum', s, sum, num);
21+
}
22+
// 注意处理的时候返回字符串,重置总的和
23+
num = sum.toString();
24+
sum = 0;
25+
}
26+
return +num;
27+
};
28+
29+
var addDigitsBetter = function(num) {
30+
// 数学课上讲过如果各位数相加等于 9 就一定能被 9 整除
31+
// 这里各位数相加得到最后的一位数之内,也肯定是 9 的余数
32+
// 但 9 的情况特别,先借位再补位
33+
return (num - 1) % 9 + 1;
34+
};
35+
addDigits(38);
36+
// @lc code=end

‎leetcode/342.power-of-four.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=342 lang=javascript
3+
*
4+
* [342] Power of Four
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 接着 2 的幂的按位与运算
10+
* 但是给出取反的二进制位
11+
* 1010101010...
12+
* 转成 16 进制提升可读性
13+
* @param {number} n
14+
* @return {boolean}
15+
*/
16+
var isPowerOfFour = function(n) {
17+
return n > 0 && (n & (n - 1)) === 0 && (n & 0xAAAAAAAA) === 0;
18+
};
19+
20+
/**
21+
* 4 的幂一定是 2 的幂
22+
* 二项式定理展开得知 4 的幂一定是除以 3 余数是 1
23+
* @param {*} n
24+
* @returns
25+
*/
26+
var isPowerOfFour2 = function(n) {
27+
return n > 0 && (n & (n - 1)) === 0 && (n % 3 === 1);
28+
};
29+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /