20220321AC算法打卡

剑指 Offer 09. 用两个栈实现队列

 

class CQueue {
public:
     stack<int> a;
     stack<int> b;
    CQueue() {
      
    }
    void appendTail(int value) {
      a.push(value);
    }
    int deleteHead() {
       int v = -1;
       if (b.size() >0) {
           v = b.top();
           b.pop();
       }
       else{
           while (!a.empty()) {
               b.push(a.top());
               a.pop();
           }
        if (!b.empty()) {
           v = b.top();
           b.pop();
       }
    }
    return v;
       }  
};

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue* obj = new CQueue();
 * obj->appendTail(value);
 * int param_2 = obj->deleteHead();
 */

 剑指 Offer 64. 求1+2+…+n

 

class Solution {
public:
    int sumNums(int n) {
      n && (n += sumNums(n-1));
      return n;
    }
};

 231. 2 的幂

 

class Solution {
public:
    bool isPowerOfTwo(int n) {
     int sum=0;
     if(n<0)return false;
     while(n!=0){
      sum+=(n&1);
     n=n>>1;
     }
     if(sum==1) return true;
     else return false;
    }

};

 326. 3 的幂

class Solution {
public:
    bool isPowerOfThree(int n) {
     return n > 0 && 1162261467%n ==0;
    }
};

​​​​​​342. 4的幂

class Solution {
public:
    bool isPowerOfFour(int n) {
   if(n <= 0)
   return false;
   int num = log(n)/log(4);
   return num*log(4) == log(n);
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值