鱼C论坛

 找回密码
 立即注册
查看: 1505|回复: 1

LeetCode 习题 231. 2 的幂

[复制链接]
发表于 2020-1-22 13:28:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 24 ms :

  1. class Solution:
  2.     def isPowerOfTwo(self, n: int) -> bool:
  3.         return bin(n).count("1") == 1 if n >= 1 else False
复制代码


C++ 0 ms :

  1. // https://leetcode-cn.com/problems/power-of-two/submissions/

  2. #include <string>
  3. using namespace std;

  4. string int2str(int n);
  5. string reverse(string s);

  6. string dec2bin(int n)
  7. {
  8.     string res = "";
  9.     while (n)
  10.     {
  11.         res += int2str(n % 2);
  12.         n /= 2;
  13.     }
  14.     res = reverse(res);
  15.     return res;
  16. }

  17. string int2str(int n)
  18. {
  19.     int m = n;
  20.     char s[100];
  21.     char ss[100];
  22.     int i = 0, j = 0;
  23.     if (n < 0)
  24.     {
  25.         m = 0 - m;
  26.         j = 1;
  27.         ss[0] = '-';
  28.     }
  29.     while (m > 0)
  30.     {
  31.         s[i++] = m % 10 + '0';
  32.         m /= 10;
  33.     }
  34.     s[i] = '\0';
  35.     i = i - 1;
  36.     while (i >= 0)
  37.     {
  38.         ss[j++] = s[i--];
  39.     }
  40.     ss[j] = '\0';
  41.     string res = ss;
  42.     return res;
  43. }

  44. string reverse(string s)
  45. {
  46.     int i;
  47.     string a = "";
  48.     for (i = s.size() - 1; i >= 0; i--)
  49.         a += s[i];
  50.     return a;
  51. }

  52. int count(string str, char ch)
  53. {
  54.     int i, r = 0;
  55.     for (i = 0; i < str.size(); i++)
  56.     {
  57.         if (str[i] == ch)
  58.         {
  59.             r++;
  60.         }
  61.     }
  62.     return r;
  63. }

  64. class Solution
  65. {
  66. public:
  67.     bool isPowerOfTwo(int n)
  68.     {
  69.         if (n < 1)
  70.             return false;
  71.         return count(dec2bin(n), '1') == 1;
  72.     }
  73. };
复制代码


JavaScript 80 ms :

  1. function reverse(string) {
  2.     var a = "";
  3.     for (var i = string.length - 1; i >= 0; i--) {
  4.         a += string[i];
  5.     }
  6.     return a;
  7. }

  8. function count(string, char) {
  9.     var r = 0;
  10.     for (var i = 0; i < string.length; i++) {
  11.         if (string[i] == char) r++;
  12.     }
  13.     return r;
  14. }

  15. function dec2bin(n) {
  16.     var res = "";
  17.     while (n) {
  18.         res += (n % 2).toString();
  19.         n -= (n % 2);
  20.         n /= 2;
  21.     }
  22.     return reverse(res);
  23. }

  24. function isPowerOfTwo(n) {
  25.     if (n < 1) return false;
  26.     else if (n == 1 || n == 2) return true;
  27.     return count(dec2bin(n), "1") == 1;
  28. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-1-24 17:19:42 From FishC Mobile | 显示全部楼层
0ms 是什么鬼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 16:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表