翻转整数 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

时间: 2024-08-25 11:24:03

翻转整数 Reverse digits of a number的相关文章

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

[LeetCode] Reverse Integer 翻转整数

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu

[LintCode] Reverse Integer 翻转整数

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Have you met this question in a real interview? Example Given x = 123, return 321 Given x = -123, return -321 LeetCode上的原题,请参见我之前的博客Reverse Integer.

【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】

[007-Reverse Integer(翻转整数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目大意 输入一个整数对其进行翻转 解题思路 通过求余数求商法进行操作. 代码实现 public class Solution { public int reverse(int x)

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

[Swift]LeetCode493. 翻转对 | Reverse Pairs

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2  Example2: Input: [2,4,3,5,1] Output:

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

翻转整数

int ReverseInterage(int Num) { long long tmp = 0; for (; Num; Num /= 10) tmp = tmp * 10 + Num % 10; return (tmp <= INT_MAX && tmp >= INT_MIN) ? tmp : 0; } 整数的翻转

K组翻转链表 &#183; Reverse Nodes in

[抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1->..->nk->next.. // to head->nk->..->n1->next.. // return n1 每k个转一次,再递归 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [