zoukankan      html  css  js  c++  java
  • 求最深字符串Output the most inner string

    1. Output the most inner string:  "ab(cd(e)fg)" => "e", "Hello World!" =>"Hello World!" "ab{cd[ef]} gh(hello{hi})" =>"ef", "hi"

    先用https://www.geeksforgeeks.org/find-maximum-depth-nested-parenthesis-string/

     

    // Traverse the input string  
            for (int i = 0; i < n; i++) { 
                if (S.charAt(i) == '(') { 
                    current_max++; 
      
                    // update max if required  
                    if (current_max > max) { 
                        max = current_max; 
                    } 
                } else if (S.charAt(i) == ')') { 
                    if (current_max > 0) { 
                        current_max--; 
                    } else { 
                        return -1; 
                    } 
                } 
            } 

     

    max和current_max求出最大深度。然后再来一次,最大深度的时候append

     

    或者用:https://stackoverflow.com/questions/12595019/how-to-get-a-string-between-two-characters

    一个不需要您执行indexOf的问题的非常有用的解决方案是使用Apache Commons库。

     StringUtils.substringBetween(s, "(", ")");

     

  • 相关阅读:
    Spring Boot
    AWS DynamoDB
    VBA读excel写xml
    WebSocket API 学习
    故障排除 Mybatis ORA-01000 和 本地缓存问题
    Java基础
    Java Tutorials Lambda表达式 翻译
    在代理环境中构建maven环境
    Pom
    我的JAVA笔记
  • 原文地址:https://www.cnblogs.com/immiao0319/p/14381512.html
Copyright © 2011-2022 走看看