题目: 实现字符串到整数的转换

解题思路:

下面给出这道题应该注意的一些细节:

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(实现字符串到整数的转换)的更多相关文章

  1. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  2. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  4. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  5. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  6. 8. String to Integer[M]字符串转整数

    题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...

  7. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

  8. 编程练习------C/C++分别实现字符串与整数的转换

    C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...

  9. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

随机推荐

  1. Linux下环境搭建(四)——jenkins+gitlab+jmeter实践

    经过前三篇博文的介绍,jenkins+gitlab+jmeter接口自动化的框架就搭建成功了,详细可见 Linux下环境搭建(一)——java.tomcat配置 Linux下环境搭建(二)——jenk ...

  2. C语言中的static和extern

    c语言中,全局变量是一个非常重要的概念.全局变量定义在函数外,可以被所有的函数共同使用. #include <iostream> ; void display() { printf(&qu ...

  3. AES加密示例

    最近用到对文本内容进行加密,于是查了一下常用的加密算法: DES(Data Encryption Standard):对称算法,数据加密标准,速度较快,适用于加密大量数据的场合:3DES(Triple ...

  4. SQL Server扩展事件system_health会话总结

    system_health会话概念 我们知道扩展事件(Extended Events)是从SQL Server 2008开始引入的.system_health会话是SQL Server默认包含的扩展事 ...

  5. 用Python完成根据日期计算是星期几

    import datetime def week(year,month,day): someday=dayetime.date(year,month,day) result={ "0&quo ...

  6. sudo的用法

    为了系统安全我们一般不直接使用root用户进行日常维护,sudo是临时提升root权限,有时执行一些命令或者更新没权限的文件时需要使用root,这个时候就需要sudo上场了 普通用户是没有sudo使用 ...

  7. jsp另外五大内置对象之response-操作cookie

    responseo3.jsp <%@ page language="java" contentType="text/html; charset=utf-8" ...

  8. netbackup如何手动获取主机ID证书。

    如何手动获取主机ID证书.   文章:100039650 最后发布:2017-09-21 评分:  20 11 产品:NetBackup 问题 从NetBackup V8.1开始,管理员需要在证书颁发 ...

  9. cv2.solvepnp 相机的位姿估计

    预备知识   图像坐标系:   理想的图像坐标系原点O1和真实的O0有一定的偏差,由此我们建立了等式(1)和(2),可以用矩阵形式(3)表示. 相机坐标系(C)和世界坐标系(W): 通过相机与图像的投 ...

  10. Dungeon Master的两种方法

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...