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. QT事件

    qtevents多线程工作object存储 Another Look at Events(再谈Events) 最近在学习Qt事件处理的时候发现一篇很不错的文章,是2004年季刊的一篇文章,网上有这篇文 ...

  2. FileFilter, FilenameFilter用法和文件排序

    FileFilter和FilenameFilter这两个类的用法都很简单,都只有一个方法 FileFilter /*** @param pathname The abstract pathname t ...

  3. Android(java)学习笔记144:Android音视频录制类MediaRecorder用法举例

    Android语音录制可以通过MediaRecorder和AudioRecorder.MediaRecorder本来是多媒体录制控件,可以同时录制视频和语音,当不指定视频源时就只录制语音(默认录制语言 ...

  4. Spring安全资料整理列表

    Spring 被爆漏洞,允许远程执行代码http://automationqa.com/forum.php?mod=viewthread&tid=2827&fromuid=21 Spr ...

  5. SQL Server调优系列基础篇 - 并行运算总结(一)

    前言 上三篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符.联合运算符的优化技巧. 本篇我们分析SQL Server的并行运算,作为多核计算机盛行的今天,SQL Server也会适时调整自 ...

  6. Object-C属性(Properties)

    前面我们写了caption和photographer的访问方法,你可能也注意到了,那些代码很简单,应该可以写成具有更普遍意义的形式. 属性是Object-C的一个特性,它允许我们自动生成访问器,同时还 ...

  7. WPF简单拖拽功能实现

    1.拖放操作有两个方面:源和目标. 2.拖放操作通过以下三个步骤进行: ①用户单击元素,并保持鼠标键为按下状态,启动拖放操作. ②用户将鼠标移到其它元素上.如果该元素可接受正在拖动的内容的类型,鼠标指 ...

  8. Codevs 1690 开关灯 USACO

    1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description YYX家门前的街上有N(2<=N& ...

  9. Git for Windows

    本篇文章由:http://www.sollyu.com/git-for-windows/ 说明 Git是用于Linux内核开发的版本控制工具.与CVS.Subversion一类的集中式版本控制工具不同 ...

  10. Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作

    Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作 1>. 创建一个控制台程序2>. 添加一个 ADO.NET实体数据模型,选择对应的数据库与表(Studen ...