题解 | #数字颠倒#
数字颠倒
https://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String num = scanner.nextLine(); System.out.println(solution1(num)); System.out.println(solution2(num)); System.out.println(solution3(num)); } public static String solution1(String str) { StringBuilder sb = new StringBuilder(); for (int i = str.toCharArray().length - 1; i >= 0; i--) { sb.append(str.charAt(i)); } return sb.toString(); } public static String solution2(String str) { int num = Integer.parseInt(str); StringBuilder sb = new StringBuilder(); while (num / 10 > 0) { sb.append(num % 10); num /= 10; } return sb.append(num).toString(); } public static String solution3(String str) { return new StringBuilder(str).reverse().toString(); } }#华为笔试#