Leetcode8--->String to Integer(实现字符串到整数的转换)
题目: 实现字符串到整数的转换
解题思路:
下面给出这道题应该注意的一些细节:
1. 字符串“ 123 ” = 123;
2. 字符串“+123” = 123;
3. 字符串“-123” = -123;
4. 字符串“-” = 0;“+” = 0;
5. 字符串“123-” = 123;
6. 字符串“21474836478” = 2147483647(Integer.MAX_VALUE)
7. 其余情况都是非法输入,一律返回0;
代码如下:
public class Solution {
public int myAtoi(String str) {
if(str == null || str.length() < 1)
return 0;
boolean negative = false;
int i = 0;
double res = 0.0;
String s = str.trim();
int length = s.length();
if(s.charAt(i) < '0'){
if(s.charAt(i) == '-'){
negative = true;
}
else if(s.charAt(i) != '+'){
return 0;
}
if(length == 1)
return 0;
i++;
}
while(i < length){
int n = (s.charAt(i) - '0') ;
if(n >= 0 && n <= 9){
res *= 10;
res += n;
i ++;
}
else
break;
}
if(negative){
res *= -1;
}
res = res >= Integer.MAX_VALUE ? Integer.MAX_VALUE : res;
res = res <= Integer.MIN_VALUE ? Integer.MIN_VALUE : res;
return (int)res;
}
}
Leetcode8--->String to Integer(实现字符串到整数的转换)的更多相关文章
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer[M]字符串转整数
题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
- 编程练习------C/C++分别实现字符串与整数的转换
C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
随机推荐
- 获取url的参数值
var url=location.search; //获取url中从?开始的所有字符 var theRequest=new Object();//定义一个对象来存放url中的参数 if( url.i ...
- C#小记
1.背景:用fileinput 上传文件 直接上传文件,但有时会发现,这个不上传文件也是可以携带其他参数的, 如果直接用: uploadFile = context.Request.Files[]; ...
- ABAP事件分类
1.报表事件 INITIALIZATION. START-OF-SELECTION. END-OF-SELECTION. 2.选择屏幕事件 在INITIALIZATION和START-OF-SELEC ...
- SQL数据库基础三
- linux机器上部署多台Tomcat
在Linux机器上部署多台Tomcat, 我部署的是Tomcat8,只需要一步,即避免端口号冲突. 在解压后的tomcat目录下,修改conf下server.xml. 修改shutdown端口: &l ...
- Python+selenium之带unittest的脚本分析
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- MySQL数据库详解(三)MySQL的事务隔离剖析
提到事务,你肯定不陌生,和数据库打交道的时候,我们总是会用到事务.最经典的例子就是转账,你要给朋友小王转 100 块钱,而此时你的银行卡只有 100 块钱. 转账过程具体到程序里会有一系列的操作,比如 ...
- windows10下安装TensorFlow Object Detection API
由于官方的文件都是在乌班图和mac系统下的,就上网搜了以下. 按照下边这个博客安装成功了 https://blog.csdn.net/qq_28019591/article/details/82023 ...
- win7下一直试用Beyond Compare 4
找到目录C:\Users\用户名\AppData\Roaming\BeyondCompare,将这个目录删除,重启compare即可.
- HTML之基本语法(表单)
一.表单的基本介绍 表单:就是互联网上用于收集用户信息的一种结构,在HTML当中事先定义好了一种标签来完成此事,标签名称为form,它是一个双标签<form action="" ...