Implement function atoi to convert a string to an integer.

If no valid conversion could be performed, a zero value is returned.

If the correct value is out of the range of representable values, INT_MAX(2147483647) or INT_MIN (-2147483648) is returned.

Example

  1. "10" => 10
  2. "-1" => -1
  3. "123123123123123" => 2147483647
  4. "1.0" => 1
  1. public class Solution {
  2. public int myAtoi(String str) {
  3. if (str == null || str.length() == )
  4. return ;
  5. str = str.trim();
  6. if (str.length() == && (str.equals("+") || str.equals("-") || str.equals(".") || str.equals(""))) {
  7. return ;
  8. }
  9.  
  10. boolean isPositive = true;
  11. long current = ;
  12.  
  13. for (int i = ; i < str.length(); i++) {
  14. if (i == && str.charAt(i) == '+') {
  15. continue;
  16. } else if (i == && str.charAt(i) == '-') {
  17. isPositive = false;
  18. } else if (str.charAt(i) >= '' && str.charAt(i) <= '') {
  19. current = current * + str.charAt(i) - '';
  20. if (isPositive && current > Integer.MAX_VALUE)
  21. return Integer.MAX_VALUE;
  22. if (!isPositive && -current < Integer.MIN_VALUE)
  23. return Integer.MIN_VALUE;
  24. } else {
  25. break;
  26. }
  27. }
  28.  
  29. if (!isPositive) {
  30. current = -current;
  31. }
  32.  
  33. return (int) current;
  34. }
  35. }

String to Integer的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

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

  3. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

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

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

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

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

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

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

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

  8. String与Integer问题

    今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...

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

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

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

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

随机推荐

  1. office 软件常用备忘(删除线/冻结等)

    Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...

  2. Fedora和Ubuntu下安装OpenGL开发环境配置

    Fedora下OpenGl开发环境配置 开发OpenGL工程需要3个库文件和对应的头文件: libglut.so,libGLU.so,libGL.so, gl.h ,glu.h, glut.h 这些库 ...

  3. poj 1845 数论综合

    题意:求A^B的所有因数的和 mod 9901 sol:一开始毫无思路,因为很多定理都不知道-_-|| 1. 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^ ...

  4. 2014ACMICPC西安网赛1006

    题意:给你一个骰子的初始状态和可以进行的四种操作,求从初始状态到目标状态的最少操作次数 题目本身很简单,bfs即可.但是因为骰子有六个面,搜索判重和记录状态比较麻烦.这时候就需要神器STL了. #in ...

  5. 洛谷P2014 TYVJ1051 选课

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...

  6. hihocoder #1270 建造基地

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...

  7. POJ 2240Arbitrage(Floyd)

    E - Arbitrage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  8. html标签(一)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. xss跨站实例总结

    跨站脚本攻击(Cross-site scripting,通常简称为XSS)发生在客户端,可被用于进行窃取隐私.钓鱼欺骗.偷取密码.传播恶意代码等攻击行为. 恶意的攻击者将对客户端有危害的代码放到服务器 ...

  10. python之BIF函数在列表中的应用

    1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 T ...