题解 | #单词倒排#

单词倒排

http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

循环 + char的判断
容易坑的地方在判断最后字符串以后,忘记把temp加入到list

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        List<String> list = new ArrayList<String>();
        char[] chars = line.toCharArray();
        String temp = "";
        for(int i = 0;i<chars.length;i++){
            char c = chars[i];
            if ( (c >= 'A' && c <= 'Z') || (c>='a' && c<= 'z') ){
                temp += String.valueOf(c);
            }
            else{
                list.add(temp);
                temp = "";
            }
            if (i == chars.length - 1){
                list.add(temp);
            }
        }
        for(int i = list.size() - 1 ; i >= 0 ;i--){
            System.out.print(list.get(i) +  " ");
        }
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务