GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Algorithm/java/coding/decodeString.java at master · lelea2/Algorithm Expand file tree/ Copy pathdecodeString.javaMore file actions 77 lines (69 loc) · 1.53 KB / Copy pathdecodeString.javaFile metadata and controls77 lines (69 loc) · 1.53 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 //s = "3[a]2[bc]", return "aaabcbc". // s = "3[a2[c]]", return "accaccacc". // s = "2[abc]3[cd]ef", return "abcabccdcdcdef". class Node { int num; ArrayList<Node> list; char symbol; boolean isList; public Node(char s) { symbol=s; } public Node(int n){ list = new ArrayList<Node>(); isList=true; num=n; } public String toString(){ String s = ""; if(isList) { s += num + ":" + list.toString(); } else { s += symbol; } return s; } } public class Solution { public String decodeString(String s) { int i = 0; Stack<Node> stack = new Stack<Node>(); stack.push(new Node(1)); String t = ""; while (i < s.length()) { char c = s.charAt(i); // new Node if (c >= '0' && c <= '9') { // number t += c; } else if (c == '[') { if (t.length() > 0) { int num = Integer.parseInt(t); stack.push(new Node(num)); t = ""; } } else if (c == ']') { Node top = stack.pop(); if (stack.isEmpty()) { } else { stack.peek().list.add(top); } } else { stack.peek().list.add(new Node(c)); } i++; } return getString(stack.peek()); } public String getString(Node node) { String s= ""; if (node.isList) { for(int i=0; i<node.num; i++){ for(Node t: node.list) s+= getString(t); } } else { s+=node.symbol; } return s; } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |