LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi)
8. String to Integer (atoi)
题目描述
LeetCode8. String to Integer (atoi)中等
Java 实现
class Solution {
public int myAtoi(String str) {
if (str == null || str.trim().length() == 0) {
return 0;
}
str = str.trim();
char firstChar = str.charAt(0);
int sign = 1, start = 0, len = str.length();
long sum = 0;
if (firstChar == '+') {
sign = 1;
start++;
} else if (firstChar == '-') {
sign = -1;
start++;
}
for (int i = start; i < len; i++) {
if (!Character.isDigit(str.charAt(i))) {
return (int) sum * sign;
}
sum = sum * 10 + str.charAt(i) - '0';
if (sign == 1 && sum > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (sign == -1 && sign * sum < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
}
return (int) sum * sign;
}
}
相似题目
参考资料
- https://leetcode.com/problems/string-to-integer-atoi/
- https://leetcode-cn.com/problems/string-to-integer-atoi/
LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))的更多相关文章
- 【LeetCode 8_字符串_实现】String to Integer (atoi)
, INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...
- 前端与算法 leetcode 8. 字符串转换整数 (atoi)
目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...
- Java实现 LeetCode 8 字符串转换整数(atoi)
8. 字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...
- [LeetCode] 8. 字符串转换整数 (atoi)
题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...
- 每日一题LeetCode 8. 字符串转换整数 (atoi)
问题描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将 ...
- LeetCode 8.字符串转换整数 (atoi)(Python3)
题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该 ...
- [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 字符串转数字练习--String to Integer (atoi)
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- LeetCode Golang 8. 字符串转换整数 (atoi)
8. 字符串转换整数 (atoi) 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组 ...
随机推荐
- [Codeforces 1242C]Sum Balance
Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \( ...
- [PWA] Storage information for PWA application
Be careful with the storage use cases, free the storage when it is necessary.
- ERP study
- [RN] React Native 生成 Android APK
在用模拟器或者真机调试完App后,需要将App打包成Apk发布文件. 下面简单记录下打包步骤: 第一:生成签名密钥 这一步的操作主要是生成需要的签名密钥,供android调用,生成的文件待用 在项目根 ...
- 【区间dp】P1063 能量项链
一道区间dp的水题 题目链接 来快活啊! 思路 很简单的区间dp,思路和floyed差不多,就是需要把项链处理成环形 用\(f[l][r]\)表示以\(a[l]\)开头\(a[r]\)结尾的数串的最大 ...
- 【数据结构】Java版
有趣有内涵的文章第一时间送达! 喝酒I创作I分享 生活中总有些东西值得分享 @醉翁猫咪 想你吴亦凡;赵丽颖 - 想你 你是程序猿对吗?会写代码的那种? 我是打字猿?会打代码的那种? 现在告诉大家一个很 ...
- GoCN每日新闻(2019-10-15)
GoCN每日新闻(2019-10-15) GoCN每日新闻(2019-10-15) 1. Go Module 存在的意义与解决的问题 https://www.ardanlabs.com/blog/20 ...
- [USACO5.1]二维凸包模板
传送门 Description 求\(n\)个点凸包的周长 Solution 计算几何打暴力必备 Code #include<bits/stdc++.h> #define reg re ...
- chown与chmod的区别
chown 修改文件和文件夹的用户和用户组属性 1.要修改文件hh.c的所有者.修改为sakia的这个用户所有 chown sakia hh.c 这样就把hh.c的用户访问权限应用到sakia作为所有 ...
- maven 发现有一个包 需要升级包版本
maven有个包需要升级版本号,但是升级完了之后,怎么编译都是原来的: 后来修改了一下版本号,然后编译,就好了,再把版本号改回来就行了: 本地版本库地址:File==>Other Setting ...