[LeetCode]#8 String to Integer (atoi)

GitHub : https://github.com/MummyDing/LeetCode

Source : https://leetcode.com/problems/string-to-integer-atoi/

Description :

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

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.


题解: 题目意思就是以字符串的形式给你一个整数,要你转换成整型返回,但是这个字符串中可能会有一些其他奇奇怪怪的符号.这在题目的开头已经提示了叫我们考虑多种情况,最好不要看提示,然后我就很听话的没有看提示 了,自然而然地wrong了好多发.根据一次次的错误数据也把它的转换规则搞清楚了,有点坑... 我的做法是首先将字符串进行一次预处理,将"真正的数字"部分抠出来,然后判断正负,看其是否超出整型范围,超出直接根据要求返回(题目中有说明,虽然我没有看,但是wrong了两发就知道了---囧--).最后如果没有超出范围那就很简单直接转换了. 其实这题用Java写也挺好,不过题目也并不复杂,就C++算了.最后运行时间是12ms,绝大部分的都是这样.


/*
Problem: String to Integer (atoi)
Description: https://leetcode.com/problems/string-to-integer-atoi/
Author: MummyDing
Date : 2015-12-29
Run Time: 12 ms
*/
#include <iostream>

using namespace std;

class Solution {
public:
    int myAtoi(string str) {
            //filter the number
            int len = str.size(),startX=-1,endX = -1;
            bool sign = true;
            for(int i=0 ; i<len ; i++){
                if(startX == -1){
                    if(str[i] == ' ') continue;
                    if(str[i] == '+' || str[i] == '-'  || str[i]>='0' && str[i]<='9'){
                        startX = i;
                    }else {
                        return 0;
                    }
                }else if((str[i]>='0' && str[i]<='9') == false){
                    endX = i;
                    break;
                }
                if(endX == -1 && len-1 == i){
                        endX = i+1;
                }
            }
            if(startX == -1 || endX == -1) return 0;
            //cout<<str.substr(startX,endX-startX)<<endl;
            if(str[startX] == '-'){
                sign = false;
                startX++;
            }else if(str[startX] == '+'){
                startX++;
            }
            string res = str.substr(startX,endX-startX);
            int lenR = res.size() , resNum = 0, pow = 1;

            // int  -2147483648 ~ 2147483647
            if(sign && (res>"2147483647" && lenR ==10 || lenR>10)){
                return 2147483647;
            }else if(sign ==false && res >"2147483648" && lenR ==10 || lenR>10){
               return  -2147483648;
            }

            // convert string to num
            for(int i = lenR-1 ; i>=0 ; i--){
                resNum += ((res[i]-'0')*pow);
                pow *=10;
            }
            if(sign) return resNum;
            else return -resNum;
    }
};
int main()
{
   // Solution s;
   // cout<<s.myAtoi(" 444-11228552307")<<endl;
    return 0;
}

【转载请注明出处】

Author: MummyDing

出处:http://blog.csdn.net/u012560612/article/category/6047025


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。所以各大互联网厂商都瞄准移动互联网这个潮流进行各大布局,经过多年的大浪淘沙,各种移动操作系统的不断面世,而目前市场占有率最高的就是微信小程序,本次开发一套基于微信小程序的生签到系统,有管理员,教师,学生三个角色。管理员功能有个人中心,学生管理,教师管理,签到管理,学生签到管理,班课信息管理,加入班课管理,请假信息管理,审批信息管理,销假信息管理,系统管理。教师和学生都可以在微信端注册和登录,教师可以管理签到信息,管理班课信息,审批请假信息,查看学生签到,查看加入班级,查看审批信息和销假信息。学生可以查看教师发布的学生签到信息,可以自己选择加入班课信息,添加请假信息,查看审批信息,进行销假操作。基于微信小程序的生签到系统服务端用Java开发的网站后台,接收并且处理微信小程序端传入的json数据,数据库用到了MySQL数据库作为数据的存储。这样就让用户用着方便快捷,都通过同一个后台进行业务处理,而后台又可以根据并发量做好部署,用硬件和软件进行协作,满足于数据的交互式处理,让用户的数据存储更安全,得到数据更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值