zoukankan      html  css  js  c++  java
  • leetcode-剑指67-OK

    // language C with STL(C++)
    // 剑指67
    // https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof/comments/
    // 同主站8题
    // https://leetcode-cn.com/problems/string-to-integer-atoi/
    
    
    class Solution {
    public:
        long power_10(long i){
            long res = 1;
            while(i>0){
                res*=10;
                i--;
            }
            return res;
        }
        int char2int(char c){
            if(c =='0')
                return 0;
            if(c =='1')
                return 1;
            if(c =='2')
                return 2;
            if(c =='3')
                return 3;
            if(c =='4')
                return 4;
            if(c =='5')
                return 5;
            if(c =='6')
                return 6;
            if(c =='7')
                return 7;
            if(c =='8')
                return 8;
            if(c =='9')
                return 9;
            return 10;  // 出错了
    
        }
        int strToInt(string str) {
            int firstNOTspace = 0;
            while(str[firstNOTspace] ==' ')
                firstNOTspace++;    //找到第一个非空格字符
            if(str[firstNOTspace] == '')
                return 0;
            bool minus = false;
            if(str[firstNOTspace]== '-'){
                minus = true;
                firstNOTspace++;
            }else if(str[firstNOTspace]== '+')    firstNOTspace++;
            while(str[firstNOTspace]== '0')
                firstNOTspace++;
            int FirstDigit = firstNOTspace;
            // printf("%d-", FirstDigit);
            while(char2int(str[firstNOTspace]) <10)
                firstNOTspace++;
            int LastDigit = firstNOTspace-1;
            // printf("%d", LastDigit);
            if(LastDigit<FirstDigit)
                return 0;
            long res=0;
            if(LastDigit-FirstDigit>= 10){
                if(minus)
                    return INT_MIN;
                else
                    return INT_MAX;
            }
            for(int i = LastDigit; i>=FirstDigit; i--){
                res += char2int(str[i]) * power_10(LastDigit-i);
            }
            if(minus)
                res *= (-1);
            if(res<= -2147483648)
                return INT_MIN;
            if(res>= 2147483648)
                return INT_MAX;
            return (int)res;
        }
    };
    
  • 相关阅读:
    wpf 不规则窗体
    wpf treeview使用expanded事件出错的问题
    获取文件图标
    C#操作快捷方式总结
    mysql 更改存储引擎,更改自增列计数值,更改默认字符集
    zend framework集成smarty
    文本文件数据导入mysql注意事项
    MYSQL 外键
    uchome 日志发布函数blog_post()
    mysql order by null
  • 原文地址:https://www.cnblogs.com/gallien/p/14379288.html
Copyright © 2011-2022 走看看