zoukankan      html  css  js  c++  java
  • Palindrome Partitioning

    Palindrome Partitioning

     问题:

    Given a string s, partition s such that every substring of the partition is a palindrome.

    Return all possible palindrome partitioning of s.

    思路:

      典型dfs+回溯

    我的代码:

    public class Solution {
        public List<List<String>> partition(String s) {
            if(s == null || s.length() == 0)    return rst;
            List<String> list = new ArrayList<String>();
            helper(s, list);
            return rst;
        }
        public void helper(String s, List<String> list)
        {
            if(s == null || s.length() == 0)
            {
                rst.add(new ArrayList(list));
                return;
            }
            for(int i = 1; i <= s.length(); i++)
            {
                String part = s.substring(0,i);
                if(isPalindrome(part))
                {
                    list.add(part);
                    helper(s.substring(i),list);
                    list.remove(list.size() - 1);
                }
            }
        }
        private List<List<String>> rst = new ArrayList<List<String>>();
        public boolean isPalindrome(String part)
        {
            int len = part.length();
            for(int i = 0; i < len/2; i++)
            {
                char first = part.charAt(i);
                char last = part.charAt(len - 1 - i);
                if(first != last)   return false;
            }
            return true;
        }
    }
    View Code
  • 相关阅读:
    Android开发--Layout元素
    Android开发--RelativeLayout的应用
    group by调优的一些测试
    mysql explain中key_len的计算
    mysql索引长度的一些限制
    order by调优的一些测试
    metadata lock
    JDBC连接数据库
    Java—Map.Entry
    innodb buffer pool
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4337375.html
Copyright © 2011-2022 走看看