力扣 ---- java

文章目录

#31

public class No31 {
    public static void main(String[] args) {
        nextPermutation(new int[]{1,2,3,6,4,6,5});
    }
    public static void nextPermutation(int[] nums) {

        int i = 0, j = 0, k = 0, left = 0,right = nums.length - 1;//定义基础变量
        //从后往前循环,索引从 nums.length --> 1,也就是循环 length - 1 次
        for (int x = nums.length - 1; x > 0; --x) {//寻找i,j
            if (nums[x] > nums[x-1]){
                i=x-1;//升序中心点上一个位置
                j=x;//记录从后往前,第一个升序的中心点位置
                break;
            }
        }

        boolean desc = (i == j);
        if (!desc){
            //查找k,交换i,k
            for(int y=nums.length-1;y>=j;--y){
                if(nums[y]>nums[i]){
                    k=y;
                    break;//跳出循环
                }
            }
            int temp=nums[k];
            nums[k]=nums[i];
            nums[i]=temp;//交换 nums k,  i
            left=j;//边界点
        }

        while(left<right){
            int temp1=nums[left];
            nums[left]=nums[right];
            nums[right]=temp1;//交换 nums left,right
            right--;
            left++;
        }
    }
}

#32

    @Test
    public void test(){



//        String s = "";//0
        String s = "(";
        System.out.println(this.longestValidParentheses(s));
    }

    /**
     *
     */
    public int longestValidParentheses(String s) {
        int max = 0;//保存最大配对长度

        //字符串长度为0,直接返回0
        if (s.length() == 0){
            return max;//0
        }

        char[] chars = s.toCharArray();//字符串转为字符数组

        int count = 0;//记录有效配对的括号数量
        int p = 0;//循环遍历,循环数组

        char[] mystack = new char[chars.length];//创建字符长度的字符数组,模拟栈
        int ps = -1;//栈顶指针
        int match=0;
        while (p < chars.length) {//循环每个字符
            if (chars[p] == '('){
                match++;
                mystack[++ps]='(';
            } else if (chars[p] == ')'){
                match--;
                mystack[++ps]=')';
                if (match >= 0){
                    count+=2;
                }else if(match < 0){//右括号多余左括号
                    max = Math.max(max,count);
                    count=0;
                    match=0;
                    ps=-1;
                }
            }
            p++;
        }
        match=0;
        count=0;
        while (ps >=0){
            if (mystack[ps] == ')'){
                match++;
            }else if (mystack[ps] == '('){
                match--;
                if(match>=0){
                    count+=2;
                }else if (match<0){
                    max=Math.max(max,count);
                    count=0;
                    match=0;
                }
            }
            ps--;
        }
        max = Math.max(max,count);

        return max;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠闲的线程池

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值