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. 1. null or empty string
  2. 2. white spaces
  3. 3. +/- sign
  4. 4. calculate real value
  5. 5. handle min & max

Java Solution

  1. public int atoi(String str) {
  2.  
  3. 	if (str == null || str.length() < 1)
  4.  
  5. 		return 0;
  6.  
  7. 	// trim white spaces
  8.  
  9. 	str = str.trim();
  10.  
  11. 	char flag = '+';
  12.  
  13. 	// check negative or positive
  14.  
  15. 	int i = 0;
  16.  
  17. 	if (str.charAt(0) == '-') {
  18.  
  19. 		flag = '-';
  20.  
  21. 		i++;
  22.  
  23. 	} else if (str.charAt(0) == '+') {
  24.  
  25. 		i++;
  26.  
  27. 	}
  28.  
  29. 	// use double to store result
  30.  
  31. 	double result = 0;
  32.  
  33. 	// calculate value
  34.  
  35. 	while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
  36.  
  37. 		result = result * 10 + (str.charAt(i) - '0');
  38.  
  39. 		i++;
  40.  
  41. 	}
  42.  
  43. 	if (flag == '-')
  44.  
  45. 		result = -result;
  46.  
  47. 	// handle max and min
  48.  
  49. 	if (result > Integer.MAX_VALUE)
  50.  
  51. 		return Integer.MAX_VALUE;
  52.  
  53. 	if (result < Integer.MIN_VALUE)
  54.  
  55. 		return Integer.MIN_VALUE;
  56.  
  57. 	return (int) result;
  58.  
  59. }

[算法]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. [转] java代码块 介绍

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...

  2. PyCharm搭建Spark开发环境 + 第一个pyspark程序

    一, PyCharm搭建Spark开发环境 Windows7, Java 1.8.0_74, Scala 2.12.6, Spark 2.2.1, Hadoop 2.7.6 通常情况下,Spark开发 ...

  3. windows upd广播包无法发送到局域网解决方法

    不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是Win7,Win10还是Xp. 禁用VMw ...

  4. Ajax主要用来完成哪些工作?

    AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下. 它使用 JavaScript 在 web 浏览器与 web 服务器之间来发送和接收数据. ajax主要用于在页面内容加 ...

  5. 开发app应用的开源项目推荐

    app检测内存泄漏 请看这里:LeakCanary Android 和 Java 内存泄露检测 app应用想要控制状态栏 StatusBarUtil :https://github.com/laobi ...

  6. Linux3_文件系统

    1.Linux发行版本之间的差别很少,差别主要表现在系统管理的特色工具以及软件包管理方式的不同.目录结构基本上都是一样的. Windows的文件结构是多个并列的树状结构,最顶部的是不同的磁盘(分区), ...

  7. 可执行jar包

    我已经解决了这个问题,在eclipse中有一个打包工具,可以将程序打包成.jar文件: 右键要打包的 project--->Export--->Java--->JAR file--- ...

  8. phalcon builder 用法

    $rawSql = $builder->columns(["aa","bb"]) ->from("TableName") -&g ...

  9. MapReduce Input Split(输入分/切片)具体解释

    看了非常多博客.感觉没有一个说的非常清楚,所以我来整理一下. 先看一下这个图 输入分片(Input Split):在进行map计算之前,mapreduce会依据输入文件计算输入分片(input spl ...

  10. Idea 远程调试jenkins 项目

    1.Jenkins配置 jenkins 服务启动时 需要在jvm启动项里加入如下代码: -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y ...