思路
DP + Bit Manupulation。主用运用 x & (x-1)
移除x最低位1的思想。。
source code
1 | // Runtime: 96 ms, faster than 72.42% of JavaScript online submissions for Counting Bits. |
test cases
1 | test("test1", () => { |
前端小天才智多星班进修中
DP + Bit Manupulation。主用运用 x & (x-1)
移除x最低位1的思想。。
1 | // Runtime: 96 ms, faster than 72.42% of JavaScript online submissions for Counting Bits. |
1 | test("test1", () => { |
思路:求底,判断结果是否是整数
1 | // Runtime: 88 ms, faster than 18.54% of JavaScript online submissions for Power of Two. |
思路:位运算,因为2的指数结果二进制一定是以1开头,剩下位数都是0的串。
1 | // Runtime: 96 ms, faster than 15.25% of JavaScript online submissions for Power of Two. |
1 | test("test1", () => { |
位运算。
1 | // Runtime: 72 ms, faster than 26.58% of JavaScript online submissions for Number of 1 Bits. |
1 | test("test1", () => { |
DFS。这道题借用了 79. Word Search 中的exist方法。
1 | // RuRuntime: 1352 ms, faster than 20.07% of JavaScript online submissions for Word Search II. |
Trie。
//todo
1 | const board = [ |
使用回溯法解决。
1 | // Runtime: 76 ms, faster than 89.67% of JavaScript online submissions for Word Search. |
1 | const board = [ |
1 | // Runtime: 212 ms, faster than 34.03% of JavaScript online submissions for Implement Trie (Prefix Tree). |
原题:69. Sqrt(x)
二分法
要牢记二分法的模板:
1 | let left = 0; |
1 | // Runtime: 88 ms, faster than 35.63% of JavaScript online submissions for Sqrt(x). |
求sqart(x),要求精度为n
1 | // init variable |
牛顿迭代法:
这道题中,f(xn) = xn ^ 2 - y
,f(xn)的导数 f'(xn) = 2 * xn
。
最终可得:x(n+1) = (xn + y/xn)/2
。
1 | // Runtime: 64 ms, faster than 95.63% of JavaScript online submissions for Sqrt(x). |
1 | test("test1", () => { |
思路:backtrace + 适当剪枝,判断是否合法应用了36. Valid Sudoku的解法
时间复杂度:O(n^3)
1 | // Runtime: 192 ms, faster than 16.58% of JavaScript online submissions for Sudoku Solver. |
1 | const board = [ |
思路:brute force
时间复杂度:O(n^2)
1 | // Runtime: 96 ms, faster than 28.80% of JavaScript online submissions for Valid Sudoku. |
思路:利用set缓存每一步的结果
时间复杂度:O(n^2),实际上比方法一通用、高效很多
1 | // Runtime: 100 ms, faster than 25.57% of JavaScript online submissions for Valid Sudoku. |
1 | test("test1", () => { |
原题:51. N-Queens
这道题主要用到 回溯 的思想解题。
假设第i行Q占据图中的位置,则她的四条攻击路径如图所示。
容易得出:路径3(撇)经过的单元格都有y+x=c1
,路径4(捺)经过的单元格都有y-x=c2
,其中c1/c2是一个常数。
col
标记已经占用的列xySum
标记已经被占用的撇xyDiff
标记已经占用的捺最后回溯即可。
1 | // Runtime: 88 ms, faster than 33.33% of JavaScript online submissions for N-Queens. |
1 | test("test1", () => { |
tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true