[leetcode]_String to Integer (atoi)
非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。
题目:将一个string转换为int。实现函数atoi()的功能。
先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了)
input | output |
”+1“ | 1 |
” + 1“ | 0(error了) |
” 1“ | 1(前头只有空格是合法的) |
”12b45“ | 12(取前面的数字) |
溢出 : ”2147483648“(负数情况同) | 2147483647(MAX_VALUE) |
类似我对atoi()的功能不熟的人来说,这道题就是不停WA的血泪史。每次要修正一次错误答案。
最终AC:
public int atoi(String str) {
int len = str.length();
long num = 0;
//用long型存储,以处理溢出的情况
int ifNegative = 1;
boolean numStatus = false;
for(int i = 0 ; i < len ; i++){
char ch = str.charAt(i);
if(numStatus && (ch < '0' || ch > '9')) break;
else if(numStatus && ch >= '0' && ch <= '9'){
num = num * 10 + (ch - '0');
}else if(!numStatus && ch != '-' && ch != '+' && (ch < '0' || ch > '9')){
num = 0;
break;
}else if(!numStatus && ch == '-'){
numStatus = true;
ifNegative = -1;
}else if(!numStatus && ch == '+'){
numStatus = true;
}else if(!numStatus && ch >= '0' && ch <= '9'){
numStatus = true;
num = num * 10 + (ch - '0');
} }
num *= ifNegative; int result = 0;
if(num > Integer.MAX_VALUE) result = Integer.MAX_VALUE;
else if(num < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
else result = (int) num; return result; }
[leetcode]_String to Integer (atoi)的更多相关文章
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode OJ-- String to Integer (atoi) **
https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] String to Integer (atoi) 字符串
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode]-algorithms-String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- [LeetCode]String to Integer (atoi)
题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...
- leetcode String to Integer (atoi) python
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...
随机推荐
- 类似 go get –u 的命令行参数实现
我们可能需要类似 go get –u -. 这样的方式来实现我们的应用,这时候我们无法简单地使用 flag.Parse 了,而是要用 FlagSet 了, 使用例子如下: package main ...
- 关于asp.net 网站网站发布时提示:错误 27 对路径 AppData\Local\Temp\~632b\bin\App_Code.compil的解决方法
关于asp.net 网站网站发布时提示:错误 27 对路径 AppData\Local\Temp\~632b\bin\App_Code.compil的解决方法 问题如下图所示,方法是去掉: <i ...
- C++primer练习14.44
编写一个简单的桌面计算器使其处理二元运算 // 14_44.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iost ...
- IntelliJ IDEA显示行号方法
File->Settings->Editor->General->Appearence->Show line numbers
- Grub2 使用摘记
>>>不使用子菜单 # sudo vim /etc/default/grub添加配置:GRUB_DISABLE_SUBMENU=yFrom:http://tieba.baidu.co ...
- [VB.NET]Dictionary类
字典类是一个很重要的类,尤其是对于数据的简单存储,查询,和处理. 废话不多说,简单记录下我探索的结果. 1. Dictionary内部索引是0基的.也就是说第一个元素的序号是0. 2. Public ...
- css选择器nth-child()和nth-of-type()的应用
<style> .table-striped tbody > tr:nth-child(odd) > td, .table-striped tbody > tr:nth- ...
- Unity原生渲染方案
Unity原生渲染方案 作者:3dimensions three_dimensions@live.cn 本文为原创内容,转载请注明出处. 做这个的动机是想在原生代码中使用Unity的材质系统绘制,同时 ...
- VR就是下一个浪潮_2016 (GMGC) 全球移动游戏大会观后感
"VR就是下一个浪潮" --2016 (GMGC) 全球移动游戏大会观后感 早在2014年参会Unity举办的一年一度的金立方盛典大会,就初次体验了VR头盔设备,于是印象深刻 ...
- python拷贝文件到多个文件夹
主要用来做数据备份,每次用完以后再跑一次脚本,又可以将文件夹下的所有文件拷贝到指定的文件夹内 import os,sys,shutil; class cur_env: path = sys.path[ ...