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. [Web] About image: MozJPEG

    Image is quite heavy in web traffic. it is about 53% whole web traffic. It is important to make sure ...

  2. 手写队列以及stl中队列的使用

    一,手写队列. struct queue { ; ,rear=,a[maxn]; void push(int x) { a[++rear]=x; } void pop() { first++; } i ...

  3. MongoDB 4.2 的主要亮点(转载)

    在6月份召开的MongoDB全球用户大会上, MongoDB官宣了MongoDB Server 4.2,在经过100,000多个运行实例的测试后,MongoDB 4.2表现强劲.现在4.2版本正式上线 ...

  4. WinDbg常用命令系列---符号相关命令

    ld (Load Symbols) ld命令加载指定模块的符号并更新所有模块信息. ld ModuleName [/f FileName] 参数: ModuleName指定要加载其符号的模块的名称.m ...

  5. 洛谷 P1063 能量项链 题解

    P1063 能量项链 题目描述 在\(Mars\)星球上,每个\(Mars\)人都随身佩带着一串能量项链.在项链上有\(N\)颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并 ...

  6. 均值不等式中的一则题目$\scriptsize\text{$(a+\cfrac{1}{a})^2+(b+\cfrac{1}{b})^2\ge \cfrac{25}{2}$}$

    例题已知正数\(a.b\)满足条件\(a+b=1\),求\((a+\cfrac{1}{a})^2+(b+\cfrac{1}{b})^2\)的最小值: 易错方法\((a+\cfrac{1}{a})^2+ ...

  7. 安卓入门教程(十四)-菜单,ActionBar,对话框

    已经发表个人公众号 菜单类型 选项菜单(OptionMenu) 子菜单(SubMenu) 上下文菜单(ContextMenu) 方法: public boolean onCreateOptionsMe ...

  8. mysql 获取字符串的长度

    mysql> select * from test; +----+------------+-------+-----------+ | id | name | score | subject ...

  9. DB proxy, mysql proxy

    db proxy 在大型互联网站的数据库部署中,部署最多的数据库为MySQL.随着MySQL中Innodb存储引擎对事物的支持,MySQL在互联网公司部署中,应用量越来越多.典型应用MySQL的公司有 ...

  10. python opencv PyQt5

    import cv2 import numpy as np import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from P ...