字符反转和括号合法化以及深度算法

给定一组括号,然后计算括号合法性, 有没有 正常闭合等, 括号深度待解.....


/*
题目:给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效有效字符串需满足:
        1、左括号必须用相同类型的右括号闭合。
        2、左括号必须以正确的顺序闭合。
        注意:空字符串可被认为是有效字符串。

  二、分析:
看到这个题目,应该联想到栈
首先遍历这个字符串的每个元素c,
如果 c 是左边括号,那么就让与该括号相对于的右边括号入栈
如果 c 是右边括号,就判断 c 是否与之前入栈的元素相同
        */
import java.util.Stack;
import java.util.Vector;

public class arrayListDemo {

    public static void main(String arg[]){
        String s="(())[()]{}";
        Stack<Character>  stack = new Stack();
        for (char c:s.toCharArray()){
            if (c=='('){
                stack.push(')');
            }else if (c=='['){
                stack.push(']');
            }else if (c=='{'){
                stack.push('}');
            }else {
                if (stack.isEmpty() || stack.pop()!=c){
                    System.out.println("false");
                    return ;
                }
            }
        }
        System.out.println("true");
    }

 //括号深度
        //Scanner sin = new Scanner(System.in);
       // String strin = sin.nextLine();
        String ins="(({})()(([])){{{({[]})}}})";
        int count=0,max=0;
        for (char c:ins.toCharArray()){
            if (c=='('||c=='{'||c=='['){
                count++;
            }else{
                count--;
            }
            max = max>count?max:count;
        }
        System.out.println("最大深度: "+max);
    }

}

 

 

给定字符串反转 , 可以再赋值给数组

  String[] stc= new String[10];     

String s = "i abc cai mig Li testdevlope ok frist aa bbb ccc";
        String str[]=s.split(" ");
        //给一个数据赋值,直接翻转整个语句
        for (int i=str.length-1;i>=0;i--){
            System.out.print(str[i]+" ");
        }
       // System.out.println("&&&&&&");
        //下面处理,指定区间 翻转语句,重点是交换条件,把前面数组换到后面去
//(int k=it,i=ins;k<i&&k!=i;i--,k++) 条件是重点,就是区间交换  k!=i 是两个下标数组相同,就不交换,稳定排序
        int it=2,ins=5;
        String temp,tempt;
        if (it>ins || it<0 || ins<0)
            return ;
        for(int k=it,i=ins;k<i&&k!=i;i--,k++){
            temp = str[i];
            tempt = str[k];
            str[i] = tempt;//前面数组换到后面
            str[k] = temp;//后面数据换到前面
        }
        System.out.println("=====");
        for(int i=0;i<str.length;i++){
            System.out.print(str[i]+" ");
        }

第二种字符串反转  通过str.toCharArray();

 1 public class StringReverse {
 2     public static void main(String[] args) {
 3         String str="hello";
 4         System.out.println(reverse(str));
 5 
 6     }
 7     public static String reverse(String str){
 8         char[] chars = str.toCharArray();
 9         String ans="";
10         for (int i = chars.length - 1; i >= 0; i--) {
11             ans+=chars[i];
12         }
13         return ans;
14     }
15 }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值