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 ...
随机推荐
- php中提示Undefined index的解决方法
我们经常接收表单POST过来的数据时报Undefined index错误,如下: $act=$_POST['action']; 用以上代码总是提示 Notice: Undefined index: a ...
- linux笔记八---------文件查找
1.find文件查找指令 > find 目录 参数 参数值,参数 参数值..... > find / -name passwd //从系统根目录开始递归查找name=p ...
- 汇编基础知识之二debug的使用
DEBUG的使用 (要在win32位习题下进行,win7 64位需要安装DosBox和debug这2个软件): 1:win64位下debug的使用教程: 下载debug.exe,这里我把debug放在 ...
- 二叉树-二叉查找树-AVL树-遍历
一.二叉树 定义:每个节点都不能有多于两个的儿子的树. 二叉树节点声明: struct treeNode { elementType element; treeNode * left; treeNod ...
- 20145337 《Java程序设计》第二周学习总结
20145337 <Java程序设计>第二周学习总结 教材学习内容总结 Java可分基本类型与类类型: 基本类型分整数(short.int.long).字节(byte).浮点数(float ...
- IOS第11天(3:UIPickerView省市联动)
********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...
- Apache Spark技术实战之4 -- 利用Spark将json文件导入Cassandra
欢迎转载,转载请注明出处. 概要 本文简要介绍如何使用spark-cassandra-connector将json文件导入到cassandra数据库,这是一个使用spark的综合性示例. 前提条件 假 ...
- centos安装配置amoeba以及测试
一.amoeba介绍网址:http://docs.hexnova.com/amoeba/ 二.安装java se1.5 三.安装amoeba2.2.01.下载地址:http://sourceforge ...
- 详解依赖注入(DI)和Ioc容器
简单的来说,关键技术就是:注册器模式. 场景需求 我们知道写一个类的时候,类本身是有个目的的,类里面有很多方法,每个方法搞定一些事情:我们叫这个类为主类. 另外这个主类会依赖一些其他类的帮忙,我们叫这 ...
- sublimetext ruby 插件
写ruby的编辑器推荐 俗话说磨刀不误砍柴工,好的编辑器可以写的更舒服,更快. 完全初学者建议用RubyMine,这个目前估计最强的写Ruby的IDE. 不过我没有使用它,因为它速度太慢了.如果你能 ...