[LeetCode]-algorithms-String to Integer (atoi)
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.
需求:字符串转成数字,需要识别正负号
""
=>0
-1
=>-1
+-1
=>0
1234567890123456789012345678901234567890
=>0
"2147483648"
=>2147483647
public int myAtoi(String str) {
        str = str.trim();
        if(null==str || "".equals(str))
            return 0;
        char flag = '+';
        int i = 0;
        if(str.charAt(0)=='+'){
            flag = '+';
            i++;
        }else if(str.charAt(0)=='-'){
            flag = '-';
            i++;
        }
        double result = 0.0;
        if(str.length()>12)
            return 0;
        while(str.length()>i && str.charAt(i)>='0' && str.charAt(i)<='9'){
            result = result * 10 + (str.charAt(i)-'0');
            i++;
        }
        if(flag=='-')
            result = -result;
        if(result>Integer.MAX_VALUE)
            result = Integer.MAX_VALUE;
        if(result<Integer.MIN_VALUE)
            result = Integer.MIN_VALUE;
        return (int)result;
    }
[LeetCode]-algorithms-String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
		Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ... 
- 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- ... 
- 【LeetCode】String to Integer (atoi) 解题报告
		这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ... 
- LeetCode 8. String to Integer (atoi) (字符串到整数)
		Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ... 
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
		Implement atoi which converts a string to an integer. The function first discards as many whitespace ... 
- LeetCode 7 -- String to Integer (atoi)
		Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ... 
随机推荐
- C++中的new/delete、构造/析构函数、dynamic_cast分析
			1,new 关键字和 malloc 函数区别(自己.功能.应用): 1,new 关键字是 C++ 的一部分: 1,如果是 C++ 编译器,则肯定可以用 new 申请堆空间内存: 2,malloc 是由 ... 
- svn下载项目的时候出现 Path to certificate
			svn关联的时候出现这种情况,并且有svn的账号的时候,可以找setting中Version Control 中的Subversion中celar 一下即可,然后再重新下载就会让你重新输入用户名和密码 ... 
- centos7 设置内核启动顺序
			1.查看设备上安装了几个内核 cat /boot/grub2/grub.cfg |grep menuentry 2.查看当前内核 grub2-editenv list saved_entry=Cent ... 
- 在easyui中解决使west和center为1:1,并且拖动窗口时能够自适应变化
			<script type="text/javascript"> // 解决页面两个grid的布局问题 $(function(){// 在页面加载完毕后 //consol ... 
- Java的GUI框架如何选择? Swing、SWT、AWT、SwingX、JGoodies、JavaFX、Apache Pivot、BeautyEye框架(美观)?
			AWT 是swing的基础,运行良好,但缺少高级组件.如果您打算创建丰富的应用程序,那么AWT可能不是最好的方法.但是对于不需要丰富用户界面的较小gui应用程序来说.这可能非常适合,因为它是一个久经考 ... 
- 这才是最完美SSD:性能满血发挥 万里挑一
			固态硬盘同质化日益严重,不同品牌的固态盘想要一决高下就只有靠品牌口碑与做工硬实力了. 最近影驰对他们的ONE系列固态硬盘产品进行了一波更新,推出了多种容量的ONE PCIe M.2 SSD,今天我们就 ... 
- AIX中的/etc/inittab文件
			1./etc/inittab文件 /etc/inittab文件从上到下逐行表述了某个服务或应用的启动需求.运行级别.应用脚本,格式如下: identifier:Runlevel: Action: ... 
- 认识JWT(转)
			1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ... 
- SetConsoleTextAttribute和SetConsoleScreenBufferInfoEx的使用
			主要是作用于控制台文本下划线和改变文本颜色 #include "pch.h" #include <iostream> #include <Windows.h> ... 
- SpringMVC 向前台页面传值-ModelAndView
			ModelAndView 该对象中包含了一个model属性和一个view属性 model:其实是一个ModelMap类型.其实ModelMap是一个LinkedHashMap的子类 view:包含了一 ... 
