大厂算法手撕高频

1、两数之和(LeetCode 1) (√)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                return new int[]{i,map.get(target-nums[i])};
            }
            map.put(nums[i],i);
        }
        return null;
    }
}

2、盛最多水的容器(LeetCode 11) (√)

class Solution {
    public int maxArea(int[] height) {
        int max=0;
        int left=0,right=height.length-1;
        while(left<right){
            max=Math.max(max,(right-left)*Math.min(height[left],height[right]));
            if(height[left]>height[right]){
                right--;
            }else{
                left++;
            }
        }
        return max;
    }
}

3、三数之和(LeetCode 15) (√)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
       List<List<Integer>> lists=new ArrayList();
       Arrays.sort(nums);
       for(int i=0;i<nums.length;i++){
           if(i>0&&nums[i]==nums[i-1])continue;
          int left=i+1,right=nums.length-1;
          while(left<right){
               if(nums[i]+nums[left]+nums[right]==0){
                    List<Integer> list=new ArrayList();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    lists.add(list);
                    left++;
                    right--;
                    while(left<right&&nums[left]==nums[left-1])left++;
                    while(left<right&&nums[right]==nums[right+1])right--;
               }else if(nums[i]+nums[left]+nums[right]>0){
                    right--;
               }else{
                     left++;
               }
          }
       }
       return lists;
    }
}

4、接雨水(LeetCode 42) (√)

class Solution {
    public int trap(int[] height) {
        int n=height.length;
        int[] left=new int[n];
        int[] right=new int[n];
        left[0]=height[0];
        right[n-1]=height[n-1];
        for(int i=1;i<n;i++){
            left[i]=Math.max(height[i],left[i-1]);
        }
        for(int i=n-2;i>=0;i--){
            right[i]=Math.max(height[i],right[i+1]);
        }
        int res=0;
        for(int i=1;i<n-1;i++){
            res+=Math.min(left[i],right[i])-height[i];
        }
        return res;
    }
}

5、无重复字符的最长子串(LeetCode 3) (√)

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashSet<Character> set=new HashSet();
        int p=0,max=0;;
        for(int i=0;i<s.length();i++){
            while(set.contains(s.charAt(i))){
                set.remove(s.charAt(p));
                p++;
            }
            set.add(s.charAt(i));
            max=Math.max(max,i-p+1);
        }
        return max;
    }
}

6、最大子数组和(LeetCode 53) (√)

class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp=new int[nums.length];
        dp[0]=nums[0];
        int max=dp[0];
        for(int i=1;i<nums.length;i++){
            dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
            max=Math.max(max,dp[i]);
        }
        return max;
    }
}

7、轮转数组(LeetCode 189) (√)

class Solution {
    public void rotate(int[] nums, int k) {
        k%=nums.length;
          reverse(nums,0,nums.length-1);
          reverse(nums,0,k-1);
          reverse(nums,k,nums.length-1);  
    }
    public void reverse(int[] nums,int left,int right){
        while(left<right){
            int temp=nums[left];
            nums[left]=nums[right];
            nums[right]=temp;
            left++;
            right--;
        }
    }
}

8、反转链表(LeetCode 206) (√)

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null) return null;
        ListNode dummy=new ListNode();
        ListNode p=head,q;
        while(p!=null){
            q=p.next;
            p.next=dummy.next;
            dummy.next=p;
            p=q;
        }
        return dummy.next;
    }

9、反转链表II(LeetCode 92) (√)

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy=new ListNode();   
        dummy.next=head;
        ListNode x1=dummy,x2=dummy,y1=dummy,y2=dummy;  //分别代表left的前驱节点和left节点,right和right后继
         for(int i=1;i<left;i++){
            x1=x1.next;
         }
         x2=x1.next;for(int i=1;i<=right;i++){
            y1=y1.next;
        }
        y2=y1.next;     
         //断开头尾
        x1.next=null;   
        y1.next=null;
        //准备头插法反转
        ListNode L=new ListNode();
        ListNode p=x2,q;
        while(p!=null){
            q=p.next;
            p.next=L.next;
            L.next=p;
            p=q;
        }
        //连接回反转后的节点
        x1.next=L.next;
        x2.next=y2;
        return dummy.next;
    }
}

10、环形链表(LeetCode 141) (√)

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head==null)return false;
        ListNode slow=head,fast=head.next;
        while(fast!=null&&fast.next!=null){
              if(fast==slow) return true;
              slow=slow.next;
              fast=fast.next.next;
        }
        return false;
    }
}

