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.

解题思路:

本题难度并不大,比较无聊,主要是要考虑到几个边界条件,本人也是提交数次才通过的。

JAVA代码如下:

	static public int myAtoi(String str) {
if (str == null )
return 0;
str = str.trim();
if(str.length()==0)
return 0;
boolean isNagetive = false; if (str.charAt(0) == '-')
isNagetive = true;
long result = 0;
for (int i = 0; i < str.length(); i++) {
if(i==0&&(str.charAt(0)=='-'||str.charAt(0)=='+'))
continue;
int temp = str.charAt(i) - '0';
if (temp >= 0 && temp <= 9)
{
if(isNagetive){
result=result * 10 - temp;
}
else result = result * 10 + temp;
if (result > Integer.MAX_VALUE)
return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
} else break;
}
return (int) result;
}

C++

 #include<algorithm>
using namespace std;
class Solution {
public:
int myAtoi(string str) {
bool isFirstChar = true, isNagetive = false;
long result = ;
for (int i = ; i < str.length(); i++) {
if (isFirstChar&&str[i] == ' ')
continue;
if (isFirstChar&&str[i] == '-') {
isNagetive = true;
isFirstChar = false;
continue;
}
if (isFirstChar&&str[i] == '+') {
isFirstChar = false;
continue;
}
int temp = str[i] - '';
if (temp >= && temp <= )
{
if (isNagetive) {
result = result * - temp;
}
else result = result * + temp;
if (result > INT_MAX)
return INT_MAX; if (result < INT_MIN)
return INT_MIN;
}
else break;
isFirstChar = false;
}
return (int)result;
}
};

【JAVA、C++】 LeetCode 008 String to Integer (atoi)的更多相关文章

  1. 【JAVA、C++】LeetCode 013 Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. 【JAVA、C++】LeetCode 015 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  5. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 【前端学习】git命令行界面

    学习目标:掌握git命令行界面的操作.掌握最基本的clone add commit push pull操作. 先下载客户端:http://github-windows.s3.amazonaws.com ...

  2. Cocos2d-X3.0 刨根问底(二)----- 从HelloWorld开始

    小鱼习惯直接从代码实例来学习一套成型的引擎库. 运行cpp-empty-test 一个典型的HelloWorld程序翻看代码结构 看到了 main.h与main.cpp文件就从这里开始 #ifndef ...

  3. BIEE连接数据库的方法

    BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...

  4. PHP配置,php.ini以及覆盖问题

    在部署一个cms项目到服务器上的时候,因为cms的模板比较老,服务器上用的php是5.3.3版(大于5.3,可以认为是新的),有些页面会显示"deprecated"类别的错误信息. ...

  5. fedora安装软件

    jdk 1.下载rpm包 注意32位还是64位,注意是rpm格式 2.安装 sudo rpm -ivh jdk.rpm sudo update-alternatives --config java # ...

  6. BZOJ-3226 校门外的区间 线段数+拆点(类似的思想)

    shabi题....bzoj关键字检查freopen??可怕,,1A的卡了一小时.... 3226: [Sdoi2008]校门外的区间 Time Limit: 10 Sec Memory Limit: ...

  7. 关于clonezilla

    Clonezilla 是一个很好的系统克隆工具,它可以说是吸取了 Norton Ghost 和 Partition Image 的优点.即不仅支持对整个系统进行克隆,而且也可以克隆单个的分区,这种灵活 ...

  8. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  9. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  10. object-c(oc)内存管理机制详解

    1.内存的创建和释放 让我们以Object-c世界中最最简单的申请内存方式展开,谈谈关于一个对象的生命周期.首先创建一个对象: 1 2 3 //“ClassName”是任何你想写的类名,比如NSStr ...