其他分享
首页 > 其他分享> > 牛客华为机试HJ13

牛客华为机试HJ13

作者:互联网

原题传送门

1. 问题描述

2. Solution

1、思路
切割,逆序,输出
2、实现
Java

package huawei.HJ013;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner in;
        if (!"Linux".equals(System.getProperty("os.name"))) {
            in = new Scanner(Paths.get("/Users/jun/Documents/Learn/JavaLearning/NowCoder/src/huawei/HJ013/input.txt"));
        } else {
            in = new Scanner(System.in);
        }
        while (in.hasNext()) {
            String s = in.nextLine();
            solve(s);
        }
    }

    private static void solve(String s) {
        String[] parts = s.split("\\s+");
        for (int i = parts.length - 1; i >= 0; i--)
            System.out.print(parts[i] + " ");
    }
}

Python

import sys

for line in sys.stdin:
    parts = line.strip().split()
    parts.reverse()
    [print(x, end=" ") for x in parts]

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)

标签:HJ13,java,String,System,牛客,parts,机试,import,Scanner
来源: https://www.cnblogs.com/junstat/p/16070061.html