public class Solution {
public int myAtoi(String str) {
int index = 0, sign = 1, total = 0;
//1. 边界条件判断
if(str.length() == 0) return 0; //2. 移除空格
while(str.charAt(index) == ' ' && index < str.length())
index ++; //3. 处理符号位
if(str.charAt(index) == '+' || str.charAt(index) == '-'){
sign = str.charAt(index) == '+' ? 1 : -1;
index ++;
} //4. 转变为int,并且避免溢出
while(index < str.length()){
int digit = str.charAt(index) - '0';
if(digit < 0 || digit > 9) break; if(Integer.MAX_VALUE/10 < total || Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; total = 10 * total + digit;
index ++;
}
return total * sign;
}
}

[Leetcode]008.String to Integer (atoi)的更多相关文章

  1. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

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

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  4. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  6. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  7. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  8. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  9. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

随机推荐

  1. POJ2891Strange Way to Express Integers (线性同余方程组)

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...

  2. 系列文章--打造自己的MVC框架

    MVC源码学习系列文章目录: MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理) MVC系列——MVC源码学习:打造自己的MVC框架(二:附源码) MVC系列——MVC源码学习:打造自 ...

  3. Azure上部署FTP服务

    FTP是个比较复杂的协议,其协议分为控制层和数据层,工作模式分为主动和被动两种模式. 在默认的Active模式下其工作原理如下: 可以看到,客户端发起FTP的请求道服务器端,FTP的端口是21.用户在 ...

  4. Python:.join()函数

    转于:https://blog.csdn.net/chixujohnny/article/details/53301995 博主:chixujohnny 介绍:.join是一个字符串操作函数,将元素相 ...

  5. RS485总线防雷保护方案

    RS485作为最为最常用的电表通讯方式之一.日常生活中雷电和静电干扰已经成为485通信总线在实际工程经常遇到的问题.故如何对芯片以及总线进行有效的保护,是摆在每一个使用者面前的一个问题.在这里,我们主 ...

  6. ES6学习之let和const

    1.let 基本用法:let声明的变量,只在let命令所在的代码块内有效 { let a = 1; var b = 2; } console.log(a) //a is not defined con ...

  7. mouseenter与mouseover的区别

    mouseover 事件:只有在鼠标指针穿过被选元素时,才会触发. mouseover 事件:鼠标指针穿过任何子元素,都会触发. 请看例子的演示.

  8. [51nod1181]质数中的质数(素数筛法)

    解题关键: 注意下标 #include<bits/stdc++.h> #define maxn 10000002 using namespace std; typedef long lon ...

  9. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  10. 使用showMessageDialog显示消息框

    -----------------siwuxie095                         工程名:TestJOptionPane 包名:com.siwuxie095.showdialog ...