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. MVC - 身份验证

    FormsAuthenticationTicket  使用此类来为用户生成一个身份票据 持有该票据则说明该用户是通过了身份验证的用户 可以随时访问某些资源 我们先创建几个类 //用户 public c ...

  2. Git 安装与简单使用(新手必看)

    1.安装git,默认下一步下一步等待安装完成 2.设置全局账号 安装之后去快速启动栏点击GitBash git config --global user.name "xiefeng" ...

  3. log4j中存在日志无法打印问题解决

    我在项目中配置双数据中心,原来类包名称前最都是一致的,后来由于项目的需要根据数据来源命名不同的类包名称,这个导致一个问题,sql语句运行无法正常打印出来,提示以下内容: log4j:WARN No a ...

  4. 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法

    今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...

  5. 封装一个ISortable接口

    using System;/// <summary>/// 排序规范/// </summary>/// <typeparam name="T"> ...

  6. jenkins(二)项目构建

    通过上一篇“jenkins(一)集成环境搭建示例”,已经完成了jenkins的安装,基本配置,启动,下面继续小结jenkins使用 一.jenkins系统配置 访问jenkins,点击系统管理-> ...

  7. iOS开发——UIWebView

    (已写好代码,待空闲更新……)

  8. java线程中生产者与消费者的问题

    一.概念 生产者与消费者问题是一个金典的多线程协作的问题.生产者负责生产产品,并将产品存放到仓库:消费者从仓库中获取产品并消费.当仓库满时,生产者必须停止生产,直到仓库有位置存放产品:当仓库空时,消费 ...

  9. Bzoj 2431 HAOI2009 逆序对数列

    Description 对于一个数列{ai},如果有i**<**j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数. ...

  10. 九度OJ 1086 最小花费--动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1086 题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对 ...