LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8;
/*
* 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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
*/ public class Solution {
public static int myAtoi(String str) {
StringBuilder sb=new StringBuilder();
int flag=0;
if(str.length()==0)
return 0;
else{
for(int i=0;i<str.length();i++){
if(str.charAt(i)<'0'||str.charAt(i)>'9')
if(str.charAt(i)=='0'||str.charAt(i)==' ')
if (sb.length()==0)
continue;
else
break;
else if (str.charAt(i)=='-')
if (flag==0){
flag=-1;
sb.append('0');
}
else
break;
else if (str.charAt(i)=='+')
if (flag==0){
flag=1;
sb.append('0');
}
else
break;
else
break;
else
sb.append(str.charAt(i));
}
}
if(sb.length()==0)
return 0;
else if(sb.length()>12)
str=sb.substring(sb.length()-12);
else
str=sb.toString();
if (flag==-1)
str="-"+str;
long result=Long.parseLong(str);
System.out.println(result);
if (result>2147483647)
return 2147483647;
else if (result<-2147483648)
return -2147483648;
else
return (int)result; }
public static void main(String[] args){
String str1="";//
String str2="+";//
String str3=" +45555";//
String str4=" -45555";//-45555
String str5="2147483647";//
String str6="2147483648";//
String str7="+-2";//
String str8="-+2";//
String str9="++2";//
String str10="-2147483648";//-2147483648
String str11=" - 321";//
String str12=" -11919730356x";//-2147483648
String str13="1234567890123456789012345678901234567890";//
System.out.println(myAtoi(str1));
System.out.println(myAtoi(str2));
System.out.println(myAtoi(str3));
System.out.println(myAtoi(str4));
System.out.println(myAtoi(str5));
System.out.println(myAtoi(str6));
System.out.println(myAtoi(str7));
System.out.println(myAtoi(str8));
System.out.println(myAtoi(str9));
System.out.println(myAtoi(str10));
System.out.println(myAtoi(str11));
System.out.println(myAtoi(str12));
System.out.println(myAtoi(str13)); }
}
LeetCode----8. String to Integer (atoi)(Java)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Java [leetcode 8] String to Integer (atoi)
问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- Js文字特效—文字段逐个变色循环
自己用来练习的,附上详细注释,如果有和我一样喜欢并想要学习Dom特效创作的朋友,推荐先系统了解Javascript中Html Dom Object部分的内容,包括常用方法及属性. <!DOCTY ...
- linux中,常用的账号管理命令
创建新用户:adduser 用户名创建新用户并将其加入一个现有组中:adduser 用户名 -G 组名创建新用户并使其只属于该组:adduser 用户名 -g 组名创建用户密码:passwd 用户名创 ...
- FZU1894 单调队列
S - 1019 Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- ready是先执行的,load后执行,DOM文档的加载步骤
在jq中在文档载入完毕后有这几种方式去执行指定函数: $(document).ready(function() { // ...代码... }); //document ready 简写 $(func ...
- Linux常用文件介绍
您的一举一动都会记录在/var/log/secure 和/var/log/messages文件中
- viewPager的基本使用
viewPager是android扩展包v4中的类,这个类可以使用户左右切换当前的view. 特性: 1.viewPager直接继承了viewGroup类,所以它是一个容器类,可以在其中添加其它的vi ...
- ExtJS笔记 Store
The Store class encapsulates a client side cache of Model objects. Stores load data via a Proxy, and ...
- bootstrap插件学习
转自http://v3.bootcss.com/javascript/ bootstrap javascript插件学习 模态框 打开模态框 <button type="button& ...
- bootstrap组件学习
转自http://v3.bootcss.com/components/ bootstrap组件学习 矢量图标的用法<span class="glyphicon glyphicon-se ...
- WordPress 添加面包屑导航
所谓面包屑,就是类似这种:首页 > 公司简介 > 发展历史 展示网站树型结构,并让网站访问者随时知道自己所处的位置,方便返回上几级. 将下面的代码添加到主题的 functions.php ...