11、环形链表II(LeetCode 142) (√)

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null) return null;
        ListNode slow=head,fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(fast==slow){
                fast=head;
                while(fast!=slow){
                    fast=fast.next;
                    slow=slow.next;
                }
                return  fast;
            }
          
        }
        return null;
    }
}

12、相交链表(LeetCode 160) (√)

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA=0,lenB=0;
        ListNode p=headA,q=headB;
        while(p!=null){
            lenA++;
            p=p.next;
        }
        while(q!=null){
            lenB++;
            q=q.next;
        }
        p=headA;
        q=headB;
        if(lenA>lenB){
            for(int i=1;i<=lenA-lenB;i++){
                p=p.next;
            }
        }else{
             for(int i=1;i<=lenB-lenA;i++){
                q=q.next;
            }
        }
        while(p!=null&&q!=null){
            if(p==q)return p;
            p=p.next;
            q=q.next;
        }
        return null;

    }
}

13、合并两个有序链表(LeetCode 21) (√)

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode L=new ListNode();
        ListNode p=list1,q=list2,r=L;
        while(p!=null&&q!=null){
            if(p.val<q.val){
                r.next=p;
                p=p.next;
            }else{
                r.next=q;
                q=q.next;
            }
            r=r.next;
        }
        if(p!=null) r.next=p;
        if(q!=null) r.next=q;
        return L.next;
    }
}

14、删除排序链表中的重复元素(LeetCode 83) (√)

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode p=head;
        while(p.next!=null){
            if(p.val==p.next.val){
                p.next=p.next.next;
            }else{
               p=p.next;
            }
        }
        return head;
    }
}

15、删除排序链表中的重复元素II(LeetCode 82) (√)

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode dummy=new ListNode();
        dummy.next=head;
        ListNode p=dummy;
        while(p.next!=null&&p.next.next!=null){
            if(p.next.val==p.next.next.val){
                int temp=p.next.val;
                while(p.next!=null&&p.next.val==temp){
                    p.next=p.next.next;
                }
            }else{
                p=p.next;
            }
        }
        return dummy.next;
    }
}

16、删除链表的倒数第N个结点(LeetCode 19) (√)

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy=new ListNode();
        dummy.next=head;
        ListNode p=dummy,q=head;
        for(int i=0;i<n;i++){
            q=q.next;
        }
        while(q!=null){
            p=p.next;
            q=q.next;
        }
        p.next=p.next.next;
        return dummy.next;
    }
}

17、K个一组翻转链表(LeetCode 25) (√)

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy=new ListNode();
        dummy.next=head;
        ListNode p=dummy,q,r;
        while(true){
            q=p;
            int num=0;
            while(q.next!=null&&num<k){
                q=q.next;
                num++;
            }
            if(num<k)return dummy.next;
            ListNode nextK=q.next,nextp=p.next;
            q.next=null;
            //头插法反转
            q=p.next;
            p.next=null;
            while(q!=null){
                r=q.next;
                q.next=p.next;
                p.next=q;
                q=r;
            }
            nextp.next=nextK;
            p=nextp;
        }
    }   
}

18、LRU缓存机制(LeetCode 146) (√)

class LRUCache {
    class DLink{
        DLink prev,next;
        int key,value;
        DLink(){}
        DLink(int key,int value){this.key=key;this.value=value;}
    }
    private Map<Integer,DLink> cache=new HashMap<Integer,DLink>();
    private int capacity;
    private int size;
    private DLink head,tail;

    public LRUCache(int capacity) {
        this.capacity=capacity;
        size=0;
        head=new DLink();
        tail=new DLink();
        head.next=tail;
        tail.prev=head;
    }
    
    public int get(int key) {
        DLink node=cache.get(key);
        if(node==null) return -1;
        moveToHead(node);
        return node.value;
    }
    
    public void put(int key, int value) {
        DLink node=cache.get(key);
        if(node==null){
            DLink newnode=new DLink(key,value); 
            cache.put(key,newnode);
            addToHead(newnode);
            size++;
            if(size>capacity){
                DLink x=removeTail();
                cache.remove(x.key);
                size--;
            }
        }else{
            node.value=value;
            moveToHead(node);
        }
    }
    public void addToHead(DLink node){
        node.prev=head;
        node.next=head.next;
        head.next.prev=node;
        head.next=node;
    }

    public void removeNode(DLink node){
        node.next.prev=node.prev;
        node.prev.next=node.next;
    }

    public  void moveToHead(DLink node){
        removeNode(node);
        addToHead(node);
    }

    public DLink removeTail(){
        DLink res=tail.prev;
        removeNode(res);
        return res;
    }
}

