String to Integer (atoi)

time=272ms   accepted

需考虑各种可能出现的情况

public class Solution {
public int atoi(String str) {
int length=str.length();
long result=0;
int flag=1;
Boolean bFlag=false,bSpace=false,bNum=false;
if(length<=0)
return (int) result;
else{
char[] s=str.toCharArray();
for(int index=0;index<length;index++){
if(s[index]==32){
if(!bSpace)
bSpace=true;
if(bSpace&&bNum)
return (int) ((int)flag*result);
}else if(s[index]==43||s[index]==45){
if(!bFlag){
flag=-flag*(s[index]-44);
bFlag=true;
bNum=true;
}else{
return (int) (flag*result);
}
}else if(s[index]<48||s[index]>57){
//0-9 ASCII:48-57 +:43 -:45 space:32
//System.out.println("Invalid Input!");
return (int) (flag*result);
}else{
bNum=true;
result=result*10+(s[index]-48);
} if (flag*result > Integer.MAX_VALUE)
return Integer.MAX_VALUE; if (flag*result < Integer.MIN_VALUE)
return Integer.MIN_VALUE; } } return (int) (flag*result);
}
}

leetcode第八题 String to Integer (atoi) (java)的更多相关文章

  1. leetcode第八题--String to Integer (atoi)

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

  2. LeetCode【8】. String to Integer (atoi) --java实现

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  3. [leetcode]经典算法题- String to Integer (atoi)

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

  4. LeetCode(8)String to Integer (atoi)

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

  6. LeetCode----8. String to Integer (atoi)(Java)

    package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  7. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  8. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  9. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

随机推荐

  1. Java实现简单选择排序

    package select; import java.util.Scanner; /*采用最简单的选择方式:从头到尾扫描序列找出最小的记录和第一个记录交换,接着在剩下的记录中继续这种选择和交换,最终 ...

  2. 页面常见效果js实现

    2015.12.2 页面常见效果js实现 [有没有觉得很坑,[笑哭,邮箱写上]] 复习: Js内置对象: 1.浏览器对象 window document history location event  ...

  3. 基于Windows的套接字相关函数及示例

    链接ws2_32.lib库 头文件#include <winsock2.h> int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSADa ...

  4. .net中下载文件的方法(转)

    .net中下载文件的方法 一.//TransmitFile实现下载      protected void Button1_Click(object sender, EventArgs e)      ...

  5. JQ实现复选框的全选反选不选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  7. ACM——进制转换

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1012 进制转换 时间限制(普通/Jav ...

  8. PHP中的魔术方法总结

    1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...

  9. windows访问linux共享

    1. 安装samba yum install  samba 2. 配置samba配置文件,添加共享文件夹 vim   /etc/samba/smb.conf 3. 关闭selinux vi  /etc ...

  10. linux/windows系统oracle数据库简单冷备同步

    linux/windows系统oracle数据库简单冷备同步 我们有一个财务系统比较看重财务数据的安全性,同时我们拥有两套系统,一个生产环境(linux),一个应急备份环境(windows).备份环境 ...