【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, ...
随机推荐
- Spring AOP详解 、 JDK动态代理、CGLib动态代理
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...
- OpenCV中的全景拼接例程
使用Stitcher类,通过createDefault()方法创建拼接对象,通过stitch()方法执行默认的自动拼接.自动拼接和07年Brown和Lowe发表的论文描述的步骤基本一致,只不过使用的特 ...
- RequestMethod 相关
Http协议的Delete和Put方法是做什么的?怎么用? RequestMethod 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一般来说,Web服务器默认的只支持Pos ...
- UOJ150 运输计划
运输计划(transport.cpp/c/pas)[问题描述]公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n-1 条 双向 航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- Longest Common Subsequence (LCS)
最长公共子序列(LCS)是经典的DP问题,求序列a[1...n], b[1..m]的LCS. 状态是DP[i][j],表示a[1..i],b[1..j]的LCS. DP转移方程是 DP[i][j]= ...
- 修改eclipse/MyEclipse中包的显示结构为树形
在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图:
- jmap命令详解
1.命令基本概述 Jmap是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本.打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其 ...
- MyEclipse------如何在特定目录下创建文件夹
Directory.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- vs配置
每次遇到vs配置都要让我头疼一段时间,对于某些不太清楚,有时自己试着配置,能运行起来就行,下次又忘了咋陪的了,其中配置的东西真心多. 1.输出目录这样配置../../Bin/Server/ 这个路径是 ...
- 初学Hibernate主键生成策略
具有业务含义的主键叫自然主键:随机生成,不具备业务含义的字段作为主键,叫代理主键. 在表与POJO类关系映射文件XXX.hbm.xml中,可通过配置id元素下generator节点的class属性指定 ...