Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Analysis

The following cases should be considered for this problem:

1. null or empty string
2. white spaces
3. +/- sign
4. calculate real value
5. handle min & max

Java Solution

public int atoi(String str) {
	if (str == null || str.length() < 1)
		return 0;
	// trim white spaces
	str = str.trim();
	char flag = '+';
	// check negative or positive
	int i = 0;
	if (str.charAt(0) == '-') {
		flag = '-';
		i++;
	} else if (str.charAt(0) == '+') {
		i++;
	}
	// use double to store result
	double result = 0;
	// calculate value
	while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
		result = result * 10 + (str.charAt(i) - '0');
		i++;
	}
	if (flag == '-')
		result = -result;
	// handle max and min
	if (result > Integer.MAX_VALUE)
		return Integer.MAX_VALUE;
	if (result < Integer.MIN_VALUE)
		return Integer.MIN_VALUE;
	return (int) result;
}

[算法]String to Integer(atoi)的更多相关文章

  1. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

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

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

  3. 【leetcode】String to Integer (atoi)

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

  4. No.008 String to Integer (atoi)

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

  5. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  6. 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 ...

  7. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

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

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

  9. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  10. LeetCode: String to Integer (atoi) 解题报告

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

随机推荐

  1. sql CHARINDEX() 与 PATINDEX() LEN() substring() COLLATE RAISERROR

    CHARINDEX()  在一个表达式中搜索另一个表达式并返回其起始位置(如果找到). CHARINDEX ( expressionToFind , expressionToSearch [ , st ...

  2. orcad 里误给元件添加了属性,如何删除

    1.先尝试点击delete property 2.第一步不行就右键选择filters----->remove current filter进行删除

  3. Godaddy域名 绑定ip 服务器

    比如我的域名是wmxl.info 第一个红框代表wmxl.info 绑定的 211.83.110.216 第一个代表www.wmxl.info 绑定的 211.83.110.216, 你也可以换一个服 ...

  4. ubuntu环境 rake aborted!

    错误: rake aborted! Gem::LoadError: You have already activated rake 10.3.2, but your Gemfile requires ...

  5. 千万级的大表!MySQL这样优化更好

    对于一个千万级的大表,现在可能更多的是亿级数据量,很多人第一反应是各种切分,可结果总是事半功倍,或许正是我们优化顺序的不正确.下面我们来谈谈怎样的优化顺序可以让效果更好. MySQL数据库一般都是按照 ...

  6. iOS ZipArchive文件解压缩

    ZipArchive可以用于iOS中文件的解压缩 压缩文件的方法: //将工程中picture添加到左面111.zip压缩文件中 如果崩溃请更换压缩路径 -(void)testZipFile{ //压 ...

  7. android Webview 实现js调用java代码实现Activity跳转

    今天有了一个需求,在android里webview加载的html页面,要求点击html页面的按钮实现Activity的跳转. 咱是是菜鸟,webview的接触不多,于是就和度娘来了次亲密接触.在其中也 ...

  8. 机器学习13—PCA学习笔记

     主成分分析PCA 机器学习实战之PCA test13.py #-*- coding:utf-8 import sys sys.path.append("pca.py") impo ...

  9. springmvc上传方法

    /** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...

  10. 再理解 as3.0接口

    As3.0 接口的理解与运用 1.把接口当作"类"来理解.你easy接受她. 我们看她的标准结构: package 包路径{ public interface 接口名称{ func ...