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中if的另类实现
偶然发现一篇有意思的博客<JS利用短路原理简写if语句> 利用&&短路来实现if的简写. 如 1==2&&a1=1,则后面的a1=1不会进行判断,1==1 ...
- ajaxfileupload.js
jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' ...
- Orale介绍
Oracle数据库: 是甲骨文公司的一款关系数据库管理系统 具有网格计算的框架 数据量大,并发操作比较多,实时性要求高,采取ORACLE数据库 Oracle数据库的体系结构包括物理存储结构和逻辑存储结 ...
- 【7集iCore3基础视频】7-3 iCore3硬件介绍
iCore3原理图介绍: 高清源视频:http://pan.baidu.com/s/1gfbhuE3%20密码:xnbc iCore3 购买链接:https://item.taobao.com/ite ...
- kuohao
#include <stdio.h> int b[50]; int a[50]; int w[50]; int main() { freopen("in.txt",&q ...
- MarkdownPad 2
摘要 升级到 Windows 10 后 MarkdownPad 2,遇到了html 渲染错误的问题: windows10 MarkdownPad html渲染错误 awesomium 升级到 Wind ...
- jfinal路由简单解析
在jfinal中,通过JFinalFilter对所有的类进行过滤. 以下是路由的调用关系(我在调用关系旁边做了标记,会贴出具体的代码和解释): -1- Config: Routes -2- Inter ...
- PHP:array_chunk()数组分割
array_chunk(); 作用:把一个数组分割为新的数组块 用法: 实例:把数组分割为带有两个元素的数组块 $cars=array("Volvo","BMW" ...
- swift2.0单例
import Foundation public class SwiftSingleton{ var name = "000" public static le ...
- Lua JSONRPC学习笔记
JSON RPC JSON RPC 为利用json数据格式来执行远程调用方式, 作用同xmlrpc,不过与xmlrpc相比, jsonrpc更加轻量,json更加节省数据量,更加可读性高. 官网网站: ...