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

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
["aa","b"],
["a","a","b"]
]

题目大意:给一个字符串,输出这个字符串所有可能的回文子串。

解题思路:这道题我是用回溯来解,dfs之后再还原状态,首先确定解由两部分构成,每次只拆分后半部分,验证前半部分,如果前面是回文,则递归拆分并验证后半部分。

注意:DFS之后要还原状态,从上一个合法解之后继续遍历其他可能。

Talk is cheap>>

public class PalindromePartitioning {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
ArrayList<String> tmp = new ArrayList<>();
int length = s.length();
dfs(s, tmp, res, length);
return res;
} public void dfs(String src, ArrayList<String> tmp, List<List<String>> res, int length) {
if (length == 0) {
res.add((ArrayList<String>) tmp.clone());
return;
}
for (int i = 1; i <= src.length(); i++) {
if (isValid(src.substring(0, i))) {
tmp.add(src.substring(0, i));
dfs(src.substring(i, src.length()), tmp, res, length - i);
tmp.remove(tmp.size() - 1);
}
}
} public boolean isValid(String s) {
if (s == null || s.length() <= 1) {
return true;
}
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
 

Palindrome Partitioning——LeetCode的更多相关文章

  1. Palindrome Partitioning leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  2. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  3. [LeetCode] Palindrome Partitioning 拆分回文串

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

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  6. [LeetCode] Palindrome Partitioning II 解题笔记

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

  7. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  8. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  9. LeetCode: Palindrome Partitioning 解题报告

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

随机推荐

  1. Linux - 标准输入转换为标准输出 代码(C)

    标准输入转换为标准输出 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy Linux能够使用getc()和putc(),读取和写入每个输入字符. 代码: / ...

  2. 内存管理概述、内存分配与释放、地址映射机制(mm_struct, vm_area_struct)、malloc/free 的实现

    http://blog.csdn.net/pi9nc/article/details/23334659 注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料 ...

  3. std::bad_alloc

    一个服务器程序,长时间运行报错了. ----------- terminate called after throwing an instance of 'std::bad_alloc' what() ...

  4. Android Studio 快捷键(转)

    Android Studio 快捷键 操作 Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ctrl + ...

  5. Android开发--WIFI实现

    wifi的基本结构 在Android的官方文档中定义了如下五种状态: WIFI_STATE_DISABLING  WIFI网卡正在关闭  0 WIFI_STATE_DISABLED   WIFI网卡不 ...

  6. ASP.NET图片验证码学习!

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  7. try{...} catch {...} finally{...} 各种情况代码的执行情况

    try { int i = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("in the 'try'"); } ca ...

  8. JavaScript--时间显示小插件

    JS调用系统时间,显示在当前页面上,利用DOM操作节点来实现动态刷新 <!DOCTYPE html> <html> <head> <meta charset= ...

  9. 使用WMI来连接远端计算机

    1. wmi连接前提 利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码.如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以. 2. 相关类的用法- ...

  10. flash与php 交互(as传参给php)

    一种 不传参 直接读取PHP文件 btn.addEventListener(MouseEvent.CLICK,loadTxt);function loadTxt(evt:MouseEvent):voi ...