`
淡淡的一抹
  • 浏览: 18979 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.
题目出处:https://oj.leetcode.com/problems/string-to-integer-atoi/

解题思路
本题实现字符串转换为整数的函数。

相关知识点
(1)根据下标删除字符串中的字符
String中并没有相应的函数,处理的方法是使用StringBuffer。
StringBuffer sb = new StringBuffer(str);
sb.deleteCharAt(0);//删除下标为0的字符


(2)String中的subString(int beginIndex, int endIndex)的方法中endIndex参数并不包括在内,也就是说是从beginIndex到endIndex-1。

(3)String中compareTo(String anotherString)中的比较规则是不管长度大小,总是从第一个字符开始比较,如果相同则返回0,大于或者小于返回两个的差值(一个大于0,一个小于0)。

自己的代码
package leetcode;

public class StringToIntegerAtoi {
	public int atoi(String str) {
		if(str == null) return 0;
		//将头部的空格去掉
		StringBuffer sb = new StringBuffer(str);
		for(int i = 0; i < sb.length(); i++){
			if(sb.charAt(i) == ' ') {
				sb.deleteCharAt(i);
				i = -1;
			}
			else break;
		}
		str = sb.toString();
		if(str.length() == 0) return 0;
		//判断首字符是否为“ + - 数字”
		int sign = 1;
		char firChar = str.charAt(0);
		if(firChar != '+' && firChar != '-' && !(firChar >= '0' && firChar <= '9'))
			return 0;
		if(firChar == '-' || firChar == '+'){
			if(str.length() == 1) return 0;
			else if(firChar == '-') sign = 0;
			str = str.substring(1);
		}
		//判断字符中是否全为数字,然后将非数字字符前的数字全部提取出来
		for(int i = 0; i < str.length(); i ++){
			char curChar = str.charAt(i);
			if(!(curChar >= '0' && curChar <= '9')) {
				if(i == 0) return 0;
				else {
					str = str.substring(0, i);
					break;
				}
			}
		}
		//判断是否越界
		if(str.length() > 10 && sign == 1) return 2147483647;
		if(str.length() > 10 && sign == 0) return -2147483648;
		if(str.length() == 10 && str.compareTo("2147483647") >= 0 && sign == 1) return 2147483647;
		else if(str.length() == 10 && str.compareTo("2147483648") >= 0 && sign == 0) return -2147483648;
		
		int sum = 0;
		int power = 0;
		for(int i = str.length() - 1; i >= 0; i--, power++){
			sum += (str.charAt(i)-'0')*Math.pow(10, power);
		}
		if(sign == 0) sum = 0 - sum;
		
        return sum;
    }
	
	public static void main(String[] args) {
		//String str = null;
		//String str = "";
		//String str = "+";
		//String str = "-";
		//String str = "+1";
		//String str = "+12";
		//String str = "-1";
		//String str = "-12";
		//String str = "1";
		//String str = "12";
		//String str = "+-1";
		//String str = "010";
		//String str = "    010";
		//String str = " -0012a42";
		//String str = "-2147483648";
		//String str = "-2147483649";
		//String str = "23a8f";
		String str = " -11919730356x";
		
		StringToIntegerAtoi stia = new StringToIntegerAtoi();
		System.out.println(stia.atoi(str));
	}
}

分享到:
评论

相关推荐

    LeetCode 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 ...

    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 ...

    leetcode2sum-Problems:编程问题的回购

    leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of writing this ...Integer ...String To Integer Atoi [Medium] LC9: Palindrome Number [Easy] LC11:

    leetcode-String-to-Integer-atoi

    leetcode字符串转换为整数

    Coding Interview In Java

    19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 ...

    leetcode小白刷题-String-to-Integer-atoi-:来自https://leetcode.com/problems/st

    (atoi) 示例 1: Input: " 42 " Output: 42 示例 2: Input: " -42 " Output: - 42 Explanation: The first non-whitespace character is ' - ' , which is the minus sign. Then take as many numerical digits as ...

    leetcode530-algorithm:算法

    String to Integer (atoi) 009 Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum ...

    leetcode双人赛-LeetCode:力扣笔记

    String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 ...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    String to Integer (atoi) 中等 9 Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest ...

    leetcode338-LeetCode:LeetCode刷题总结

    8.String to Integer (atoi) 9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3...

    leetcode跳跃-LeCode:乐科

    String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马...

    leetcode中国-leetcode:leetcode刷题

    String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular ...

    LeetCodeJSSolutions

    内存使用量:40 MB,少于JavaScript在线提交的String to Integer(atoi)的88.17%。 相关材料: #1710卡车解决方案上的最大单位: 运行时间:124毫秒,快于JavaScript在线提交的卡车上最大单位数的30.46%。 ...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    String to Integer (atoi) 18.5% 中等 9 Palindrome Number 56.7% 简单 10 Regular Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 ...String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy

    leetcode答案-leetcode:leetcode问题解决方案

    leetcode 答案 Leetcode题解 leetcode题库的答案及解决思路,随着解题的深入,题解会不断改进时间复杂度和空间复杂度,因此每个题包含多个算法。 目录 Algorithms(算法) ...String to Integer (atoi) Medium com.bc

    Flamg:火焰——青春的火焰在熊熊燃烧

    String to Integer (atoi)][15]][16] [![9. Palindrome Number][17]][18] [![10. Regular Expression Matching][19]][20] [![11. Container With Most Water][21]][22] [![12. Integer to Roman][23]][24] [![13. ...

    lrucacheleetcode-Algorithm:一些常见的算法的解题报告

    8.String to Integer (atoi) 9.Palindrome Number 11.Container With Most Water 14.Longest Common Prefix 15.3Sum 16.3Sum Closest 19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two ...

    leetcode卡-LeetCode:LeetCode题解

    Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- strcpy :star: :star: :star: 字符串复制,返回值,赋值语句 0028 strStr :star: :star: :star:...

Global site tag (gtag.js) - Google Analytics