资讯专栏INFORMATION COLUMN

[LintCode] Split String

Riddler / 1307人阅读

Problem

Give a string, you can choose to split the string after one character or two adjacent characters, and make the string to be composed of only one character or two characters. Output all possible results.

Example

Given the string "123"
return [["1","2","3"],["12","3"],["1","23"]]

Solution
public class Solution {
    /*
     * @param : a string to be split
     * @return: all possible split string array
     */
    public List> splitString(String s) {
        // write your code here
        List> res = new ArrayList<>();
        if (s == null) return res;
        if (s.length() == 0) {
            res.add(new ArrayList());
            return res;
        }
        dfs(s, 0, new ArrayList(), res);
        return res;
    }
    public void dfs(String s, int index, List cur, List> res) {
        if (index >= s.length()) {
            res.add(new ArrayList(cur));
            return;
        }
        int i = index;
        while (i <= index+1 && i < s.length()) {
            cur.add(s.substring(index, i+1));
            dfs(s, i+1, cur, res);
            cur.remove(cur.size()-1);
            i++;
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68473.html

相关文章

  • [LintCode/LeetCode] Binary Tree Serialization

    摘要:这里要注意的是的用法。所以记住,用可以从自动分离出数组。跳过第一个元素并放入数组最快捷语句建立的用意记录处理过的结点并按处理所有结点和自己的连接下面先通过判断,再修改的符号的顺序,十分巧妙更轻便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...

    keithyau 评论0 收藏0
  • [LintCode] Missing String

    Problem Given two strings, you have to find the missing string. Example Given a string str1 = This is an exampleGiven another string str2 = is example Return [This, an] Solution public class Solution ...

    IamDLY 评论0 收藏0
  • [LintCode] Valid Number

    摘要:两种情况有无,如上所示。如何判断是否只有一个之后的长度小于等于。注意,为空只有在字符串最末位或者只有都是错误的。规则若字符串首位为,取其,然后将应用转换为数组,逐位判断字符,出现将置如已经置,再次出现则直接返回。 Problem Validate if a given string is numeric. Example 0 => true 0.1 => true abc =>...

    since1986 评论0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 评论0 收藏0
  • [LintCode] Simplify Path [字符串操作]

    摘要:,可以用函数去掉所有,然后多考虑一个中间为空的。关于语句的一个特点我们对和其实都是不做操作,然而,两个可以都,但是不能都不做操作。像这样这样这两个就都会等价于下一个就会出错。 Problem Given an absolute path for a file (Unix-style), simplify it. Example /home/, => /home //去掉末尾的slash...

    leanxi 评论0 收藏0

发表评论

0条评论

Riddler

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<