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/code/lc131.java at master · mJackie/leetcode Expand file tree/ Copy pathlc131.javaMore file actions 55 lines (52 loc) · 1.77 KB / Copy pathlc131.javaFile metadata and controls55 lines (52 loc) · 1.77 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 package code; import java.util.ArrayList; import java.util.List; /* * 131. Palindrome Partitioning * 题意:回文切割,找出所有切法 * 难度:Medium * 分类:Backtracking * 思路:典型回溯法,注意向res添加内容时要重新new一下 * Tips: lc5, lc9, lc125, lc131, lc234, lc647 * lc39 * lc132 * 判断是否为回文的方法: * 1. 从中心往两边扩充,中心可能是一个字符,也可能是两个字符 * 2. dp,利用之前计算的结果,只判断边缘两个字符是否相等,从后往前dp * 3. 翻转了以后,判断两个串是否相等 */ public class lc131 { public static void main(String[] args) { List res = partition("aab"); System.out.println(); } public static List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<>(); helper(s, res, new ArrayList<>()); return res; } public static void helper(String s, List<List<String>> res, List<String> curr){ if(s.length()==0) { res.add(new ArrayList<>(curr)); //新new一个 } for (int i = 1; i <= s.length() ; i++) { String curr_str = s.substring(0,i); if(isPalindrome(curr_str)){ curr.add(curr_str); helper(s.substring(i), res, curr); curr.remove(curr.size()-1); //不要用curr.remove(curr_str); 会把集合内顺序打乱,AC不了 } } } public static boolean isPalindrome(String s){ //判断是否为回文 int b =0; int e = s.length()-1; while(b<e){ if(s.charAt(b)!=s.charAt(e)) return false; b++; e--; } return true; } } |
| You are visitor 01402775 — 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. |