forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
luzhipeng
committed
May 17, 2019
1 parent
c25465e
commit e89b64e
Showing
11 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* @lc app=leetcode id=172 lang=javascript | ||
* | ||
* [172] Factorial Trailing Zeroes | ||
*/ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var trailingZeroes = function(n) { | ||
// tag: 数论 | ||
// 只有 2 和 5 相乘才等于 10 | ||
// if (n === 0) return n; | ||
|
||
// return Math.floor(n / 5) + trailingZeroes(Math.floor(n / 5)); | ||
let count = 0; | ||
while(n >= 5) { | ||
count += Math.floor(n / 5); | ||
n = Math.floor(n / 5); | ||
} | ||
return count; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* @lc app=leetcode id=263 lang=javascript | ||
* | ||
* [263] Ugly Number | ||
*/ | ||
/** | ||
* @param {number} num | ||
* @return {boolean} | ||
*/ | ||
var isUgly = function(num) { | ||
// TAG: 数论 | ||
if (num <= 0) return false; | ||
if (num === 1) return true; | ||
|
||
const list = [2, 3, 5]; | ||
|
||
if (list.includes(num)) return true; | ||
|
||
for (let i of list) { | ||
if (num % i === 0) return isUgly(Math.floor(num / i)); | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* @lc app=leetcode id=342 lang=javascript | ||
* | ||
* [342] Power of Four | ||
*/ | ||
/** | ||
* @param {number} num | ||
* @return {boolean} | ||
*/ | ||
var isPowerOfFour = function(num) { | ||
// tag: 数论 | ||
|
||
// 100 | ||
// 10000 | ||
// 1000000 | ||
|
||
// 发现规律: 4的幂次方的二进制表示 1 的位置都是在奇数位(且不在最低位),其他位置都为0 | ||
|
||
// 10 | ||
// 100 | ||
// 1000 | ||
|
||
// 发现规律: 2的幂次方的特点是最低位之外,其他位置有且仅有一个1 | ||
|
||
// 如果满足: | ||
// 1. 是 2 的幂次方, 就能保证最低位之外,其他位置有且仅有一个1 | ||
// 我们还需要保证 | ||
// 2. 这个1不再偶数位置,一定在奇数位置就行了 | ||
|
||
// 我们可以取一个特殊数字,这个特殊数字,奇数位置都是1,偶数位置都是0,然后和这个特殊数字 | ||
// `求与`, 如果等于本身,那么毫无疑问,这个1不再偶数位置,一定在奇数位置 | ||
// 因为如果在偶数位置,`求与`的结果就是0了 | ||
// 特殊的数字: | ||
|
||
// 01010101010101010101010101010101 | ||
|
||
if (num === 1) return true; | ||
if (num < 4) return false; | ||
|
||
if ((num & (num - 1)) !== 0) return false; | ||
|
||
return (num & 0x55555555) === num; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* @lc app=leetcode id=575 lang=javascript | ||
* | ||
* [575] Distribute Candies | ||
*/ | ||
/** | ||
* @param {number[]} candies | ||
* @return {number} | ||
*/ | ||
var distributeCandies = function(candies) { | ||
const count = new Set(candies); | ||
return Math.min(count.size, candies.length >> 1); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,8 @@ eg : | |
|
||
3. 双"指针" 模型 | ||
|
||
4. bit 运算 | ||
|
||
|
||
## 代码 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,8 @@ Output: 0 | |
|
||
- 可以用递归实现, 个人认为比较难想到 | ||
|
||
- bit 运算 | ||
|
||
代码: | ||
|
||
```js | ||
|