首页 > 其他 > 详细

Shopee代码准备

时间:2021-06-10 12:28:13      阅读:26      评论:0      收藏:0      [点我收藏+]

相交链表

160
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null)return null;
        int la =0,lb =0,dlt=0;
        ListNode ta = headA,tb = headB;
        while(ta!=null){la++;ta=ta.next;}
        while(tb!=null){lb++;tb= tb.next;}
        dlt = la-lb;
        ta = headA ;tb = headB;
        while(dlt!=0){
            if(dlt>0){ta=ta.next;dlt--;}
            if(dlt<0){tb=tb.next;dlt++;}
        }
        while(ta!=tb){
            ta = ta.next;
            tb = tb.next;
        }
        return ta;
    }
}

704二分查找

class Solution {
    public int search(int[] nums, int target) {
        int l = 0,r = nums.length-1;
        while(l<=r){
            int m = (l+r)/2;
            if(nums[m]<target){
                l = m+1;
            }
            else if(nums[m]>target) {
                r = m-1;
            }
            else{
                return m;
            }
        }
        return -1;
    }
}

232 栈实现队列

class MyQueue {
LinkedList<Integer> in,out;
        public MyQueue(){
            in = new LinkedList<>();
            out = new LinkedList<>();
        }

        public void push(int x){
            while(!out.isEmpty()){
                in.push(out.pop());
            }
            in.push(x);
        }

        public int pop(){
            while(!in.isEmpty()){
                out.push(in.pop());
            }
            return out.pop();
        }

        public int peek(){
            while(!in.isEmpty()){
                out.push(in.pop());
            }
            return out.peek();
        }

        public boolean empty(){
            return in.isEmpty()&&out.isEmpty();
        }
}

Shopee代码准备

原文:https://www.cnblogs.com/zhouyu0-0/p/14869644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!