diff --git a/172.factorial-trailing-zeroes.js b/172.factorial-trailing-zeroes.js deleted file mode 100644 index 4f9d45205..000000000 --- a/172.factorial-trailing-zeroes.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * @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; -}; - diff --git a/263.ugly-number.js b/263.ugly-number.js deleted file mode 100644 index ff07c1594..000000000 --- a/263.ugly-number.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * @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; -}; diff --git a/342.power-of-four.js b/342.power-of-four.js deleted file mode 100644 index 849bbd00c..000000000 --- a/342.power-of-four.js +++ /dev/null @@ -1,43 +0,0 @@ -/* - * @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; -}; diff --git a/575.distribute-candies.js b/575.distribute-candies.js deleted file mode 100644 index d41326683..000000000 --- a/575.distribute-candies.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - * @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); -}; - diff --git a/README.en.md b/README.en.md index 77b6c6286..6ce24bb68 100644 --- a/README.en.md +++ b/README.en.md @@ -97,10 +97,11 @@ The data structures mainly includes: - [0020.Valid Parentheses](./problems/20.validParentheses.md) - [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md) - [0088.merge-sorted-array](./problems/88.merge-sorted-array.md) -- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) 🆕 -- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) 🆕 +- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) +- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) - [0136.single-number](./problems/136.single-number.md) - [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md) +- [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕 - [0169.majority-element](./problems/169.majority-element.md) - [0190.reverse-bits](./problems/190.reverse-bits.md) - [0191.number-of-1-bits](./problems/191.number-of-1-bits.md) @@ -109,8 +110,11 @@ The data structures mainly includes: - [0206.reverse-linked-list](./problems/206.reverse-linked-list.md) - [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md) - [0226.invert-binary-tree](./problems/226.invert-binary-tree.md) +- [0263.ugly-number](./problems/263.ugly-number.md) 🆕 - [0283.move-zeroes](./problems/283.move-zeroes.md) +- [0342.power-of-four](./problems/342.power-of-four.md) 🆕 - [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md) +- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕 #### Medium @@ -118,15 +122,15 @@ The data structures mainly includes: - [0002. Add Two Numbers](./problems/2.addTwoNumbers.md) - [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md) - [0011.container-with-most-water](./problems/11.container-with-most-water.md) -- [0015.3-sum](./problems/15.3-sum.md) 🆕 +- [0015.3-sum](./problems/15.3-sum.md) - [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md) - [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md) - [0039.combination-sum](./problems/39.combination-sum.md) - [0040.combination-sum-ii](./problems/40.combination-sum-ii.md) - [0046.permutations](./problems/46.permutations.md) - [0047.permutations-ii](./problems/47.permutations-ii.md) -- [0055.jump-game](./problems/55.jump-game.md) 🆕 -- [0062.unique-paths](./problems/62.unique-paths.md )🆕 +- [0055.jump-game](./problems/55.jump-game.md) +- [0062.unique-paths](./problems/62.unique-paths.md ) - [0075.sort-colors](./problems/75.sort-colors.md) - [0078.subsets](./problems/78.subsets.md) - [0086.partition-list](./problems/86.partition-list.md) @@ -138,12 +142,12 @@ The data structures mainly includes: - [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md) - [0139.word-break](./problems/139.word-breakmd) - [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md) -- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md) 🖊 +- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md) - [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕 - [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md) - [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md) - [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md) -- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) 🖊 +- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) - [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md) - [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕 - [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md) diff --git a/README.md b/README.md index 51c43749a..6eb0aa6db 100644 --- a/README.md +++ b/README.md @@ -95,10 +95,11 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0020.Valid Parentheses](./problems/20.validParentheses.md) - [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md) - [0088.merge-sorted-array](./problems/88.merge-sorted-array.md) -- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) 🆕 -- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) 🆕 +- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) +- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) - [0136.single-number](./problems/136.single-number.md) - [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md) +- [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕 - [0169.majority-element](./problems/169.majority-element.md) - [0190.reverse-bits](./problems/190.reverse-bits.md) - [0191.number-of-1-bits](./problems/191.number-of-1-bits.md) @@ -107,8 +108,11 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0206.reverse-linked-list](./problems/206.reverse-linked-list.md) - [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md) - [0226.invert-binary-tree](./problems/226.invert-binary-tree.md) +- [0263.ugly-number](./problems/263.ugly-number.md) 🆕 - [0283.move-zeroes](./problems/283.move-zeroes.md) +- [0342.power-of-four](./problems/342.power-of-four.md) 🆕 - [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md) +- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕 #### 中等难度 diff --git a/assets/drawio/172.factorial-trailing-zeroes.drawio b/assets/drawio/172.factorial-trailing-zeroes.drawio new file mode 100644 index 000000000..2252116b8 --- /dev/null +++ b/assets/drawio/172.factorial-trailing-zeroes.drawio @@ -0,0 +1 @@ +7V3bbuM2EP0aA22BNURS1OUxTp0u0C6waLYt0JdCsWhbqGxpJSVx+vUlbcm2OMzVsjiJ/bBYeSzrcuaQM2eGUgbscrH6pYjy+ZcsFumAOvFqwH4eUEoodeV/yvKwsXhBbZgVSVzvtDNcJ/+J2ujU1tskFmVrxyrL0irJ28ZJtlyKSdWyRUWR3bd3m2Zp+6x5NBPAcD2JUmj9K4mr+cYaUH9n/yyS2bw5M/HCzTeLqNm5vpNyHsXZ/Z6JjQfsssiyarO1WF2KVIHX4LL53dUj324vrBDL6iU/WN1/vR4X7Mvvv/5xd3Xzjdws7r5/qo9yF6W39Q0PqJfK443i5E5uztRmYyrzaKnbppk8u4I/zYr1Abzvt9lmB+aLyBPOvskpq4ca2caoDvCpXPv9Qu4g72i1/4v6bHxEfDrg8nfOVTSpsiKJ1L19K6IkTZYzufm3KDJJlPqyJAybK2tfrTSbbkKaW7dLW1dKK7FS9nm1SKWBKCSqIvtXXNY3vcyWQt1JkqaaKUqT2VJ+nEgfCWkf3YmiSiS7LuovFkkcq9OM7udJJa7zaKLOeS/HkrQV2e0yFsp9zvay1AHE6lEKkC2x5IgU2UJUxYPcpf4B9Wou1oORufXn+x21WcPX+R6teW2L6tE02x56Rzi5UXPuFfyjgH8E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RjpvBp/KgPfI8yK33+D8+GK28zsfkfGxA/5Rrq2WIw4kYL8vfQygouYDPkb4xLBHhm30guYg1l78BWWvwu6QBX70pXO0bXdQ10PnDihElTvwPY7MqIsLuRBqRIUcvieSXQfdJBBCKaWww/dsqBvgw44C7IbDITrkOMOHnOEluo5SCmrT2WzKE1CHr7dGz/aAceRYRwlUTHOeH0DnkWbFXC9pf2hK+9+s88lTflMyYHzhjR3sYr9mE6zJdViHo2GgD2THsuoPYf49/WH5YzOU9VELheByPc6flFf7Nv6KfZupw3n1OfiY9nQe1tN53Leep04GXvHjkQqBj+x/MrM21UMucTictpnf67RteG0g30XdevsDhN1m2u2o7MGIrzkzbJY47ofg5tHffnwJxeI5BD8Vgrvigq9zIfDhuO41BG+LcE8+iyMPlOTlY+Npz/lRmW/++NE0WSkUdTyPUE/g+mTp+/DJN2bAlB0P03Mr6+Vt52Z8NS1oZY8Tedj6SsvsVnmxE64Eet3OJIdMWvZoUzFxoJhV8fP8sPTBJR9QpDU5u9cWN3Gg9oUPhSCebF2uYxo2tQN7k+15/R3KyZaDcrUp8e15sjV1SY452Z7ak+acgyJTACcIv1enE5jh9uB0OAmdkNNDAiNtz043daaO7vRTWj8Inc7g9N6z06kNp5/QG68MTm+eBrfndCi2e3D6WZt1zKPQeprQtxA/tTdUQacTx7CUoCOvy4+7P2m+/m7vD8Oz8f8= \ No newline at end of file diff --git a/assets/drawio/263.ugly-number.drawio b/assets/drawio/263.ugly-number.drawio new file mode 100644 index 000000000..a744f51d0 --- /dev/null +++ b/assets/drawio/263.ugly-number.drawio @@ -0,0 +1 @@ +5Zhdb5swFIZ/DZepwAaSXuazvWinbW213VUOOGDFYGachu7X7xhMAqHVNi0brK3UYF5/P+fYHNvC86S4kiSLb0VIuYXssLDwwkLo0pnArxaeK8H13UqIJAsryTkKd+w7NaJt1B0Lad4qqITgimVtMRBpSgPV0oiUYt8uthG83WtGItoR7gLCu+oXFqq4UidofNSvKYviumfHv6xyElIXNjPJYxKKfUPCSwvPpRCqSiXFnHLNruZS1Vu9knsYmKSp+pUK19PpvUcWN2s0eXB3t1vnkxyNUNXKE+E7M+HDcNVzzUCKXRpS3Yxt4dk+ZoreZSTQuXswOmixSji8OZA0DVKpaPHqSJ3D/MFvqEioks9QpK7gelUV4zMONmPaHy3gGSluwK81YmweHVo+YoGEIfMblHCHkvltYYIJqzaLXEmxpXPBhQQlFSmUnG0Y5ycS4SxK4TUAQBT0mcbHwAmnJiNhYai7eRF+2zxn4I9sv82/tkeDv/sCf/S3+LtdL+3dSZFvD8tJvQFCwkOD5Hcgeb1DcocGadyB9PXtbnbYGQ9rs5u8J/qu4w2L/mX3U38KHwK5TCdZUsaOTSucwlQia6g3ZE35R5EzxYTOXQulRAIFuM6YkWAblYBrk1kIb8o/KFJ2Ns2zKsbV9En9smGFNsnMjGcRK6WD46kGgVZBmOILBuHxhoHp5EUAPaJVSBSBh9ZzeG44USMS6HHlo1LUIFaeo2nes2D7eEvkduSgyUWWRtqzRKoawxwvp/7yTC7heQPbDuujSPPL2v+n9TRIPiyT3jA53aXznqJkcNx+ty7nhcNc7256Gib376bdw1ztuQMKlPvH1D1OvOEw5DQI7H8td2Pw/z8OcX4ahywgyrjiJM8fP9O9kFtoD60g6ijgH1ImQtEBC+M04CKn/z4e6X9pdk8IFvK5Xokhe2r5iP9tp68XSzyjvLxcBXPYMP2ihFPnQyoyz7KdPCPpnzXkzZCPLQ+q2g8R1zg/7JI1LHXTA8y86qTdsXYJPYdaHeSG85qzad3cYKNzBRkeajkf9rrOhzA+y84Er8fb6DKvcaWPlz8A \ No newline at end of file diff --git a/assets/drawio/342.power-of-four.drawio b/assets/drawio/342.power-of-four.drawio new file mode 100644 index 000000000..6e8e0c880 --- /dev/null +++ b/assets/drawio/342.power-of-four.drawio @@ -0,0 +1 @@ +7Vpdb5swFP01PFbCNjjhsUnTTZOqTerDtL5MDJwEleCUkIb21+8SbAIYJNYGjJZQKYXjjwvnXl8fGwwy36RfYne7fuA+Cw1s+qlB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3xa9CusFR1GwYsYM6qn1n5fqdyP/4nci09saxx+9E4PL0kfSxrPSlOVn8BQ== \ No newline at end of file diff --git a/assets/drawio/575.distribute-candies.drawio b/assets/drawio/575.distribute-candies.drawio new file mode 100644 index 000000000..1f892397e --- /dev/null +++ b/assets/drawio/575.distribute-candies.drawio @@ -0,0 +1 @@ +7Zldc6IwFIZ/jZc4EL7spVrd7U4725le7MzeRQiQMRAWYtX++j2BpAraaTvrgrvai5K8CTnhPOckSAb2NN18KXCePPCQsAEyw83Avh0gdGON4L8UtrXgeE4txAUNa8naCU/0hSjRVOqKhqRsdBScM0HzphjwLCOBaGi4KPi62S3irGk1xzE5EJ4CzA7VHzQUSa2OkL/TvxIaJ9qy5d3ULSnWndWTlAkO+XpPsmcDe1pwLupSupkSJn2n/VLfN3+j9XViBcnER25Y3t0nLxm988to+/jwM/2efAsMy62HecZspZ5YzVZstQsKvspCIkcxB/ZknVBBnnIcyNY1MActESmDmgVFNRwpBNm8OVHr9fEhbAhPiSi20EXd4Iz8oZqWChpbVdc7AraeebLnfWQqESvq8evgO8dAQfnmM35yzs9P/tk56X0fQRLkskjTKu/2PSJ9QSHxxozGGWiC53vqPV4Q9shLKiiXrQsuBE+hA5MNExws48r/U854Udmyo+oPulTGxmVerw8SDtaViG4ksYmaz20ihFxYxtIRaB6EmTOksLREFMgWwwAsonmIBYaL1Eu4RgUhRiDNGkFS0FKkuDR0I4K4mU9xFm4NC42GeRafALylF0dF3jMPyLvmIXitnZw7ulzujGakM+yj88JuXy72mG3zpCvuqMXdMnsG/4G98J8Db74LfolTwgjPjCoCYGNfGnrHR/NqST49ebO5xVtuz+S9/5D8Oe7wLmqmvHv4btcpd//KvRPuHmomvNMz99GVezf57ja5j3pe52+u3Lvh7rRe7fpOeP3L8kr+b6/0TuvVzuyb/PXrTTfkfbOV86jnxd664O833ea83SLf92u9dcGfcLrNebu12ve+zx/7Je8xMDsJ6XMjArxfK3lENYl4JoyyOqADZ5vw+JvqHEO3QylW12qcMsfZnw3kTlzfHbhwq3kLlAq6WAkCFUmHklIbAgfUtpr2JXf5KFptxTXAFM1wBgt8SXQ4ZjyT4R5RxloSVuEeAHxSHMmDlIahNHP0DKh5SiR9oQ49ka7vJYQ/G3sz8zTHRXZr37Gtw30HOUeCEH0+CKG6O9is2vZOh+3Zbw== \ No newline at end of file diff --git a/assets/problems/172.factorial-trailing-zeroes-1.png b/assets/problems/172.factorial-trailing-zeroes-1.png new file mode 100644 index 000000000..29735f203 Binary files /dev/null and b/assets/problems/172.factorial-trailing-zeroes-1.png differ diff --git a/assets/problems/172.factorial-trailing-zeroes-2.png b/assets/problems/172.factorial-trailing-zeroes-2.png new file mode 100644 index 000000000..5bc35e687 Binary files /dev/null and b/assets/problems/172.factorial-trailing-zeroes-2.png differ diff --git a/assets/problems/172.factorial-trailing-zeroes-3.png b/assets/problems/172.factorial-trailing-zeroes-3.png new file mode 100644 index 000000000..9eaa8342a Binary files /dev/null and b/assets/problems/172.factorial-trailing-zeroes-3.png differ diff --git a/assets/problems/263.ugly-number.png b/assets/problems/263.ugly-number.png new file mode 100644 index 000000000..89bf87cb3 Binary files /dev/null and b/assets/problems/263.ugly-number.png differ diff --git a/assets/problems/342.power-of-four-1.png b/assets/problems/342.power-of-four-1.png new file mode 100644 index 000000000..10e7627c6 Binary files /dev/null and b/assets/problems/342.power-of-four-1.png differ diff --git a/assets/problems/342.power-of-four-2.png b/assets/problems/342.power-of-four-2.png new file mode 100644 index 000000000..6ecd473b1 Binary files /dev/null and b/assets/problems/342.power-of-four-2.png differ diff --git a/assets/problems/342.power-of-four.png b/assets/problems/342.power-of-four.png new file mode 100644 index 000000000..542bc717b Binary files /dev/null and b/assets/problems/342.power-of-four.png differ diff --git a/assets/problems/575.distribute-candies.png b/assets/problems/575.distribute-candies.png new file mode 100644 index 000000000..46c6b62d0 Binary files /dev/null and b/assets/problems/575.distribute-candies.png differ diff --git a/problems/172.factorial-trailing-zeroes.md b/problems/172.factorial-trailing-zeroes.md new file mode 100644 index 000000000..5f1c95c51 --- /dev/null +++ b/problems/172.factorial-trailing-zeroes.md @@ -0,0 +1,76 @@ + +## 题目地址 +https://leetcode.com/problems/factorial-trailing-zeroes/description/ + +## 题目描述 + +``` +Given an integer n, return the number of trailing zeroes in n!. + +Example 1: + +Input: 3 +Output: 0 +Explanation: 3! = 6, no trailing zero. +Example 2: + +Input: 5 +Output: 1 +Explanation: 5! = 120, one trailing zero. +Note: Your solution should be in logarithmic time complexity. + +``` + +## 思路 + +我们需要求解这n个数字相乘的结果末尾有多少个0,由于题目要求log的复杂度,因此暴力求解是不行的。 + +通过观察,我们发现如果想要结果末尾是0,必须是分解质因数之后,2 和 5 相乘才行,同时因数分解之后发现5的个数远小于2, +因此我们只需要求解这n数字分解质因数之后一共有多少个5即可. + +![172.factorial-trailing-zeroes-2](../assets/problems/172.factorial-trailing-zeroes-2.png) + +如图如果n为30,那么结果应该是图中红色5的个数,即7。 + +![172.factorial-trailing-zeroes-1](../assets/problems/172.factorial-trailing-zeroes-1.png) + +我们的结果并不是直接f(n) = n / 5, 比如n为30, 25中是有两个5的。 +类似,n为150,会有7个这样的数字,通过观察我们发现规律`f(n) = n/5 + n/5^2 + n/5^3 + n/5^4 + n/5^5+..` + +![172.factorial-trailing-zeroes-3](../assets/problems/172.factorial-trailing-zeroes-3.png) + +如果可以发现上面的规律,用递归还是循环实现这个算式就看你的了。 +## 关键点解析 + +- 数论 + + +## 代码 + +```js + + +/* + * @lc app=leetcode id=172 lang=javascript + * + * [172] Factorial Trailing Zeroes + */ +/** + * @param {number} n + * @return {number} + */ +var trailingZeroes = function(n) { + // tag: 数论 + + // if (n === 0) return n; + + // 递归: f(n) = n / 5 + f(n / 5) + // 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; +}; +``` diff --git a/problems/263.ugly-number.md b/problems/263.ugly-number.md new file mode 100644 index 000000000..7bc663a67 --- /dev/null +++ b/problems/263.ugly-number.md @@ -0,0 +1,92 @@ +## 题目地址 + +https://leetcode.com/problems/ugly-number/description/ + +## 题目描述 + +``` +Write a program to check whether a given number is an ugly number. + +Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. + +Example 1: + +Input: 6 +Output: true +Explanation: 6 = 2 × 3 +Example 2: + +Input: 8 +Output: true +Explanation: 8 = 2 × 2 × 2 +Example 3: + +Input: 14 +Output: false +Explanation: 14 is not ugly since it includes another prime factor 7. +Note: + +1 is typically treated as an ugly number. +Input is within the 32-bit signed integer range: [−231, 231 − 1]. + +``` + +## 思路 + +题目要求给定一个数字,判断是否为“丑陋数”(ugly number), 丑陋数是指只包含质因子2, 3, 5的正整数。 + +![263.ugly-number](../assets/problems/263.ugly-number.png) + +根据定义,我们将给定数字除以2、3、5(顺序无所谓),直到无法整除。 +如果得到1,说明是所有因子都是2或3或5,如果不是1,则不是丑陋数。 + +这就好像我们判断一个数字是否为n(n为大于1的正整数)的幂次方一样,我们只需要 +不断除以n,直到无法整除,如果得到1,那么就是n的幂次方。 这道题的不同在于 +它不再是某一个数字的幂次方,而是三个数字(2,3,5),不过解题思路还是一样的。 + +转化为代码可以是: + +```js + + while(num % 2 === 0) num = num / 2; + while(num % 3 === 0) num = num / 3; + while(num % 5 === 0) num = num / 5; + + return num === 1; + +``` + +> 我下方给出的代码是用了递归实现,只是给大家看下不同的写法而已。 + +## 关键点 +- 数论 +- 因数分解 +## 代码 + +```js + +/* + * @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; +}; +``` + diff --git a/problems/342.power-of-four.md b/problems/342.power-of-four.md new file mode 100644 index 000000000..0ceeff17f --- /dev/null +++ b/problems/342.power-of-four.md @@ -0,0 +1,105 @@ +## 题目地址 + +https://leetcode.com/problems/power-of-four/description/ + +## 题目描述 + +``` +Given an integer (signed 32 bits), write a function to check whether it is a power of 4. + +Example 1: + +Input: 16 +Output: true +Example 2: + +Input: 5 +Output: false +Follow up: Could you solve it without loops/recursion? + +``` + +## 思路 + +符合直觉的做法是不停除以 4 直到不能整除,然后判断是否为 1 即可。 代码如下: + +```js +while (num && num % 4 == 0) { + num /= 4; +} +return num == 1; +``` + +但是这道题目有一个 follow up: “你是否可以不使用循环/递归完成”。因此我们需要换种思路。 + +我们先来看下,4 的幂次方用 2 进制表示是什么样的. + +![263.342.power-of-four-1](../assets/problems/342.power-of-four-1.png) + +发现规律: 4 的幂次方的二进制表示 1 的位置都是在奇数位(且不在最低位),其他位置都为 0 + +我们还可以发现: 2 的幂次方的特点是最低位之外,其他位置有且仅有一个 1(1 可以在任意位置) + +我们进一步分析,如果一个数字是四的幂次方,那么只需要满足: + +1. 是 2 的幂次方, 就能保证最低位之外,其他位置有且仅有一个 1 +2. 这个 1 不在偶数位置,一定在奇数位置 + +对于第一点,如果保证一个数字是 2 的幂次方呢? 显然不能不停除以 2,看结果是否等于 1,这样就循环了。 +我们可以使用一个 trick, 如果一个数字 n 是 2 的幂次方,那么 n & (n - 1) 一定等于 0, +这个可以作为思考题,大家思考一下。 + +对于第二点,我们可以取一个特殊数字,这个特殊数字,奇数位置都是 1,偶数位置都是 0,然后和这个特殊数字 +`求与`, 如果等于本身,那么毫无疑问,这个 1 不再偶数位置,一定在奇数位置,因为如果在偶数位置,`求与`的结果就是 0 了 +题目要求 n 是 32 位有符号整形,那么我们的特殊数字就应该是`01010101010101010101010101010101`(不用数了,一共 32 位)。 + +![263.342.power-of-four-2](../assets/problems/342.power-of-four-2.png) + +如上图,64和这个特殊数字求与,得到的是本身。 8 是 2的次方,但是不是4的次方,我们求与结果就是0了。 + +为了体现自己的逼格,我们可以使用计算器,来找一个逼格比较高的数字,这里我选了十六进制,结果是`0x55555555`。 + +![263.342.power-of-four](../assets/problems/342.power-of-four.png) + +代码见下方代码区。 + +说实话,这种做法不容易想到,其实还有一种方法。 +如果一个数字是 4 的幂次方,那么只需要满足: + +1. 是二的倍数 +2. 减去 1 是三的倍数 + +代码如下: + +```js +return num > 0 && num & (num - 1 === 0) && (num - 1) % 3 === 0; +``` + +## 关键点 + +- 数论 +- 2的幂次方特点(数学性质以及二进制表示) +- 4的幂次方特点(数学性质以及二进制表示) +## 代码 + +```js +/* + * @lc app=leetcode id=342 lang=javascript + * + * [342] Power of Four + */ +/** + * @param {number} num + * @return {boolean} + */ +var isPowerOfFour = function(num) { + // tag: 数论 + + if (num === 1) return true; + if (num < 4) return false; + + if ((num & (num - 1)) !== 0) return false; + + return (num & 0x55555555) === num; +}; +``` diff --git a/problems/575.distribute-candies.md b/problems/575.distribute-candies.md new file mode 100644 index 000000000..9567319d9 --- /dev/null +++ b/problems/575.distribute-candies.md @@ -0,0 +1,62 @@ + +## 题目地址 +https://leetcode.com/problems/distribute-candies/description/ + +## 题目描述 + +``` +Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain. +Example 1: +Input: candies = [1,1,2,2,3,3] +Output: 3 +Explanation: +There are three different kinds of candies (1, 2 and 3), and two candies for each kind. +Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. +The sister has three different kinds of candies. +Example 2: +Input: candies = [1,1,2,3] +Output: 2 +Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. +The sister has two different kinds of candies, the brother has only one kind of candies. +Note: + +The length of the given array is in range [2, 10,000], and will be even. +The number in given array is in range [-100,000, 100,000]. +``` + +## 思路 +由于糖果是偶数,并且我们只需要做到两个人糖果数量一样即可。 + +考虑两种情况: + +![575.distribute-candies](../assets/problems/575.distribute-candies.png) + +- 如果糖果种类大于n / 2(糖果种类数为n),妹妹最多可以获得的糖果种类应该是`n / 2`(因为妹妹只有n / 2个糖). +- 糖果种类数小于n / 2, 妹妹能够得到的糖果种类可以是糖果的种类数(糖果种类本身就这么多). + +因此我们发现,妹妹能够获得的糖果种类的制约因素其实是糖果种类数。 + +## 关键点解析 + +- 这是一道逻辑题目,因此如果逻辑分析清楚了,代码是自然而然的 + + +## 代码 + +```js +/* + * @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); +}; +``` + +