413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).
Given x = 123, return 321
Given x = -123, return -321
解法一:
class Solution {
public:
/*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
long ret = ;
while (n != ) {
ret = ret * + n % ;
n /= ;
}
if (ret > INT_MAX || ret < INT_MIN) {
return ;
}
return (int)ret;
}
};
先用long来计算防止越界,然后再把越界的处理掉。
解法二:
class Solution {
public:
int reverseInteger(int x) {
int rst = ;
while(x != ){
int next_rst = rst * + x % ;
x = x / ;
if(next_rst / != rst) {
rst = ;
break;
}
rst = next_rst;
}
return rst;
}
};
参考@NineChapter 的代码
413. Reverse Integer【easy】的更多相关文章
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- Express优化-合并路由
原app.js var express = require('express'); var app = express(); /*start*/ app.get('/',function(req,re ...
- Google Breakpad 完全解析(二) —— Windows前台实现篇
原创文章,转载请标明出处:Soul Apogee (http://bigasp.com),谢谢. 好,看完了如何使用breakpad,我们现在看看breakpad在Windows下到底是如何实现的呢? ...
- jquer回显选中select下拉框
公司使用的框架比较旧,没有使用el等表达式. <% String context = request.getContextPath(); String index = (String)reque ...
- Flask参数解析、请求钩子
转载请注明出处 https://www.cnblogs.com/chenxianpao/p/9949279.html 参数解析 Flask的参数解析主要用Request完成(from flask i ...
- ajax常用请求方式
1.JAVA @RequestMapping(value = "testAjax") @ResponseBody public Map<String, Object> ...
- Java程序猿笔试面试之String
1.怎样实现字符串的反转比如:"how are you"--->"you are how" 2.怎样推断2个字符串是否有同样的字符组成 比如"a ...
- iOS:触摸控件UITouch、事件类UIEvent
UITouch:触摸控件类 UIEvent:事件类 ❤️❤️❤️UITouch的介绍❤️❤️❤️ 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhas ...
- Google Chrome插件开发-Context Menus
本节主要介绍如何在Google Chrome浏览器web页面上点击右键弹出自定义菜单,即如何使用谷歌Context Menus API接口.上节已经把主要流程介绍了,这节就直接上代码,代码都是官方例子 ...
- MySql Replication基本原理
Replication的思想是将数据在集群的多个节点同步.备份,以提高集群数据的可用性(HA):Mysql使用Replication架构来实现上述目的,同时可以提升了集群整体的并发能力.5.6版本作为 ...
- MongoDB分片集群常用操作
下架主节点: db.adminCommand({replSetStepDown : 1, force : true}) 删除节点: rs.remove("IP:PORT") 新增节 ...