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 — LeetCode/L093_Restore_IP_Addresses.java at master · likevson/LeetCode Expand file tree/ Copy pathL093_Restore_IP_Addresses.javaMore file actions 64 lines (50 loc) · 1.1 KB / Copy pathL093_Restore_IP_Addresses.javaFile metadata and controls64 lines (50 loc) · 1.1 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 package LeetCode; import java.util.ArrayList; import java.util.List; public class L093_Restore_IP_Addresses { List<String> rt = new ArrayList<String>(); String[] stack = new String[4]; public List<String> restoreIpAddresses(String s) { if (s == null || s.length() == 0) { return new ArrayList<String>(); } dfs(s, 0, 0); return rt; } /** * * @param s * @param p * :指针 * @param pstack * :stack的下标 */ public void dfs(String s, int p, int pstack) { if (pstack == 4) { // 如果stack长度为4,且s的字符全部用上 // 则stack[0...3]存了一个结果 if (p >= s.length()) { String ip = String.join(".", stack); rt.add(ip); } return; } // 获取1~3个字符 for (int i = 1; i <= 3; i++) { // 如果超过字符串长度,返回 if (p + i > s.length()) { return; } // 若选取的第一个字符是0,则停止选取 if (i > 1 && s.charAt(p) == '0') { continue; } String number = s.substring(p, p + i); // 如果number<=255,递归 if (Integer.parseInt(number) <= 255) { stack[pstack] = number; dfs(s, p + i, pstack + 1); } } } } |
| You are visitor 01402792 — 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. |