首页 > 代码库 > decode-string
decode-string
https://leetcode.com/problems/decode-string/public class Solution { private String s; private int newPos; public String decodeString(String ins) { s = ‘.‘ + ins + ‘]‘; newPos = 0; String outStr = impl(1, 0); return outStr.substring(1, outStr.length()); } private String impl(int prefix, int startPos) { int base = 0; String baseStr = ""; String outStr = ""; for (int i=startPos; i<s.length(); i++) { char ch = s.charAt(i); if (ch == ‘[‘) { int tmpPos = i+1; baseStr += impl(base, tmpPos); i = newPos; base = 0; } else if (ch == ‘]‘) { for (int j=0; j<prefix; j++) { outStr += baseStr; } // At begin, use i+1, is wrong, // because in each loop there‘s i++ newPos = i; return outStr; } else if (!Character.isDigit(ch)){ baseStr += ch; } else { base = base * 10 + ch - ‘0‘; } } return outStr; }}
decode-string
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。