题解 | #数字翻转#
数字翻转
https://www.nowcoder.com/practice/bc62febdd1034a73a62224affe8bddf2
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()){ String s = sc.nextLine(); String[] s1 = s.split(" "); Integer result = 0 ; for (String string : s1) { result+= getString(string); } System.out.println(getString(String.valueOf(result))); } } private static Integer getString(String s) { char[] charArray = s.toCharArray(); //反转这个数组 栈,先进后出 Stack stack = new Stack(); for (char c : charArray) { stack.push(c); } //取出来,如果开头是以0开始的则不加入 StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) { Object pop = stack.pop(); if(i==0&&(char)pop=='0'){ continue; } sb.append((char) pop); } String string = sb.toString(); return Integer.valueOf(string); } }