19、 二叉树的中序遍历(LeetCode 94)(x)

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList();
        Deque<TreeNode> stack=new LinkedList();
        TreeNode node=root;
        while(node!=null||!stack.isEmpty()){
            while(node!=null){
                stack.push(node);
                node=node.left;
            }
            node=stack.pop();
            list.add(node.val);
            node=node.right;
        }
        return list;
    }
}

20、 二叉树的最大深度(LeetCode 104) (√)

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        int left= maxDepth(root.left);
        int right=maxDepth(root.right);
        return Math.max(left,right)+1;
    }
}

21、二叉树的层序遍历(LeetCode 102) (√)

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists=new ArrayList();
        if(root==null) return lists;
        Queue<TreeNode> queue=new LinkedList();
        queue.add(root);
        while(!queue.isEmpty()){
            int size=queue.size();
            List<Integer> list=new ArrayList();
            for(int i=0;i<size;i++){
                TreeNode node=queue.poll();
                list.add(node.val);
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
            }
            lists.add(list);
        }
        return lists;
    }
}

22、路径总和III(LeetCode 437) (√)

class Solution {
    public int pathSum(TreeNode root, long targetSum) {
        if(root==null) return 0;
        int res=0;
        res+=dfs(root,targetSum);
        res+=pathSum(root.left,targetSum);
        res+=pathSum(root.right,targetSum);
        return res;
    }
    public  int dfs(TreeNode root,long target){
        if(root==null) return 0;
        int res=0;
        if(root.val==target) res++;
        res+=dfs(root.left,target-root.val);
        res+=dfs(root.right,target-root.val);
        return res;
        
    }
}

23、二叉树的最近公共祖先(LeetCode 236)(√)

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null||root==p||root==q)return root;
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        if(left==null) return right;
        if(right==null) return left;
        return root;
    }
}

(晚上做:6-10)

23、全排列(LeetCode 46) (√)

class Solution {
    List<List<Integer>> lists;
    List<Integer> list;
    boolean[] visit;
    public List<List<Integer>> permute(int[] nums) {
        lists=new ArrayList();
        list=new ArrayList();
        visit=new boolean[nums.length];
        dfs(nums,0);
        return lists;
    }
    public void dfs(int[] nums,int x){
        if(x==nums.length){
            lists.add(new ArrayList(list));
            return;
        }
        for(int i=0;i<nums.length;i++){
            if(!visit[i]){
                visit[i]=true;
                list.add(nums[i]);
                dfs(nums,x+1);
                visit[i]=false;
                list.remove(list.size()-1);
            }
        }
    }
}

24、子集(LeetCode 78) (√)

class Solution {
    List<List<Integer>> lists;
    List<Integer> list;
    public List<List<Integer>> subsets(int[] nums) {
        lists=new ArrayList();
        list=new ArrayList();
        dfs(nums,0);
        return lists;
    }
    public void dfs(int[] nums,int x){
        if(x==nums.length){
            lists.add(new ArrayList(list));
            return;
        }
        list.add(nums[x]);
        dfs(nums,x+1);
        list.remove(list.size()-1);
        dfs(nums,x+1);
    }
}

25、 搜索插入位置(LeetCode 35) (√)

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target)return mid;
            else if(nums[mid]<target) left=mid+1;
            else right=mid-1;
        }  
        return  left;
    }
}

26、搜索旋转排序数组(LeetCode 33) (√)

class Solution {
    public int search(int[] nums, int target) {
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target) return mid;
            if(nums[mid]>=nums[left]){   //左侧有序
                 if(nums[left]<=target&&nums[mid]>target){
                     right=mid-1;
                 }else{
                    left=mid+1;
                 }
            }else{
                if(nums[right]>=target&&nums[mid]<target){
                    left=mid+1;
                }else{
                    right=mid-1;
                }
            }
        }
        return -1;
    }
}

27、有效的括号(LeetCode 20) (√)

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack();
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{'){
                stack.push(s.charAt(i));
            }else if(s.charAt(i)==')'){
                if(stack.isEmpty()||stack.pop()!='('){
                    return false;
                }
            }else if(s.charAt(i)==']'){
                if(stack.isEmpty()||stack.pop()!='['){
                    return false;
                }
            }else{
                if(stack.isEmpty()||stack.pop()!='{'){
                    return false;
                }
            }
        }
        if(!stack.isEmpty()) return false;
        return true;
        
    }
}

28、每日温度(LeetCode 739) (√)

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack=new Stack();
        int[] answer=new int[temperatures.length];
        for(int i=0;i<temperatures.length;i++){
            while(!stack.isEmpty()&&temperatures[i]>temperatures[stack.peek()]){
                int num=stack.pop();
                answer[num]=i-num;
            }
            stack.push(i);
        }
        return answer;
    }
}

