GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/342.power-of-four.js at master · hungry-python/leetcode

Expand file tree

/

342.power-of-four.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
43 lines (33 loc) · 1.17 KB
/

342.power-of-four.js

Copy path

File metadata and controls

43 lines (33 loc) · 1.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
};
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.