- 浏览: 18979 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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。
(2)String中的subString(int beginIndex, int endIndex)的方法中endIndex参数并不包括在内,也就是说是从beginIndex到endIndex-1。
(3)String中compareTo(String anotherString)中的比较规则是不管长度大小,总是从第一个字符开始比较,如果相同则返回0,大于或者小于返回两个的差值(一个大于0,一个小于0)。
自己的代码
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)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
Implement strStr()
2014-10-28 15:17 281题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 417题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 299题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 345题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 320题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 343题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 354题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 305题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 254题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 291题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 230题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 254题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 315题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 333题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
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 ...
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 ...
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字符串转换为整数
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 ...
(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 ...
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 ...
String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 ...
Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...
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 ...
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...
String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马...
String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular ...
内存使用量:40 MB,少于JavaScript在线提交的String to Integer(atoi)的88.17%。 相关材料: #1710卡车解决方案上的最大单位: 运行时间:124毫秒,快于JavaScript在线提交的卡车上最大单位数的30.46%。 ...
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 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 ...String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy
leetcode 答案 Leetcode题解 leetcode题库的答案及解决思路,随着解题的深入,题解会不断改进时间复杂度和空间复杂度,因此每个题包含多个算法。 目录 Algorithms(算法) ...String to Integer (atoi) Medium com.bc
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. ...
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 ...
Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- strcpy :star: :star: :star: 字符串复制,返回值,赋值语句 0028 strStr :star: :star: :star:...