leetcode_3.23

1.给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。
列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。
https://leetcode-cn.com/problems/flatten-nested-list-iterator/
重点在于根本没有看懂题目啊!!!!题目理解:相当于一个多维的列表需要展开成为一维的列表,可以在遍历前先进行展开, 当然也可以一边找一边输出
思路如下:每一个数相当于一个叶子,每一个列表相当于一个结点,,因此使用到栈进行深度优先遍历,首先逆序入栈,在输出时按序弹出,若该数是数,则用next进行输出,若是列表则再次将该列表逆序入栈,再次调用判断函数(因为可能列表是空的!!!!),若非空则调用next输出,为空时返回false。
代码:

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */

class NestedIterator {
public:
stack<NestedInteger> st;
    NestedIterator(vector<NestedInteger> &nestedList) {
        for(int i=nestedList.size()-1;i>=0;i--){
            st.push(nestedList[i]);
        }
    }
    
    int next() {
        NestedInteger a=st.top();
        st.pop();
        return a.getInteger();
    }
    
    bool hasNext() {
        if(!st.empty()){//不空则还有,返回对
            if(st.top().isInteger()){
                return true;
            }else{//不是数展开放回,在次判断
                NestedInteger a=st.top();
                st.pop();
                vector<NestedInteger> nl=a.getList();
                for(int i=nl.size()-1;i>=0;i--){
                    st.push(nl[i]);
                }
                return hasNext();
            }
        }else{
            return false;
        }
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

2.请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
函数 myAtoi(string s) 的算法如下:
读入字符串并丢弃无用的前导空格
检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
将前面步骤读入的这些数字转换为整数(即,“123” -> 123, “0032” -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
返回整数作为最终结果。
注意:
本题中的空白字符只包括空格字符 ’ ’ 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
https://leetcode-cn.com/problems/string-to-integer-atoi/
转化应该挺简单的,if判断即可 (事实打脸了,注意"±1";“00000-42a1234”;" -0 123",应该输出0啊!因为不符合数字的形式了,因此想用if记得用一位标志位表示数字的开始,以保证数字格式的正确),重点在于如何检查是否越界,我是采用的longlong记录然后比较…(太会钻空子了!但是注意longlong也会超范围,所以应该在乘的时候就检测,并且pow(2,31)*flag会越界!直接返回最省事)
代码:

class Solution {
public:
    int myAtoi(string s) {
        int flag=1;
        int tag=0;//因为没有用正则式,因此用标志位表示数字开始了
        long long result=0;
        for(int i=0;i<s.length();i++){
            if(s[i]==' '){
                if(tag==1){
                    break;
                }
                //跳过
            }else if(s[i]=='-'){//符号
                if(tag==1){
                    break;
                }
                tag=1;
                flag=-1;
            }else if(s[i]=='+'){
                if(tag==1){
                    break;
                }
                tag=1;
                //跳过
            }else if(s[i]-'0'<0||s[i]-'0'>9){
                break;
            }else{//是数字了
                tag=1;
                result=result*10+(s[i]-'0');
                if(flag==1){
                    if(result>=2147483647){
                        result=2147483647;//超范围了直接返回
                        return 2147483647;
                    }
                }else{
                    if(result>=2147483648){
                        result=2147483648;//超范围了直接返回
                        return -2147483648;
                    }
                }
            }
        }
        return (int)result*flag;
    }
};

官方思路处理溢出:
本题可能产生溢出的步骤在于推入,乘 10 操作和累加操作都可能造成溢出。对于溢出的处理方式通常可以转换为 INT_MAX 的逆操作。比如判断某数乘 10 是否会溢出,那么就把该数和 INT_MAX 除 10 进行比较。

官方思路:自动机
在此反思一下:我一看到题也想到了自动机,但是太自信且懒得画,直接上手敲。。。非常悲惨。。。

看到的更合适的方法:不想画自动机那就不要用for循环~~太机智了!而且很细节‘s[i]’可能直接超范围,所以注意先算-‘0’后的值哦!
代码:

class Solution {
public:
    int myAtoi(string s) {
        int sign = 1, tmp = 0, i = 0;

        while(s[i] == ' ')  ++i;    //1.忽略前导空格

        if(s[i] == '+' || s[i] == '-')    //2.确定正负号
            sign = 1 - 2 * (s[i++] == '-');    //s[i]为+的话sign依旧为1,为-的话sign为-1

        while(s[i] >= '0' && s[i] <= '9')   //3.检查输入是否合法
        {
            if(tmp > INT_MAX / 10 || (tmp == INT_MAX / 10 && s[i] - '0' > 7))    //4.是否溢出
                return sign == 1 ? INT_MAX : INT_MIN;
            tmp = tmp * 10 + (s[i++] - '0');    //不加括号有溢出风险
        }
        return tmp * sign;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值