Implement atoi to convert a string to an integer.
来源:互联网 发布:开淘宝企业店流程 编辑:程序博客网 时间:2024/05/18 02:24
package com.dengpf.StringToInteger;/** * Created by kobe73er on 16/12/17. */public class Solution { public int myAtoi(String str) { if (str == null || str.length() < 1) return 0; // trim white spaces str = str.trim(); char flag = '+'; // check negative or positive int i = 0; if (str.charAt(0) == '-') { flag = '-'; i++; } else if (str.charAt(0) == '+') { i++; } // use double to store result double result = 0; // calculate value while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') { result = result * 10 + (str.charAt(i) - '0'); i++; } if (flag == '-') result = -result; // handle max and min if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE; return (int) result; } public static void main(String args[]) { Solution solution = new Solution(); System.out.println(solution.myAtoi("")); }}
0 0
- Implement atoi to convert a string to an integer.
- Implement atoi to convert a string to an integer.
- leetcode 8 Implement atoi to convert a string to an integer.
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- string to integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer(atoi)
- String to Integer (atoi)
- bzoj 2464: 中山市选[2009]小明的游戏 最短路
- Linux下SVN常用命令
- Android布局控件相同属性的简单写法style
- Java中关于String类型的10个问题
- Retina 显示屏的网页图片兼容方案
- Implement atoi to convert a string to an integer.
- 银河英雄传说(并查集)
- 最小生成树-prim算法
- SqlServer索引及优化详解
- web前端之JavaScript高级程序设计六:事件
- eclipse中的project->clean做了什么
- web页面
- Zookeeper的启动流程
- C++json文件的使用