题解 | #句子逆序#

句子逆序

http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        String str = new String(in.nextLine());
        if(1 <= str.length() && str.length() <= 1000){
            int end = str.length() - 1;
            int start = 0;
            for(int i = str.length() - 1; i >= 0; i--){
                if(str.charAt(i) == " ".charAt(0) || i == 0){
                    if(i == 0){
                        /*for(int j = i; j <= end; j++){
                        System.out.print(str.charAt(j));
                        }*/
                        System.out.print(str.substring(i, end + 1));
                        break;
                    }
                    else{
                        //旧方法,效率较低,其实不用循坏,直接调用substring()即可
                        /*for(int j = i + 1; j <= end; j++){
                        System.out.print(str.charAt(j));
                        }*/
                        System.out.print(str.substring(i + 1, end + 1));
                        end = i - 1;
                        System.out.print(" ");
                    }
                }
            }
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-23 10:26
佳木斯大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-15 17:17
听说过付费实习,没想到这么贵啊我去,要不我给你个腰子吧
哈哈哈,你是老六:这种公司一定要注意啊,不要随便签合同,只要签了后面钱可能回不来,而且你通过法律途径也弄不回
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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