【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)
题目
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
代码
public class Solution {public int atoi(String str) {String num_str="";char[] str_char=str.toCharArray();char[] sympo="-+".toCharArray();boolean abs=true;for(int i=0;i<str.length();i++){if(str_char[i]==sympo[0] ||str_char[i]==sympo[1]){if(!Character.isDigit(str_char[i+1])){return 0;}if(str_char[i]==sympo[0]){abs=false;}}if(Character.isDigit(str_char[i])){ num_str+=String.valueOf(str_char[i]); }else{if(num_str.length()!=0){break;}}
// if(Character.isDigit(str_char[i])){
// num_str+=String.valueOf(str_char[i]);
// }if(num_str!=""){if(Math.abs(Integer.parseInt(num_str))>=Integer.MAX_VALUE / 10){return Integer.MAX_VALUE;}}}if(num_str!=""){if(abs){return Integer.parseInt(num_str);}else{return -Integer.parseInt(num_str);}}else{return 0;}}}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)相关推荐
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- 【LeetCode从零单排】No189 .Rotate Array
题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- 【LeetCode从零单排】No67.AddBinary
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- 【LeetCode从零单排】No58.Length of Last Word
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...
- 【LeetCode从零单排】No38.CountAndSay
题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...
最新文章
- saltstack pkg模块用法
- js取整、四舍五入等数学函数
- 100的阶乘c语言代码,求10000的阶乘(c语言代码实现)
- Cordova(PhoneGap) 环境搭建与基础
- LeetCode 1863. 找出所有子集的异或总和再求和(DFS)
- 改ip地址会有什么影响_led显示屏会带来什么负面影响
- 一文带你 GNN 从入门到起飞,做一个饭盆最稳 GNN 饭人!
- Struts2教程3:struts.xml常用配置解析
- 永久的CheckBox(单选,全选/反选)!
- JavaSE——Java8之Stream流
- Cadence PSpice 模型3:从官网或者技术支持得到的PSpice模型与Capture库关联方法图文教程
- 浓缩大学生活的顶级精华
- 我的世界服务器哪个有自动铺路,我的世界自动铺路指令是什么
- 挖掘数百万参与的IMVU用户
- 服务器端查看图片库 eog display Xforwarding
- C# 自定义动态九宫格键盘,简单实用
- 浙江大学软件学院人工智能保研面经2021
- 百趣代谢组学分享:从SWATH到MRM:一种新型高覆盖度靶向代谢组学技术
- 怎样才算得上是一名优秀的软件测试工程师呢?
- DIY TCP/IP IP模块和ICMP模块的实现2
热门文章
- PAT甲级1090 Highest Price in Supply Chain:[C++题解]树、结点到根结点的距离、记忆化搜索、树形dp
- 数据结构_栈和队列的区别
- 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)
- LayoutInflater中调用系统服务
- java is number_数据类型----Number
- mysql主从复制自增_关于mysql主从复制自增长列
- 九江学院计算机主任黄冬久,陈春生副校长到实验中间调研引导工作
- android 关闭多点触控_Cocos Creator关闭多点触摸的问题
- 小乌龟 拉取项目_从有个叫“乌龟石”的地方讲起
- jenkins配置python脚本参数_jenkins配置python脚本定时任务过程图解