1. 去掉首位空格
2. 判断首位是否有正负号
3. 判断各位是否是0~9,有其他字符直接返回当前结果
 
 public class Solution {
public int atoi(String str) {
str = str.trim();
int result = 0;
boolean isPos = true; for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(i==0 && (c == '+' || c == '-')) {
isPos = c == '+' ? true : false;
} else if(c >= '0' && c <= '9') {
if(result > (Integer.MAX_VALUE- (c-'0'))/10) {
return isPos ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + c - '0';
} else {
return isPos ? result : -result;
}
} return isPos ? result : -result;
}
}
 
 
 
 

实现atoi的更多相关文章

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

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

  2. 编写atoi库函数

    看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...

  3. 行程编码(atoi函数)

    #include<iostream> #include<string> #include<vector> using namespace std; void jie ...

  4. No.008:String to Integer (atoi)

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

  5. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  6. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  7. [LeetCode] 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...

  8. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  9. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...

随机推荐

  1. 010-流程控制 while 与 until 语句

    流程控制 while 与 until 语句 while循环是不定循环,也称作条件循环,只要条件成立,循环就一直继续.与for的固定循环不同 until只要条件不成立,循环就一直继续 #!/bin/ba ...

  2. 01JAVA入门

    1 Welcome to java public class ch01Welcome { public static void main(String[] args) { System.out.pri ...

  3. MixConv

    深度分离卷积一般使用的是3*3的卷积核,这篇论文在深度分离卷积时使用了多种卷积核,并验证了其有效性 1.大的卷积核能提高模型的准确性,但也不是越大越好.如下,k=9时,精度逐渐降低 2. mixCon ...

  4. 树莓派上固定ip

    sudo nano /etc/dhcpcd.conf interface eth0 static ip_address=192.168.123.99/24 static routers=192.168 ...

  5. python 使用wxpy实现获取微信好友列表 头像 群成员

    最近在学习 python 突然想要试试能不能把微信里面的微信群和好友取出来 结果百度了一下 找到了 wxpy 这怎么能不试一下呢 用到 wxpy.threading.os.time 四个库 第一步 判 ...

  6. vue组件结构

    1.组件结构 2.项目结构

  7. linux运维、架构之路-nfs网络文件系统

    一.nfs介绍  NFS是Network File System的缩写,是网络文件系统,它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录,主要存储用户上传的图片附件等信息. ...

  8. idea 打包model 为jar包

    1,在项目上鼠标右键 --> Open Module Settings 2, Artifacts --> + --> JAR --> From modules with dep ...

  9. 批量下载文件JSP

    最近项目有个需求,用户想对挂有附件的数据记录 实现一键下载全部附件(目前项目仅支持每次点击单条记录进行附件下载),下面记录我实现的解决方案.项目框架基于SSMservice业务实现层(impl):// ...

  10. Java——static

    [static] <1>static成员变量存储在内存data segment区域,不是存放在堆中. <2>静态成员变量属于整个类,任何一个对象都可以访问这个值:如果没有对象, ...