原文地址:http://www.niu12.com/article/48

git地址:git@github.com:ZQCard/leetcode.git

给定一个 32 位有符号整数,将整数中的数字进行反转。

示例 1:

输入: 123
输出: 321

示例 2:

输入: -123
输出: -321

示例 3:

输入: 120
输出: 21
package reverse_integer

import (
"math"
) func ReverseInteger(x int) int {
// 先确定正负数
var flag = true
if x < 0{
flag = false
x = -x
} // 将数字作为单个元素存储入组 倒序
var baseNum []int var modNum = 10 for{
mod := x % modNum
baseNum = append(baseNum, mod)
x = (x - mod) / modNum
if x < 1{
break
}
} var res = 0
// 将元素中的数一次抽取出来相加
length := len(baseNum)
for i := 0; i < length; i++ {
if baseNum[i] == 0 {
continue
}
res += baseNum[i] * int(math.Pow(float64(modNum), float64(length - i - 1)))
} if !flag {
res = 0 - res
if res < math.MinInt32 {
return 0
}
}else if res > math.MaxInt32{
return 0
}
return int(res)
} func ReverseIntegerTwo(x int) int {
sign := 1 // 处理负数
if x < 0 {
sign = -1
x = -1 * x
} res := 0
for x > 0 {
// 取出x的末尾
temp := x % 10
// 放入 res 的开头
res = res*10 + temp
// x 去除末尾
x = x / 10
} // 还原 x 的符号到 res
res = sign * res // 处理 res 的溢出问题
if res > math.MaxInt32 || res < math.MinInt32 {
res = 0
} return res
}
func TheBest(x int) int{
var digits []int8 for i := x; i != 0; i = i / 10{
digits = append(digits, int8(i % 10))
} var res int
for i := len(digits) - 1; i >= 0; i--{
res += int(digits[i]) * int(math.Pow10(len(digits) - i - 1))
}
if res > math.MaxInt32 || res < math.MinInt32{
return 0
}
return res
}
 

leetcode练习之No.7------ 翻转整数reverse_integer的更多相关文章

  1. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  2. [链表]LeetCode 25 K组一个翻转链表

    LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3, ...

  3. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  4. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  5. Java实现 LeetCode 25 K个一组翻转链表

    25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持 ...

  6. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  7. 翻转整数 Reverse digits of a number

    两种方法翻转一个整数.顺序翻转和递归翻转 这里没考虑overflow的情况 递归的作用是使得反向处理.即从递归栈的最低端開始处理.通过绘图可得. 假设是rec(num/10): 12345 1234 ...

  8. [LeetCode] 25. k个一组翻转链表

    题目链接: https://leetcode-cn.com/problems/reverse-nodes-in-k-group/ 题目描述: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链 ...

  9. [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

随机推荐

  1. [object-c 2.0 程序设计]object-c file handle (二)

    // // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...

  2. Backbone Model 源码简谈 (版本:1.1.0 基础部分完毕)

    Model工厂   作为model的主要函数,其实只有12行,特别的简练 var Model = Backbone.Model = function(attributes, options) { va ...

  3. 《Java编程思想》笔记 第二章 一切都是对象

    1.对象存储位置 对象的引用存在栈中,对象存在堆中.new 出来的对象都在堆中存储.栈的存取速度较快. 所有局部变量都放在栈内存里,不管是基本类型变量还是引用类型变量,都存储在各自的方法栈中: 但是引 ...

  4. Spring MVC基础篇4

    Spring MVC操作原生Servlet 对象 Spring MVC 可以操作原生的Servlet API,如下的这些原生API,可以各自 自由混合使用,也可以和其他非原生 参数组合使用 实例代码: ...

  5. hdu 1932(spfa)

    XYZZY Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3694   Accepted: 1059 Description ...

  6. 【转】docker之Dockerfile实践

    转自:https://www.cnblogs.com/jsonhc/p/7767669.html 上一篇介绍了Dockerfile中使用的指令,现在开始进行指令实践 先查看下本地的镜像,选一个作为ba ...

  7. [Jquery]斑马线表格

    <!doctype html> <html> <head> <script src='js/jquery-1.9.1.min.js'></scri ...

  8. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  9. C++—揭秘大牛博客一些不同凡人的写法

    天下之大,无奇不有,C++也是这样,今天小编来盘点几个有意思的代码,看看你认识几个?以后见到之后千万别装不认识. 一.基础篇——不一样的输出 1.cerr 输出 cout和cerr究竟有什么不同?这也 ...

  10. SHELL 在指定行的前/后插入指定内容

    #如果知道行号可以用下面的方法 sed -i '88 r b.file' a.file    #在a.txt的第88行插入文件b.txt awk '1;NR==88{system("cat ...