DataWhale task 14

2的幂

class Solution {
    public boolean isPowerOfTwo(int n) {
        int l = 0; int h = 32;
        
        while(h>=l){
            int med = l+(h-l)/2;
            int temp = (int)Math.pow(2,med);
            
            if(temp==n) return true;
            else if(temp>n) h = med-1;
            else{
                if(n%temp!=0) return false;
                l = med+1;
            }
        }
        
        return false;
        
    }
}

用栈实现队列

class MyQueue {

    /** Initialize your data structure here. */
    public MyQueue() {

    }
    LinkedList queue=new LinkedList();
    /** Push element x to the back of queue. */
    public void push(int x) {
        queue.addFirst(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return queue.removeLast();
    }
    
    /** Get the front element. */
    public int peek() {
        return queue.getLast();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

数字 1 的个数

class Solution {
    public int countDigitOne(int n) {
        int ans = 0;
        for(long i=1;i<=n;i*=10){
            long d = i*10;
            ans+=(n/d)*i+Math.min(Math.max(n%d-i+1,0),i);
        }
        return ans;
    }
}

你可能感兴趣的:(DataWhale task 14)