Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
public class Solution {
public int reverse(int x) {
StringBuffer s=new StringBuffer(Integer.toString(x));
if( s.charAt(0)=='-'){
s.deleteCharAt(0);
String t="-"+s.reverse().toString();
return Integer.parseInt(t);
}
else{
s=s.reverse();
return Integer.parseInt(s.toString());
}
}
}
本文介绍了一种用于翻转整数的算法实现,并通过Java代码示例详细展示了如何处理正负整数的翻转过程。该算法适用于面试准备和技术实践。
364

被折叠的 条评论
为什么被折叠?



