LeetCode刷题笔录Palindrome Partitioning
来源:互联网 发布:淘宝客如何用网站推广 编辑:程序博客网 时间:2024/04/30 03:27
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暴力搜索就好了。
public class Solution { public List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<List<String>>(); if(s == null || s.length() == 0) return res; dfs(s, 0, new ArrayList<String>(), res); return res; } public void dfs(String s, int start, List<String> strs, List<List<String>> res){ if(start >= s.length()){ res.add(new ArrayList<String>(strs)); return; } for(int i = start; i < s.length(); i++){ if(isPalindrome(s, start, i)){ strs.add(s.substring(start, i + 1)); dfs(s, i + 1, strs, res); strs.remove(strs.size() - 1); } } } public boolean isPalindrome(String s, int start, int end){ int i = start, j = end; while(i < j){ if(s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; }}
0 0
- LeetCode刷题笔录Palindrome Partitioning
- [leetcode刷题系列]Palindrome Partitioning
- [leetcode刷题系列]Palindrome Partitioning II
- leetcode 刷题之路 41 Palindrome Partitioning
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- leetcode第一刷_ Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 最简单的基于FFmpeg的封装格式处理:视音频分离器简化版(demuxer-simple)
- Hashing Table(笔记)
- css 清除浮动
- ASC(2)A(大数+图论DP)
- 我的程序员之路2
- LeetCode刷题笔录Palindrome Partitioning
- exception类的实现
- ASCII Table and Description
- STM32驱动W25X64存储器
- isdigit()在<ctype.h>中
- mybatis代码生成的几种方式
- Country Code
- Android 获取assets的绝对路径
- 辣辗坡媳饶焦饶泳磷刑辗遣谂鼻当