翻转整数 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 ...
随机推荐
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- 我的Python成长之路---第四天---Python基础(16)---2016年1月23日(寒风刺骨)
四.正则表达式 字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和 ...
- DDL\DML\DCL\DQL
[DML] DML = Data Manipulation Language,数据操纵语言,命令使用户能够查询数据库以及操作已有数据库中的数据的计算机语言.具体是指是UPDATE更新.INSERT插入 ...
- 关于QuartusII对ram块的综合
之前在看Altera的官方教程上就有说明,如果我们定义一个reg [`word_w]user_ram[`word_d] ; QuartusII会自动综合成为一个ram—— 当然有一些前提:(后续补充 ...
- AddSelf
今天看 C语言深度深度解剖 第58页 看到了这么一段代码,就敲进了dev 谁知居然出现了个死循环.但是我不知道为什么. 贴出来,等有空了再请教别人好好分析,或者是网络上的高人指点一下 //d ...
- docker学习笔记15:Dockerfile 指令 USER介绍
USER指令用于指定容器执行程序的用户身份,默认是 root用户. 在docker run 中可以通过 -u 选项来覆盖USER指令的设置. 举例:docker run -i -t -u mysql ...
- 还是回文(dp)
还是回文 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描述 判断回文串很简单,把字符串变成回文串也不难.现在我们增加点难度,给出一串字符(全部是小写字母),添加或删除一个字 ...
- IT该忍者神龟Instant client required
pply OS : Windows, Mac, Linux Apply Navicat Product : Navicat for Oracle, Navicat Premium Apply Navi ...
- 【BZOJ1132】【POI2008】Tro 计算几何 叉积求面积
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- 隐式意图-activity
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW);//设置动作 intent.setData(Uri.parse(& ...