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

解题思路:

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

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. Date 对象 时间格式注意事项

    Date 对象,是操作日期和时间的对象. Date 为内置的构造函数, 通过 new Date () 来获取当前本地日期与时间 const time = new Date console.log(ti ...

  2. js读取excel数据后的时间格式转换

    使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ...

  3. LibreOJ #107. 维护全序集

    内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名   splay模板题 屠龙宝刀点击就送 #include <cstdio&g ...

  4. 会写网页 就会写手机APP #2-- 范例修正 , Hybrid Mobile Apps for ASP.NET Developers (Apache Cordova)

    原文出处:会写网页 就会写手机APP #2-- 范例修正 , Hybrid Mobile Apps for ASP.NET Developers (Apache Cordova) 这是我的文章备份  ...

  5. python基础一 day14 生成器函数进阶(1)

  6. python_82_标准库_random模块

    import random print(help(random.random)) #随机整数 print(random.randint(1,7))#生成一个[1, 7]的随机整数 print(rand ...

  7. $|^|\z|\Z|/a|/l

    #!/usr/bin/perl use strict; use warnings; foreach(<>) { if (/(\w*)/a){print "$1\n";} ...

  8. 接口的多态使用; 接口应用实例:U盘、打印机可以使用共同的USB接口,插入到电脑上实现各自的功能。

    接口的多态使用 接口应用实例:U盘.打印机可以使用共同的USB接口,插入到电脑上实现各自的功能.

  9. c++ 创建路径方法

    linux.unix平台 #include "stdio.h" #include "stdlib.h" #include <sys/types.h> ...

  10. C# 获取Google Chrome的书签

    其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了. 先建立几个实体类 public class GoogleChrome_bookMark_meta_info { publ ...