29、最小栈(LeetCode 155) (√)

 class MinStack {
        Stack<Integer> xstack;
        Stack<Integer> minstack;
        public MinStack() {
            xstack=new Stack<>();
            minstack=new Stack<>();
        }
        public void push(int val) {
            xstack.push(val);
            if(minstack.isEmpty()) minstack.push(val);
            else minstack.push(Math.min(val,minstack.peek()));
        }
        public void pop() {
            xstack.pop();
            minstack.pop();
        }
        public int top() {
            return xstack.peek();
        }
        public int getMin() {
            return minstack.peek();
        }
    }

30、数组中的第K个最大元素(LeetCode 215) (√)

class Solution {
    public int findKthLargest(int[] nums, int k) {
         return quciksort(nums,0,nums.length-1,nums.length-k);
    }
    public int quciksort(int[] nums,int left,int right,int k){
       if(left<right){
            int pivot=partition(nums,left,right);
            if(k==pivot) return nums[pivot];
            else if(k>pivot) return quciksort(nums,pivot+1,right,k);
            else return quciksort(nums,left,pivot-1,k);    
       }
       return nums[left];
           
    }
    public int partition(int[] nums,int left,int right){
        int pivot=nums[left],i=left,j=right;
        while(i<j){
            while(i<j&&nums[j]>pivot) j--;
            while(i<j&&nums[i]<=pivot) i++;
            if(i<j) swap(nums,i,j);
        }
        swap(nums,left,i);
        return i;
    }
    public void swap(int[] nums,int left,int right){
        int temp=nums[left];
        nums[left]=nums[right];
        nums[right]=temp;
    }
}

31、岛屿数量(LeetCode 200) (√)

class Solution {
    int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
    public int numIslands(char[][] grid) {
        int res=0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                 if(grid[i][j]=='1'){
                     grid[i][j]='0';
                     res++;
                     dfs(grid,i,j);
                 }
            }
        }
        return res;
    }
    public void dfs(char[][] grid,int x,int y){
        for(int i=0;i<4;i++){
            int xx=x+dir[i][0],yy=y+dir[i][1];
            if(xx>=0&&xx<grid.length&&yy>=0&&yy<grid[0].length&&grid[xx][yy]=='1'){
                grid[xx][yy]='0';
                dfs(grid,xx,yy);
            }
        }
    }
}

32、买卖股票的最佳时机(LeetCode 121)(√)

class Solution {
    public int maxProfit(int[] prices) {
        int min=prices[0],res=0;
        for(int i=1;i<prices.length;i++){
            res=Math.max(res,prices[i]-min);
            min=Math.min(min,prices[i]);
        }
        return res;
    }
}

33、爬楼梯(LeetCode 70) (√)

class Solution {
    public int climbStairs(int n) {
        int[] dp=new int[n+1];
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;i++){
            dp[i]=dp[i-1]+dp[i-2];
        }
        return dp[n];

    }
}

34、打家劫舍(LeetCode 198) (√)

class Solution {
    public int rob(int[] nums) {
        if(nums.length==1)return nums[0];
        int[] dp=new int[nums.length];
        dp[0]=nums[0];
        dp[1]=Math.max(nums[0],nums[1]);
        for(int i=2;i<nums.length;i++){
            dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
        }
        return dp[nums.length-1];
    }
}

35、最长递增子序列(LeetCode 300) (√)

class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp=new int[nums.length];
        dp[0]=1;
        int max=1;
        for(int i=1;i<nums.length;i++){
            dp[i]=1;
            for(int j=i-1;j>=0;j--){
                if(nums[i]>nums[j]){
                    dp[i]=Math.max(dp[i],dp[j]+1);
                }
            }
            max=Math.max(max,dp[i]);
        }
        return max;
    }
}

36、最长回文子串(LeetCode 5) (√)

class Solution {
    public String longestPalindrome(String s) {
        int n=s.length();
        if(n<2)return s;
        boolean[][] dp=new boolean[n][n];
        int start=0,maxLen=1;
        for(int i=0;i<n;i++){
            dp[i][i]=true;
        }   
        for(int i=0;i<n-1;i++){
            if(s.charAt(i)==s.charAt(i+1)){
                dp[i][i+1]=true;
                start=i;
                maxLen=2;
            }
        }
        for(int L=3;L<=n;L++){
            for(int i=0;i<=n-L;i++){
                    int j=i+L-1;
                    if(s.charAt(i)==s.charAt(j)&&dp[i+1][j-1]){
                        dp[i][j]=true;
                        if(L>maxLen){
                            start=i;
                            maxLen=L;
                        }
                    }
            }
        }
        return s.substring(start,start+maxLen);
    }
}

