题目说明:

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.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

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.

程序代码:

  1. #include <gtest/gtest.h>
  2. using namespace std;
  3.  
  4. int myAtoi(string str)
  5. {
  6. int nLength = str.length();
  7. if (nLength == 0)
  8. {
  9. return 0;
  10. }
  11.  
  12. char* pszData = (char*)str.c_str();
  13. for (int i=0; i<nLength; ++i)
  14. {
  15. if (' ' != *pszData)
  16. {
  17. break;
  18. }
  19.  
  20. ++pszData;
  21. }
  22.  
  23. // Positive or negative
  24. int nFlag = 1;
  25. if ('+' == *pszData)
  26. {
  27. ++pszData;
  28. }
  29. else if('-' == *pszData)
  30. {
  31. nFlag = -1;
  32. ++pszData;
  33. }
  34.  
  35. // ignore base case.
  36. char cValue;
  37. long long nResult = 0;
  38. while( (cValue = *pszData) != '\0')
  39. {
  40. if ( (cValue >= '0') && (cValue <= '9'))
  41. {
  42. nResult = nResult * 10 + cValue - '0';
  43. }
  44. else
  45. {
  46. break;
  47. }
  48.  
  49. if (nResult > 0x80000000)
  50. {
  51. break;
  52. }
  53.  
  54. ++pszData;
  55. }
  56.  
  57. nResult *= nFlag;
  58. if (nResult > std::numeric_limits<int>::max())
  59. {
  60. nResult = std::numeric_limits<int>::max();
  61. }
  62. else if(nResult < std::numeric_limits<int>::min())
  63. {
  64. nResult = std::numeric_limits<int>::min();
  65. }
  66.  
  67. return (long)nResult;
  68. }
  69.  
  70. TEST(Pratices, tMyAtoi)
  71. {
  72. // 123
  73. ASSERT_EQ(myAtoi("123"),123);
  74. ASSERT_EQ(myAtoi("-123123"),-123123);
  75. ASSERT_EQ(myAtoi("-123123abdf"),-123123);
  76. ASSERT_EQ(myAtoi("2147483648"),2147483647);
  77. ASSERT_EQ(myAtoi("2147483647"),2147483647);
  78. ASSERT_EQ(myAtoi("-2147483649"),-2147483648);
  79. ASSERT_EQ(myAtoi("9223372036854775809"),2147483647);
  80.  
  81. }

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

  1. [leetcode]经典算法题- String to Integer (atoi)

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

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

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

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

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

  4. 【leetcode】String to Integer (atoi)

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

  5. No.008 String to Integer (atoi)

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

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

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

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

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

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

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

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

随机推荐

  1. maven 打包 war 包含 WEB-INF/lib 目录

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  2. maven 根据profile,resources,filters来区分部署环境

    项目过程中,在不同的阶段,分别需要部署开发环境,测试环境,线上环境.如果都用一套配置文件,很容易弄乱,所以维持多套配置文件很有必要. maven提供了一组属性以供开发人员灵活搭配,可以根据环境来打包, ...

  3. 使用Java Servlet进行简单登录

    效果图 登录页面代码:login.html <%@ page language="java" contentType="text/html; charset=UTF ...

  4. 创建第一个WCF服务

    创建WCF服务 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序. 2.建立完成后如下图所示: 3.删除系统生成的两个文件IService1.cs与Service1.s ...

  5. Nginx教程(五) Nginx配置文件详解

    一. Nginx配置文件nginx.conf中文详解 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processe ...

  6. XRP(瑞波币)账户管理系统

    目录 账户管理 分配常规密钥对 修改或移除常规密钥对 设置多重签名 发送多签名交易 账户管理 分配常规密钥对 XRP Ledger允许帐户授权二级密钥对(称为常规密钥对)来对未来的交易进行签名, 如果 ...

  7. java并发编程(8)原子变量和非阻塞的同步机制

    原子变量和非阻塞的同步机制 一.锁的劣势 1.在多线程下:锁的挂起和恢复等过程存在着很大的开销(及时现代的jvm会判断何时使用挂起,何时自旋等待) 2.volatile:轻量级别的同步机制,但是不能用 ...

  8. Java中用双缓冲技术消除闪烁

    在Java编写具有连贯变化的窗口程序时,通常的办法是在子类中覆盖父类的paint(Graphics)方法,在方法中使用GUI函数实现窗口重绘的过程.连贯变换的窗口会不断地调用update(Graphi ...

  9. C# 批量 json 读取

    // 方法一 //string test = "[{ 'CreateUser': 'CN=koujirou nishikawaOMHBK','CreateUserJ': '西川 公二郎'}, ...

  10. 二、cent OS安装配置tomcat

    下载tomcat的tar包http://tomcat.apache.org/download-80.cgi 确保安装前已经安装JDKjava -version如果没有安装可以参考上一篇文章:http: ...