标签:try class style reverse value valueof str builder iterator
看到这个第一反应是当作字符去处理,于是就有了下面比较奇葩的代码哈哈哈,后面看到其他人一水的用运算解决,感觉时间和空间上会好很多,这里还是贴出来:
class Solution { public int reverse(int x) { char[] chars=String.valueOf(x).toCharArray(); Stack<Character> stack=new Stack(); //reverse for (char c:chars){ if (c>=48&&c<=57){ stack.push(c); } } StringBuilder sb = new StringBuilder(); Iterator it = stack.iterator(); for (int i = 0; i < chars.length; i++) { if (chars[i] >= 48 && chars[i] <= 57) { chars[i] = stack.pop(); } } try { return Integer.valueOf(new String(chars)); }catch (Exception e){ return 0; } } }
标签:try class style reverse value valueof str builder iterator
原文地址:https://www.cnblogs.com/lccsblog/p/12543596.html