leetcode8 String to Integer (atoi)
题目需求:
输入一个字符串,输出对应的int值
特殊处理:
输入: null 输出:0
输入: "a122" 输出:0
输入: " 1233" 输出:1233
输入: " -123" 输出:-123
输入: "+123" 输出:123
输入: " 123a233" 输出:123
输入: "-123 a 34" 输出:-123
输入: "1232334344556566" 输出:int类型的最大值
输入: "-123444554558" 输出:int类型的最小值
思路:
1、处理正号和负号
2、处理空格
3、处理数据的溢出
4、处理数据中间出现非数字字符
代码:
public static int myAtoi(String str){
if(str == null || str.equals("")){
return 0;
}
char[] chs = str.toCharArray();
long num = 0;
boolean flag1 = true;
int flag = 1;
for(int i=0; i<chs.length; i++){
//处理正负符号
if(flag1 && chs[i] == '+'){
flag1 = false;
continue;
}
if(flag1 && chs[i] == '-'){
flag = -1;
flag1 = false;
continue;
}
//处理空格
if(flag1 && chs[i] == ' '){
continue;
}
if(!(chs[i] >= '0' && chs[i] <= '9')){
//判断是否溢出
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return (int)num1;
}
}
}else{
flag1 = false;
num = num*10 + chs[i]-'0';
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
continue;
}
}
}
}
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return (int)num1;
}
}
}
leetcode8 String to Integer (atoi)的更多相关文章
- LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
随机推荐
- 浅谈 举家搬迁静态文件到CDN
由于七牛CDN最近做活动,对于标准用户可以免费使用如下优惠 10 GB 存储空间 10 G/月 下载流量 10 万次/月 PUT/DELETE 请求 100 万次/月 GET 请求 以上这些指标直接就 ...
- NRF51822之app_button使用
我们现在开始使用app_button,为什么要使用这个来替代直接使用GPIOTE呢? 因为我们在手册中可以看到如果一直开启GPIOTE in模式的需要需要很大电流.所以我们需要使用RTC来“周期”的查 ...
- zepto源码--isEmptyObject,isNumeric,inArray,trim--学习笔记
1.isEmptyObject,判断对象是否为空对象的函数 定义变量name,遍历传入对象的属性name,如果存在任何属性,则返回false,判定传入的参数为非空对象,否则即为空对象. 2.isNum ...
- ArcGIS API for JavaScript 4.0(一)
原文:ArcGIS API for JavaScript 4.0(一) 最近ArcGIS推出了ArcGIS API for JavaScript 4.0,支持无插件3D显示,而且比较Unity和Sky ...
- Magento Error – The directory is not writable by server.
When trying to use the insert image functionality in Magento if you receive an error saying: “The di ...
- UVA11538 - Chess Queen(数学组合)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 学习一下Fiddler的强大
①引言:Fiddler (中文名称:小提琴)是一个 HTTP 的调试代理,以代理服务器的方式,监听系统的Http网络数据流动, Fiddler 可以也可以让你检查所有的 HTTP 通讯,设置断点,以及 ...
- Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select
此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...
- jquery在线手册
开发时用到jquery,有几个函数想不起来怎么用,找了一下jquery在线手册. 记录一下,下回有需要再看看. 链接:http://www.chenfahui.cn/jq/
- Excl 的一些用法--如何给很多列赋同一个值
1.用鼠标选定要负责的列(多列) 2.在处填写值 3.Ctrl+Enter