leetcode
fightingyxy
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Power of Two---231
class Solution { public: bool isPowerOfTwo(int n) { return (!(n&(n-1)) && n>0); } }; n&(n-1)的妙用: 求数的二进制中1的个数,求二进制数是否为2的N次幂,求二进制数的阶乘中有多少个含2的质因数的数。(如4!=1*2*3*4,一共2个分别为2和4)原创 2016-03-23 20:12:07 · 349 阅读 · 0 评论 -
String to Integer---8
将字符串数字转成数字。注意正负号,如果第一位是字母返回空字符,“-123a”只返回-123。 int myAtoi(string str) { long result = 0; int indicator = 1; int i; i = str.find_first_not_of(' '); if(str[i] == '-' ||原创 2016-03-25 20:00:41 · 400 阅读 · 0 评论 -
Reverse Bits---190
class Solution { public: uint32_t reverseBits(uint32_t n) { int i=32; uint32_t res=0; while(i) { res=res<<1; res=res|(n&1); n=n>原创 2016-03-23 20:50:44 · 360 阅读 · 0 评论 -
Reverse Integer---7
class Solution { public: int reverse(int x) { if(x>INT_MAX||x<INT_MIN) return 0; long long int n=0,tmp=x; while(tmp) { n=n*10+tmp%10; tmp=tmp/原创 2016-03-23 20:48:21 · 315 阅读 · 0 评论 -
Plus One---66
题意:数组存的二进制的每位数字,求加1后的结果,同样返回数组 class Solution { public: vector plusOne(vector& digits) { int i=digits.size(); int carry=1; int size=digits.size(); for(i=size-1;i>=原创 2016-03-23 20:42:55 · 400 阅读 · 0 评论 -
Add Binary---67
题意:2个包含二进制数的字符串,将二进制相加,返回这个二进制的字符串。 class Solution { public: string addBinary(string a, string b) { int i=a.size(),j=b.size(); int carry=0,sum=0; if(j>i) return addBinary(b,a); for(i=原创 2016-03-23 20:36:06 · 324 阅读 · 0 评论 -
Number of 1 Bits---191
题意:32位无符号二进制数中1的个数。 class Solution { public: int hammingWeight(uint32_t n) { int count=0; if(n==0) return 0; while(n) { count++; n=n&(n-1);原创 2016-03-23 20:31:27 · 377 阅读 · 0 评论 -
Ugly Number---263
class Solution { public: bool isUgly(int num) { if(num<=0) return false; while((num%5)==0) { num=num/5; } while((num%2)==0) { num=num/2; } while((num%3)=原创 2016-03-23 20:29:32 · 303 阅读 · 0 评论 -
Bulb Switcher---319
leetcode319 题目大意:给你N个灯泡,N个灯泡的初始状态是off,先全开on,之后每第n次关n的倍数处的灯泡,问经过第N次后还有多少个灯泡亮着on? 题目分析:这个题归为一个数学问题。 首先,在草稿纸上画出这样一个图(1代表on,0代表off),画出N=1-10,经过题目所述规律后的结果 N=1原创 2016-03-14 14:42:18 · 304 阅读 · 0 评论 -
Nim Game---292
题目大意:你和朋友做游戏,大家轮流从一堆石头中拿1~3个石头,拿到这堆石头中的最后一个石头的人就赢得比赛。也就是说,这个游戏2个人玩,从你先开始拿,那么这堆石头有多少个的情况下你绝对能赢得游戏呢?题目要求输入石头的总共个数,得出你是否能赢得比赛,赢返回true,输返回false。 题目分析:还是数学问题。我们还是来画图找规律啦!N表示这堆石头里的个数。后面是怎么拿石头的过程 N=1 1原创 2016-03-14 15:01:44 · 320 阅读 · 0 评论 -
Counting Bits---338
class Solution { public: vector countBits(int num) { vector v(num+1); v[0]=0; for(int i=1;i<=num;i++) { v[i]=v[i>>1]+(i&1); } return v;原创 2016-03-23 20:05:50 · 266 阅读 · 0 评论 -
Move Zeroes---283
题目大意:给你一个数组,找到里面的0并把0放在数组的后面位置,其他非零位置的数的前后顺序不能变。 题目分析:用C++编程,题目给的数据结构类型是vector容器,充分利用vector的成员函数,vector的数据存储结构是线性的,在用vector的erase函数时,vector类型的对象的头指针和长度是会发生相应的变化的,vector的push_back()函数是在对象存储的数据的结尾处加入数据原创 2016-03-14 16:57:09 · 274 阅读 · 0 评论 -
Palindrome Number---9
class Solution { public: bool isPalindrome(int x) { if(x<0) return false; int n=0,tmp=x; while(tmp) { n=n*10+tmp%10; tmp=tmp/10; }原创 2016-03-23 20:52:41 · 300 阅读 · 0 评论
分享