翻转整数 Reverse digits of a number
两种方法翻转一个整数。顺序翻转和递归翻转
这里没考虑overflow的情况
递归的作用是使得反向处理。即从递归栈的最低端開始处理。通过绘图可得。
假设是rec(num/10):
12345
1234
123
12
1 <-- 递归使得这个最先处理
package recursion;
public class Reverse_digits_of_a_number {
public static void main(String[] args) {
int num = 123;
System.out.println(reverse(num));
System.out.println(rec(num));
}
public static int reverse(int num) {
int rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num /= 10; // /10相当于10进制的右移一位
}
return rev;
}
static int revNum = 0;
static int base = 1;
public static int rec(int num) {
if(num == 0) {
return num;
}
rec(num/10); // 相当于block在这里。直到递归究竟,eg,num=123 -> 12 -> 1 -> 0
revNum = revNum + base*(num%10);
base *= 10;
return revNum;
}
}
http://www.geeksforgeeks.org/write-a-c-program-to-reverse-digits-of-a-number/
翻转整数 Reverse digits of a number的更多相关文章
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 7. Reverse Integer(翻转整数)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- [Swift]LeetCode7. 反转整数 | Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 【LeetCode】Reverse digits of an integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- [Swift]LeetCode25. k个一组翻转链表 | Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
随机推荐
- php 学习笔记 数组2
10.切割数组 array_slice(array, offset, length);返回一个由原始数组中的连续元素组成的新数组,参数1为原始数组,参数2为要复制的起始位置, 参数3要复制的个数:新数 ...
- 如何用vs查看结构体布局
今天遇到一个问题: 假设在每个系统的structA 结构不同,我们在windbg看了以后直接拿来用,自己定义成结构体,如何来验证这个结构体内存布局是否和windbg一致. 当然笨办法是自己一个个成员数 ...
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
安装 1准备编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 2 下载源码包(由于goog ...
- Mockito--完整功能介绍(转)
public interface DBAccess { int delete(String moi,String moc) throws Exception; void create(String m ...
- grep egrep fgrep命令
一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...
- win32多线程程序设计笔记(第五章)
前面章节介绍了线程创建等过程,现在的问题是:如何在某个线程内终止另外一个正在运行的线程? windows核心编程中提到终止运行线程的方法: 1)线程函数自己返回: 2)线程通过调用ExitThread ...
- libgdx, mouse 关节
鼠标与body的交互就靠这个mouse 关节了. 在使用中:主要分成3步: 步1:mouseDown : 这个时期,调用world->QueryAABB.它有一个回调接口,并依据鼠标指针指定一个 ...
- HDU 2040:亲和数
亲和数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- Eclipse生成Jar包方法
Eclipse生成jar包 第一:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包. 1.在eclipse中选择你要导出的类或者package,右击,选择Exp ...
- TCP三四次握手
通俗点说: 三次握手: A:发送连接请求 B:收到请求后,B知道自己的接收端是好的,返回给A请求的应答,并询问A是否收到自己的本次应答. A:收到B的应答.A知道自己的发送端和接收端都是好的.然后发送 ...