Map<Character, Integer> map = new LinkedHashMap<>();
//Insert one char from stringstream publicvoidInsert(char ch){ if (map.containsKey(ch)) { map.put(ch, map.get(ch) + 1); } else { map.put(ch, 1); } }
//return the first appearence once char in current stringstream publiccharFirstAppearingOnce(){ Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); if ((int) entry.getValue() == 1) { return (char) entry.getKey(); } } return'#'; }
publicbooleanisNumeric(char[] str){ String s = new String(str); if (s.startsWith("+") || s.startsWith("-")) { s = s.substring(1); } if (s.matches("[0-9]+.*")) { s = s.replaceFirst("[0-9]+", ""); if ("".equals(s)) { returntrue; } }
if (s.startsWith(".")) { s = s.substring(1); if (!s.matches("[0-9]+.*")) { returnfalse; } s = s.replaceFirst("[0-9]+", ""); } if ("".equals(s)) { returntrue; }
if (s.startsWith("e") || s.startsWith("E")) { s = s.substring(1); if (s.startsWith("+") || s.startsWith("-")) { s = s.substring(1); } if (!s.matches("[0-9]+")) { returnfalse; } else { returntrue; } } else { returnfalse; } }
voidbfs(){ vis[] = {0}; // or set queue<int> pq(start_val);
while (!pq.empty()) { int cur = pq.front(); pq.pop(); for (遍历cur所有的相邻节点nex) { if (nex节点有效 && vis[nex]==0){ vis[nex] = 1; pq.push(nex) } } // end for } // end while }
voidbfs(){ int level = 0; vis[] = {0}; // or set queue<int> pq(original_val); while (!pq.empty()) { int sz = pq.size();
while (sz--) { int cur = pq.front(); pq.pop(); for (遍历cur所有的相邻节点nex) { if (nex节点有效 && vis[nex] == 0) { vis[nex] = 1; pq.push(nex) } } // end for } // end inner while level++;
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); if (pRoot == null) { return lists; }
int curLevelNum = 1, nextLevelNum = 0, num = 0; Queue<TreeNode> queue = new LinkedList<>(); queue.add(pRoot); ArrayList<Integer> tmpList = new ArrayList<>(); while (!queue.isEmpty()) { TreeNode t = queue.poll();
if (t.left != null) { queue.add(t.left); nextLevelNum++; } if (t.right != null) { queue.add(t.right); nextLevelNum++; }
num++; tmpList.add(t.val); if (num == curLevelNum) { curLevelNum = nextLevelNum; nextLevelNum = 0; num = 0; lists.add(tmpList); tmpList = new ArrayList<>(); }
voidbfs(){ vis[] = {0}; // or set queue<int> pq(start_val);
while (!pq.empty()) { int cur = pq.front(); pq.pop(); for (遍历cur所有的相邻节点nex) { if (nex节点有效 && vis[nex]==0){ vis[nex] = 1; pq.push(nex) } } // end for } // end while }
voidbfs(){ int level = 0; vis[] = {0}; // or set queue<int> pq(original_val); while (!pq.empty()) { int sz = pq.size();
while (sz--) { int cur = pq.front(); pq.pop(); for (遍历cur所有的相邻节点nex) { if (nex节点有效 && vis[nex] == 0) { vis[nex] = 1; pq.push(nex) } } // end for } // end inner while level++;