Leetcode: String to Integer (atoi)
2015-09-10 21:51
387 查看
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.
代码如下:
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.
代码如下:
public static int reverse(String str) { String s = str.trim(); int length = s.length(); if(length == 0){ return 0; } boolean isPositive = true; int i = 0; if(s.charAt(i)=='-'||s.charAt(0)=='+'){ if(s.charAt(0)=='-'){ isPositive = false; i++; }else { while(s.charAt(++i)=='+'){ } if(s.charAt(i)=='-'){ isPositive = false; i++; } } } long revResult = 0; while(i < length){ if(s.charAt(i)!= ' '){ revResult = revResult*10 + s.charAt(i)-'0'; } i++; } if(isPositive == false){ revResult = -revResult; } return (int)revResult; }
相关文章推荐
- 阿里云-ECS-IPTABLE防火墙-安装
- Java 多线程中的任务分解机制-ForkJoinPool详解
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- 紫影龙的编程日记—— MFC中的常用函数
- C++Primer第五版 2.1.2节练习
- hibernate 一级缓存
- 用C语言实现常用排序算法
- nyoj 608 畅通工程 【迪杰斯特拉】
- 《程序员面试金典》--空格替换
- uvaoj 10891 - Game of Sum
- 面积最大的全1子矩阵(腾讯2012年暑期实习生招聘面试二面试题)
- 4.4 给定的点是否在三角形之内
- 32 Longest Valid Parentheses
- [CTSC2007]挂缀 解题报告
- 【iOS7一些总结】9、与列表显示(在):列表显示UITableView
- Ubuntu安装中文输入法
- 【1】基于TQ2440的MP3设计——【10、播放处理模块】
- 编程技巧 - 4
- 基础一:Activity生命周期
- Oracle SQL基础