leetcode 字符串分割对称

 1 public class Solution {
 2     public List<List<String>> partition(String s) {
 3         int len=s.length();
 4            boolean dp[][]=new boolean[len][len];
 5            get(dp,s);
 6            ArrayList<ArrayList<String>> res=new ArrayList<ArrayList<String>>();
 7            ArrayList<String> temp=new ArrayList<String>();
 8            dfs(res,temp,0,dp,s);
 9            return (List)res;
10
11
12     }
13     public void get(boolean dp[][],String s)
14     {
15         char c[]=s.toCharArray();
16         int len=s.length();
17       //single character is duichen
18       for(int i=0;i<len-1;i++)
19       {
20           dp[i][i]=true;
21           if(c[i]==c[i+1]) dp[i][i+1]=true;
22
23       }
24       dp[len-1][len-1]=true;
25
26         for(int l=2;l<len;l++)
27         {
28             for(int k=0;k<len-l;k++)
29             {
30                 dp[k][k+l]=dp[k+1][k+l-1]&&(c[k]==c[k+l]);
31             }
32
33         }
34
35
36
37     }
38 public void dfs(ArrayList<ArrayList<String>> res,ArrayList<String> temp,int l,boolean dp[][],String s)
39     {
40         if(l==dp.length)
41         {
42            res.add(new ArrayList(temp));
43         }
44         else
45         {
46             for(int j=0;j<dp.length;j++)
47             {
48                 if(dp[l][j])
49                 {
50                     ArrayList<String> t=new ArrayList<String>(temp);
51                     t.add(s.substring(l,j+1));
52                     dfs(res,t,j+1,dp,s);
53
54                 }
55
56
57             }
58
59              }
60     }
61
62
63
64
65 }

leetcode 字符串分割对称,布布扣,bubuko.com

时间: 2024-08-08 00:26:30

leetcode 字符串分割对称的相关文章

LeetCode Isomorphic Strings 对称字符串

题意:如果两个字符串是对称的,就返回true.对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的. 思路:ASCII码表哈希就行了.需要扫3次字符串,共3*n的计算量.复杂度O(n).从串左开始扫,若字符没有出现过,则赋予其一个特定编号,在哈希表中记录,并将该字符改成编号.对串2同样处理.其实扫2次就行了,但是为了短码. 1 class Solution { 2 public: 3 void cal(string &s) 4 { 5 int has[129]={0}, cnt=0; 6

动态规划的思想来求解字符串分割问题

LeetCode WordBreak原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]

OC3_字符串分割

// // main.m // OC3_字符串分割 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> /*2.对称字符串 传入一个字符串,将这个字符串组合成一个新的对称字符串返回 例如: 传入:@"I love you Mery" 返回:@"I

C++ 字符串分割

2017-06-26 21:47:06 这几天在做大数据的处理,需要处理几百万的数据.由于一直用的是c++,所以就直接用c++进行了编码. 不过后来经过学长的提醒发现其实字符串处理,尤其是处理csv文件(由逗号作为分割符)时,需要将各个部分分割开来,如果用Java或者Python进行处理的话会非常方便,比如Java就有split函数进行字符串的分割,不过我在查询资料之后发现c++中并没有现成的针对string的字符串分割函数.于是我自己写了一个split函数. 这个函数的声明是 void spl

C/C++ 字符串分割: strtok 与 strsep 函数说明

函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok和strsep两个函数的功能都是用来分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串. 返回值:从s开头开始的一个个子串,当没有分割的子串时返回NULL. 相同点:两者都会改变源字符串,想要避免,可以使用strdupa(由allocate函数实现)或strdup(由malloc函

Matlab实现字符串分割

Matlab实现字符串分割(split) Matlab的字符串处理没有C#强大,本身又没有提供OO特性,需要依赖别的手段完成这项任务. 我们在这里借助正则表达式函数regexp的split模式.一般语法: S = regexp(str, char, 'split') 其中str是待分割的字符串,char是作为分隔符的字符(可以使用正则表达式).分割出的结果存在S中. 以下面这样一串字符为例 Hello Nocturne Studio 首先去除首尾的多余空格: str = deblank(str)

strtok():“字符串分割”实用函数

最近一直纠结于一个十分简单的问题:如何将一个字符串按标志分割开来?提出这个问题的初衷是自己在处理一个将命令行字符串转换为argc,argv格式的问题. 尝试了很多种方法,最后觉得利用strtok()函数来实现是一个比较好的方法.首先进行strtok()函数的介绍. char *strtok(string, control);  --- Tokenize string with delimiter in control. --- 通过分割符控制将字符串切片化. Purpose: strtok co

php 字符串分割函数split

说明 array split    ( string $pattern   , string $string   [, int $limit  ] ) 本函数返回一个字符串数组,每个单元为   string 经区分大小写的正则表达式   pattern 作为边界分割出的子串.如果设定了   limit,则返回的数组最多包含   limit 个单元,而其中最后一个单元包含了   string 中剩余的所有部分.如果出错,则   split() 返回 FALSE. Example #1 split(

JavaScript基础 split(&quot;&quot;) 将字符串分割成字符数组

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut