LeedCode刷题笔记(第二周)(1)

82.删除所有重复的结点

    public ListNode deleteDuplicates(ListNode head) {
        ListNode n=new ListNode(0,head);
        ListNode cur=n;
        while(cur.next!=null&&cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int v=cur.next.val;
                while(cur.next!=null&&cur.next.val==v){
                    cur.next=cur.next.next;
                }
            }else{
                cur=cur.next;
            }
        }
        return n.next;
    }

7.整数反转

思路:(用StringBuffer)

代码:

    public int reverse(int x) {
        StringBuffer sb=new StringBuffer();
        if(x==0){
            return 0;
        }else if(x>0){
            sb.append(x);
            sb.reverse();
            try{
                return Integer.parseInt(sb.toString());
            }catch(Exception e){
                return 0;
            }
            
        }else{
            x=-1*x;
            sb.append(x);
            sb.reverse();
            try{
                return -1*Integer.parseInt(sb.toString());
            }catch(Exception e){
                return 0;
            }
        }
    }

 

61.旋转链表

思路:(双指针)

代码:

    public ListNode rotateRight(ListNode head, int k) {
        int count=0;
        ListNode cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        if(count==0||k%count==0)
            return head;

        int weizhi=count-k%count;
        cur=head;
        while(weizhi>0){
            if(weizhi==1){
                ListNode wei=cur;
                ListNode nn=head;
                head=cur.next;
                while(cur.next!=null){
                    cur=cur.next;
                }
                cur.next=nn;
                wei.next=null;
            }
            cur=cur.next;
            weizhi--;
        }
        return head;
    }

 

19.删除链表倒数第n个结点

思路:(指针)

代码:

     public ListNode removeNthFromEnd(ListNode head, int n) {
        if(n==0||head==null)
            return head;
        int count=0;
        ListNode cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        int weizhi=count-n+1;
        if(weizhi==1)
            return head.next;
        cur=head;
        while(weizhi>0){
            if(weizhi==2){
                cur.next=cur.next.next;
            }
            if(cur!=null)
                cur=cur.next;
            weizhi--;
        }
        return head;
    }

 

3.无重复字符串

思路:(双指针)

代码:

 

    public int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> map=new HashMap<>();
        int start=0;
        int max=0;
        for (int end=0;end<s.length();end++){
            char c=s.charAt(end);
            if(map.containsKey(c)){
                start=Math.max(map.get(c)+1,start);
            }
            map.put(c,end);
            max=Math.max(max,end-start+1);
        }
        return max;
    }

33.搜索旋转排序数组

思路:(HashMap)

用hashmap进行关键字查找

代码:

    public int search(int[] nums, int target) {
        if(nums.length==0){
            return -1;
        }
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<nums.length;i++){
            Integer s=nums[i];
            map.put(s,i);
        }
        if(!map.containsKey(target)){
            return -1;
        }
        return map.get(target);
    }

思路:(枚举+二分)

一个个列举出来,剩下的用分治

代码:

    public int search(int[] nums, int target) {
        int spin=-1;
        int len=nums.length;
        if(len==1){
            return target==nums[0]?0:-1;
        }
        for(int i=1;i<len;i++){
            if(nums[i]<nums[i-1]){
                spin=i-1;
                break;
            }
        }
        int left=0;
        int right=len-1;
        if(spin!=-1){
            if(target==nums[0])
                return 0;
            else if(target==nums[spin])
                return spin;
            else if(target==nums[spin+1])
                return spin+1;
            if(target>nums[spin]||target<nums[spin+1])
                return -1;
            else if(target>nums[len-1]){
                left=0;
                right=spin;
            }else if(target<nums[0]){
                left=spin+1;
                right=len-1;
            }else if(target==nums[len-1])
                return len-1;
            
        }
        while(left<=right){
            int mid=(left+right)>>1;
            if(nums[mid]==target)
                return mid;
            else if(nums[mid]<target){
                left=mid+1;
                continue;
            }else if(nums[mid]>target){
                right=mid-1;
                continue;
            }
        }
        return -1;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值