37、零钱兑换(LeetCode 322) (√)

class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp=new int[amount+1];
        dp[0]=0;
        for(int i=1;i<=amount;i++){
            dp[i]=Integer.MAX_VALUE;
            for(int j=0;j<coins.length;j++){
                if(i-coins[j]>=0&&dp[i-coins[j]]!=-1){
                    dp[i]=Math.min(dp[i],dp[i-coins[j]]+1);
                }
            }
            if(dp[i]==Integer.MAX_VALUE)dp[i]=-1;
        }
        return dp[amount];
    }
}

38、快速排序(排序算法常考)(LeetCode 912) (√)

class Solution {
    public int[] sortArray(int[] nums) {
        quicksort(nums,0,nums.length-1);
        return nums;
    }
    public void quicksort(int[] nums,int left,int right){
        if(left<right){
            int pivot=partition(nums,left,right);
            quicksort(nums,left,pivot-1);
            quicksort(nums,pivot+1,right);
        }
    }
    public int partition(int[] nums,int left,int right){
        int pivot=nums[left],i=left,j=right;
        while(i<j){
            while(i<j&&nums[j]>=pivot) j--;
            while(i<j&&nums[i]<=pivot) i++;
            if(i<j) swap(nums,i,j);
        }
        swap(nums,i,left);
        return i;
    }
    public void swap(int[] nums,int left,int right){
        int temp=nums[left];
        nums[left]=nums[right];
        nums[right]=temp;
    }
}

39、归并排序(排序算法常考)(LeetCode 912) (x)

class Solution {
    int[] temp;
    public int[] sortArray(int[] nums) {
        temp=new int[nums.length];
        mergesort(nums,0,nums.length-1);
        return nums;
    }
    public void mergesort(int[] nums,int left,int right){
        if(left<right){
            int mid=(left+right)/2;
            mergesort(nums,left,mid);
            mergesort(nums,mid+1,right);
            merge(nums,left,mid,right);
        }
    }
    public void merge(int[] nums,int left,int mid,int right){
        System.arraycopy(nums,left,temp,left,right-left+1);
        int i=left,j=mid+1,k=left;
        while(i<=mid&&j<=right){
            if(temp[i]<=temp[j]){
                 nums[k++]=temp[i++];
            }else{
                nums[k++]=temp[j++];
            }
        }
        while(i<=mid)nums[k++]=temp[i++];
        while(j<=right)nums[k++]=temp[j++];
    }
}

40、多线程打印ABC (√)

public class ABCPrint {
    public static final Object lock=new Object();
    public static int state=0;
    public static void main(String[] args) {
        new Thread(()->print('A',0)).start();
        new Thread(()->print('B',1)).start();
        new Thread(()->print('C',2)).start();
    }
    public static void print(char ch,int targetNow){
        for(int i=0;i<10;i++){
            synchronized(lock){
                while(state%3!=targetNow){
                    try {
                        lock.wait();
                    } catch (Exception e) {
                    }
                }
                System.out.print(ch);
                state++;
                lock.notifyAll();
            }
        }
    }
}

41、单例模式 (√)

双重检查锁

public class Singleton{
    private static volatile Singleton instance;
    private Singleton(){}
    public static Singleton getInstance(){
        if(instance==null){
            synchronized(Singleton.class){
                if(instance==null){
                    instance=new Singleton();
                }
            }
        }
        return instance;
    }
}

42、将整数减少到零需要的最少操作数(LeetCode 2571) (x)

class Solution {
    public int minOperations(int n) {
        int count=0;
        while(n>0){
            int bitNumber=0;
            while(n>0&&(n&1)==1){
                bitNumber++;
                n>>=1;
            }
            if(bitNumber==1) count++;
            else if(bitNumber>=2){
                count++;
                n+=1;
            }else{
                n>>=1;
            }
        }
        return count;
    }
}

43、 用栈实现队列(LeetCode 232) (x)

class MyQueue {
    Stack<Integer> inStack;
    Stack<Integer> outStack;
    public MyQueue() {
        inStack=new Stack<Integer>();
        outStack=new Stack<Integer>();
    }
    
    public void push(int x) {
        inStack.push(x);
    }
    
    public int pop() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.pop();
    }
    
    public int peek() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.peek();
    }
    
    public boolean empty() {
        return inStack.isEmpty()&&outStack.isEmpty();
    }

    public void in2out(){
         while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
         }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值