【JAVA、C++】 LeetCode 008 String to Integer (atoi)

时间:2023-02-07 17:52:25

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&plus;&plus;】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&plus;&plus;】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&plus;&plus;】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&plus;&plus;】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&plus;&plus;】LeetCode 022 Generate Parentheses

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

  6. 【JAVA、C&plus;&plus;】LeetCode 010 Regular Expression Matching

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

  7. 【JAVA、C&plus;&plus;】LeetCode 007 Reverse Integer

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

  8. 【JAVA、C&plus;&plus;】LeetCode 006 ZigZag Conversion

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

  9. 【JAVA、C&plus;&plus;】LeetCode 003 Longest Substring Without Repeating Characters

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

随机推荐

  1. 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏

    最近互联网招聘平台拉勾网在五一期间推出个“最难面试的IT公司”代码挑战活动,评选出了5个最难面试的IT公司,即:ThoughtWorks.Google.Unisys.Rackspace.Cypress ...

  2. mysql 日志表rename 备份

    1. 按照原历史表新增一个新表(空表): mysql> create table history_log_new ...; 2. 给历史表重命名,并将新表重命名为历史表: mysql> R ...

  3. document&period;write 方法

    如果在文档加载结束后再调用document.write(),那么输出的内容将会 重写 整个页面. 某次被问及此问题,志之!

  4. Xamarin迁移到 Unified API 注意事项

    参考官方文档: Migrating to Unified API for Components #if __UNIFIED__ ... // Mappings Unified CoreGraphic ...

  5. 利用windows系统ftp命令编写的BAT文件上传&lbrack;转&rsqb;

    利用windows系统ftp命令编写的BAT文件上传[转] 利用windows系统ftp命令编写的BAT文件上传[转] 在开发中往往需要将本地的程序上传到服务器,而且用惯了linux命令的人来说.在w ...

  6. JAVA 反射应用:properties2Object

    MixAll.java import java.lang.reflect.Method; import java.util.Properties; public class MixAll { /** ...

  7. Android&lowbar;sharePreference

    /** * Android的四中数据存储方式: * 1.SharePreferences * 2.SQLite * 3.Content Provider * 4.File * * SharePrefe ...

  8. Gradle入门系列&lpar;转&rpar;

    Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注,这也是我决定去研究Gradle的原因. 这篇文章是 ...

  9. Entity Framework Core系列之DbContext(修改)

    上一篇我们介绍了Entity Framework Core系列之DbContext(添加),这一篇我们介绍下修改数据 修改实体的方法取决于context是否正在跟踪需要修改的实体. 下面的示例中实体由 ...

  10. eclipse如何导入jar包 BUILD PATH

    http://blog.csdn.net/believejava/article/details/41750987