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. nginx 安装 thinkphp5 配置

    nginx.conf server { listen ; server_name s.huailaixx.com; charset utf-; location ~ \.php { root /dat ...

  2. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  3. JavaScript设计模式 样例二 —— 策略模式

    策略模式(Strategy Pattern): 定义:定义了一族算法: 封装了每个算法: 这族的算法可互换代替. 目的:将算法的使用与算法的实现分离开来. 场景:可用来消除大量的条件分支语句. 例:J ...

  4. 常见BUG

    1.没有配置Tomcat服务,由于 <exclusions> <exclusion> <groupId>org.springframework.boot</g ...

  5. 如何提高SMTP邮件的安全性?从而不被黑客窃听

    简单邮件传输协议(SMTP)用于在邮件服务器之间进行邮件传输,并且传统上是不安全的,因此容易被黑客窃听.命名实体的基于DNS的认证(国家统计局)用于SMTP提供了邮件传输更安全的方法,并逐渐变得越来越 ...

  6. 29.密码学知识-消息认证码MAC-6——2019年12月19日

    1. 消息认证码 1.1 消息认证 消息认证码(message authentication code)是一种确认完整性并进行认证的技术,取三个单词的首字母,简称为MAC. 思考改进方案? 从哈希函数 ...

  7. 大数阶乘(N! Plus)问题

    解题思路 将正整数N从1到N逐位相乘,即1 * 2 * 3...... * (N-1) * N.每次相乘后的值会存储到array[]中,其中一个数组元素存储值中的一位数.当值小于10时直接存储,值大于 ...

  8. js用逗号分隔字符串,保留双引号中的字符串

    /** * 按逗号分解字符串, 引号中的逗号要保留, 并去除空值. * 方案: 1.将引号中都逗号替换为一个字符串中没有的符号; 2. 用split分解; 3. 去掉空值; 4. 还原引号中的逗号. ...

  9. SpringBoot的启动简述

    一.注解和启动类SpringBootApplication 它是一个复式注解. @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME ...

  10. linux运维、架构之路-KVM虚拟化技术

    一.云计算概述 云计算:是一种资源使用和交付模式 虚拟化:一种具体的技术,用来将物理机虚拟成为多个相互独立的虚拟机.云计算不等于虚拟化,云计算是使用了虚拟化的技术做支撑 二.KVM配置使用 1.系统环 ...