题解 | #单词消消乐#

单词消消乐

http://www.nowcoder.com/practice/abb14fd6e1a34b0fb8016dfd7a99dfc5

1.这个 主要要注意字符串的比较最好用equals,且 注意substring(x,y)不包括y

2.主要是要注意不要只判断一次,要循环判断,直到数组中没有""出现

public String WordsMerge (String[] Words) {
        // write code here
        String[] ans = Delete(Words,Words.length);
        StringBuilder res = new StringBuilder();
        for (String o:ans){
            res.append(o);
        }
        return res.toString();
    }
    public String[] Delete(String[] Words,int n){
        for(int i = 0;i<= Words.length-2;i++){
            while (!Words[i].equals("")&&!Words[i+1].equals("")&&Words[i].charAt(Words[i].length()-1)==Words[i+1].charAt(0)){
                Words[i] =  Words[i].substring(0,Words[i].length()-1);
                Words[i+1] = Words[i+1].substring(1,Words[i+1].length());
            }
        }
        List<String> list = new ArrayList<>();
        for(String x:Words){
            if(!x.equals("")){
                list.add(x);
            }
        }
        if(list.size()==n){
            return list.toArray(new String[0]);
        }
        return Delete(list.toArray(new String[0]),list.size());
    }
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务