【JAVA、C++】 LeetCode 008 String to Integer (atoi)
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)的更多相关文章
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- javaEE规范和SSH三大框架到底有什么关系
转自博客:http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...
- java系统高并发解决方案(转载)
转载博客地址:http://blog.csdn.net/zxl333/article/details/8454319 转载博客地址:http://blog.csdn.net/zxl333/articl ...
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
闻说HNOI每年都有一道Hash. 1206: [HNOI2005]虚拟内存 Time Limit: 50 Sec Memory Limit: 162 MB Submit: 330 Solved: 2 ...
- BZOJ3172 后缀数组
题意:求出一篇文章中每个单词的出现次数 对样例的解释: 原文是这样的: a aa aaa 注意每个单词后都会换行 所以a出现次数为6,aa为3 (aa中一次,aaa中两次),aaa为1 标准解法好像是 ...
- C++用new和不用new创建类对象区别
new创建类对象,使用完后需使用delete删除,跟申请内存类似.所以,new有时候又不太适合,比如在频繁调用场合,使用局部new类对象就不是个好选择,使用全局类对象或一个经过初始化的全局类指针似乎更 ...
- easyloader.js源代码分析
http://www.cnblogs.com/jasonoiu/p/easyloader_source_code_analysis.html Jquery easyui是一个javascript UI ...
- 如何用sqlyog实现远程连接mysql
1,sqlyog客户端,用root用户远程链接mysql时,提示“访问被拒绝”,在网上搜索了一下原因. 原来是mysql没有授权其远程链接,所以你只能在客户端里面链接. 怎么解决呢? 原表数据 mys ...
- 字符串模拟赛T3
只看我的做法就够了 #include<iostream> #include<cstdio> #include<string> #include<cstring ...
- thinkphp中page方法
page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 用法 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例 ...
- 怎么让dedecms生成html页面更快些
如何让织梦生成html页面更快些呢? 1.把文章模板里的“相关文章”.“热点文章”.“推荐文章”这类的标记删除了,用其它方式,如:shtml.js 引入 2.把织梦模板里用标记表示的模板路径.php附 ...