8. 字符串转换整数 (atoi)

8. String to Integer (atoi)

题目描述

LeetCode

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;
}
}

相似题目

参考资料

LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))的更多相关文章

  1. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

  2. 前端与算法 leetcode 8. 字符串转换整数 (atoi)

    目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...

  3. Java实现 LeetCode 8 字符串转换整数(atoi)

    8. 字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...

  4. [LeetCode] 8. 字符串转换整数 (atoi)

    题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...

  5. 每日一题LeetCode 8. 字符串转换整数 (atoi)

    问题描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将 ...

  6. LeetCode 8.字符串转换整数 (atoi)(Python3)

    题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该 ...

  7. [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  8. 字符串转数字练习--String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  9. LeetCode Golang 8. 字符串转换整数 (atoi)

    8. 字符串转换整数 (atoi) 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组 ...

随机推荐

  1. SQL查询结果拼接成字符串

    sqlserver中将查询结果拼接成字符串   #for xml path(param)--将查询结果以xml格式输出 1 select id,name from table1 for xml pat ...

  2. django-获取购物车商品数量-redis

    视图函数views.py中 from django_redis import get_redis_connection # 连接redis class IndexView(View): '''首页'' ...

  3. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  4. Mysql 创建只读账户

    mysql 创建只读账户: 1.查询所有账号信息 SELECT DISTINCT a.`User`,a.`Host`,a.password_expired,a.password_last_change ...

  5. CF1172E Nauuo and ODT

    CF1172E Nauuo and ODT 神仙题orz 要算所有路径的不同颜色之和,多次修改,每次修改后询问. 对每种颜色\(c\)计算多少条路径包含了这个颜色,不好算所以算多少条路径不包含这个颜色 ...

  6. P1913 L国的战斗之伞兵

    题目链接 P1913 L国的战斗之伞兵 思路 从无风点倒着dfs,本来是道大水题,结果输入的时候第二层循环打错了!手残打成i++ 代码 #include<iostream> #includ ...

  7. 第12组 Beta版本演示

    前言 组长博客 组名: To Be Done 组员和贡献比例 短学号 姓名 分工 贡献比例 614 王永福* 前后端实现.发任务.催进度 30% 440 孙承恺 UI设计 15% 529 邱畅杰 文本 ...

  8. mysql 显示表结构

    例子 mysql> show columns from table1; +------------+------------------+------+-----+---------+----- ...

  9. Mysql 之根据经纬度按距离排序

    一.方式一 st_distance 计算的结果单位是度,需要乘111195(地球半径6371000*PI/180)是将值转化为米. SELECT *, (st_distance(point(lng,l ...

  10. Linux系统学习(二)一Linux基本操作

    一.Linux的目录结构 1.1 Linux的目录结构图 1.2 目录内容 /:这就是根目录.对你的电脑来说,有且只有一个根目录.所有的东西,我是说所有的东西都是从这里开始.举个例子:当你在终端里输入 ...