leetCode刷题day06

一:方法总结

1.优先队列PriorityQueue

先进先出:队列头的最先出去
使用comparator实现类对队列的元素排序
常用方法:add( ) 实际调用offer( ) 加入元素,返回Boolean,元素为null会抛异常
                  isEmpty( )
                  size( )
                  poll( ):弹出队列头,队列头为空则返回null
                  peek( ):查看队列头,为空则返回null
                   remove(index):删除元素,返回Boolean,index不写默认删除队列头

2.二分法

左右两个指针i、j ,循环条件(i<=j),比较再更新指针i=(i+j)/2+1、j=(i+2)/2-1;

3.ArratList

构造器:new ArrayList(list) 将把list的内容复制进新对象
add( )
remove(index/obj)
ArrayList当形参是,当实参赋值后,方法体内的局部变量改变会引起实参的改变(引用数据类型)

二:题目

23.合并K个升序链表

思路:将每个链表的第一个结点都加入堆并按升序排序,堆弹出的为最小数结点,并将该结点的下一个结点加入堆,直到所有结点加入完

public ListNode mergeKLists1(ListNode[] lists) {
        PriorityQueue<ListNode> p=new PriorityQueue(new Comparator<ListNode>(){
            @Override
            public int compare(ListNode o1, ListNode o2) {
                if (o1.val>o2.val)
                    return 1;
                else if (o1.val==o2.val)
                return 0;
                else return -1;
            }
        });
        for (int i=0;i<lists.length;i++){
            if (lists[i]!=null)
                p.offer(lists[i]);
        }
        ListNode head= new ListNode(Integer.MIN_VALUE);
        ListNode temp=head;
        while (!p.isEmpty()){
            ListNode cur=p.poll();//队列的队头
            temp.next=cur;
            temp=temp.next;
            if (cur.next!=null)
                p.offer(cur.next);
        }
        return head.next;
    }

31.下一个队列

将给定数字序列重新排列成字典序中下一个更大的排列(即,组合出下一个更大的整数)
思路:如果从末尾看是升序,则这个排列是最小值,从末尾找中断这个升序的数 i ,再在升序中找比 i 大一点的数字 j,交换i、j,再将末尾j以后的数字升序排列

public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        int j = nums.length - 1;
        while (i >= 0 && nums[i] >= nums[i + 1]) {//从末尾找到最小的数字
            i--;
        }
        if (i >= 0) {
            while (nums[i] >= nums[j]) {
                j--;
            }

            //交换两个数
            nums[i] = nums[i] + nums[j];
            nums[j] = nums[i] - nums[j];
            nums[i] = nums[i] - nums[j];
        }
        //对末尾升序排序
        i = i + 1;
        j = nums.length - 1;
        while (i < j) {
            nums[i] = nums[i] + nums[j];
            nums[j] = nums[i] - nums[j];
            nums[i] = nums[i] - nums[j];
            i++;
            j--;
        }
    }

32.最长有效括号

给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度
思路:用字符串的index值记录有效字符串的长度。先在栈中放一个-1,遇到( 则入栈,遇到)则出栈,此时栈不为空,说明出栈的是( ,记录长度;栈为空,说明出栈的不是( ,子串无效并将此时的)入栈,方便记录长度。

public int longestValidParentheses1(String s) {
        int maxans = 0;
        Deque<Integer> stack = new LinkedList<Integer>();
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push(i);
            } else {
                stack.pop();
                if (stack.isEmpty()) {
                    stack.push(i);
                } else {
                    maxans = Math.max(maxans, i - stack.peek());
                }
            }
        }
        return maxans;
    }

33.搜索旋转排序数组

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转
思路:先找旋转点,再进行二分查找

public int search(int[] nums, int target) {
        int i=1;
        int j=0;
        //找旋转点i
        for (;i<nums.length;i++){
            if (nums[i]<nums[i-1])
                break;
        }
        if (target>=nums[0]) {//target在左半段
            i = i - 1;
        } else if (target<=nums[nums.length-1]){//右半段
            j=i;
            i=nums.length-1;
        }
            while (j <= i) {//二分查找
                if (target > nums[(j + i) / 2])
                    j = (j + i ) / 2 + 1;
                else if (target == nums[(j + i ) / 2])
                    return (j + i ) / 2;
                else i = (j + i ) / 2 - 1;
            }
        return -1;
    }

34.在排序数组中查找target的起始位置和结束位置

思路:使用二分法,分两次while循环,一次向左找最小值,一次向右找最大值

public int[] searchRange(int[] nums, int target) {
        if (nums.length == 0)
            return new int[]{-1, -1};
        int i = 0;
        int j = nums.length - 1;
        int[] ans = new int[2];
        int left = -1;
        int righ = -1;
        while (i <= j) {//向左找最小
            if (nums[(i + j) / 2] > target)
                j = (i + j) / 2 - 1;
            else if (nums[(i + j) / 2] == target) {
                left = (i + j) / 2;
                j = (i + j) / 2 - 1;
            } else i = (i + j) / 2 + 1;
        }
        i = 0;
        j = nums.length - 1;
        while (i <= j) {//向右找最大
            if (nums[(i + j) / 2] > target)
                j = (i + j) / 2 - 1;
            else if (nums[(i + j) / 2] == target) {
                righ = (i + j) / 2;
                i = (i + j) / 2 + 1;
            } else i = (i + j) / 2 + 1;
        }
        ans[0] = left;
        ans[1] = righ;
        return ans;
    }

39.组合总和

给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合。
输入: candidates = [2,3,6,7], target = 7 输出: [[7],[2,2,3]]

public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        for (int i=0;i<candidates.length;i++){
        dfs1(ans,temp,candidates,target,i);
        }
        return ans;
    }
    public void dfs1(List<List<Integer>> ans,List<Integer> temp,int[] candidates,int target,int i){
        if (candidates[i]>target)
            return;
        if (candidates[i]==target){
            temp.add(candidates[i]);
            ans.add(new ArrayList<>(temp));
            temp.remove(temp.size()-1);
            return;
        }
        else {
            temp.add(candidates[i]);
            for (int j=i;j<candidates.length;j++){
                dfs1(ans,temp,candidates,target-candidates[i],j);
            }
            temp.remove(temp.size()-1);
        }

42.接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

思路:先找出高度最高的柱子,分成两部分。

public int trap(int[] height) {
        if(height.length<3)
            return 0;
        int max=0;//最高柱子的值
        int m=0;//最高柱子的下标
        int ans=0;//保存结果
        for (int i=0;i<height.length;i++){
            if (height[i]>max){
                max=height[i];
                m=i;
            }
        }
        //最高向左找
        int j=0;
        while (height[j]==0)
            j++;
        int temp=height[j];//保存左半部分的最高柱子
        for (;j<m;j++){
            if (height[j]<temp){
                ans=ans+temp-height[j];
            }else temp=height[j];//更新左半部分的最高柱子
        }
        //最高向右找
        j=height.length-1;
        while (height[j]==0)
            j--;
        temp=height[j];//保存右半部分的最高柱子
        for (;j>m;j--){
            if (height[j]<temp)
                ans=ans+temp-height[j];
            else temp=height[j];//更新右半部分的最高柱子
        }
            return ans;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值