Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42917
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 224
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: Java

2015-01-19 21:22:51

题目:

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.
解决思路:题目本身并不难,难在判断输入的合法性以及转换后的overflow问题,OJ本身给出了以下几个提示:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

首先是字符开始的空白字符的处理,需要找到第一个非空白字符(本人并未考虑到这一情况),然后是一个非空白字符的处理,分为四种情况:+、-、数字、其他字符,再然后就是overflow的问题。这一个题目主要考察的是考虑问题的全面性,在程序员面试宝典中有类似题目的C解法。这里给出JAVA解法:

点击(此处)折叠或打开

  1. public class Solution {
  2.     public int atoi(String str) {
  3.         if(str.length() == 0){
  4.             return 0;
  5.         }
  6.         int sum = 0;
  7.         char[] myarr = str.toCharArray();
  8.         int i = 0;
  9.         int symbol = 1;
  10.         while(myarr[i] == ' '){
  11.             i++;
  12.         }
  13.         int j = i;
  14.         if(myarr[j] == '-'){
  15.             symbol = -1;
  16.             j++;
  17.         }
  18.         else if(myarr[j] == '+'){
  19.             j++;
  20.         }
  21.         //System.out.println(j);
  22.         for(int n = j;n < myarr.length; n++){
  23.             //System.out.println(Character.getNumericValue(myarr[n]));
  24.             if(Character.getNumericValue(myarr[n]) >= 0 && Character.getNumericValue(myarr[n]) <= 9){
  25.                 if(sum == Integer.MAX_VALUE || sum == Integer.MIN_VALUE){
  26.                     continue;
  27.                 }
  28.                 else if(Math.abs(sum) > Integer.MAX_VALUE/10){
  29.                     if(symbol == 1){
  30.                         sum = Integer.MAX_VALUE;
  31.                     }
  32.                     else{
  33.                         sum = Integer.MIN_VALUE;
  34.                     }
  35.                 }
  36.                 else if(Math.abs(sum) == Integer.MAX_VALUE/10){
  37.                     if(symbol == 1){
  38.                         if(Character.getNumericValue(myarr[n]) > Integer.MAX_VALUE%10){
  39.                             sum = Integer.MAX_VALUE;
  40.                         }
  41.                         else{
  42.                             sum = sum*10 + Character.getNumericValue(myarr[n]);
  43.                         }
  44.                     }
  45.                     else{
  46.                         if(Character.getNumericValue(myarr[n]) > Math.abs(Integer.MIN_VALUE%10)){
  47.                             sum = Integer.MIN_VALUE;
  48.                         }
  49.                         else{
  50.                             sum = -(Math.abs(sum)*10 + Character.getNumericValue(myarr[n]));
  51.                         }
  52.                     }
  53.                 }
  54.                 else{
  55.                     if(symbol == 1){
  56.                         sum = sum*10 + Character.getNumericValue(myarr[n]);
  57.                     }
  58.                     else{
  59.                         sum = -(Math.abs(sum)*10 + Character.getNumericValue(myarr[n]));
  60.                     }
  61.                 }
  62.             }
  63.             else{
  64.                 return sum;
  65.             }
  66.         }
  67.         return sum;
  68.     }
  69. }
PS:该题在LeetCode中分级为easy但是acceptance只有13.7%,排在179题中倒数第5,由此可见思维的全面性的重要性                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            


阅读(424) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~