Sword Points to Offer 06. Print the linked list from end to head || [Sword Points to Offer] 67. Convert strings into integers

Use the stack first-in-last-out idea, first stack, then pop out from the top of the stack, pop()


class Solution{

public:
    vector<int> reversePrint(ListNode* head){
        stack<int> stk;
        vector<int> v;


        while(head != NULL){

            stk.push(head -> val);//head指针指向的元素
            head = head -> next; //下一个指针


        }

        while(!stk.empty()){
            v.push_back(stk.top());
            stk.pop();

        }
        return v;



    }











};

Convert string to integer

class Solution {
public:
    int strToInt(string str) {
        str.erase(0, str.find_first_not_of(' '));
        int64_t ans = 0;
        int tag = 1;
        for(int ii = 0; ii < str.size(); ii++) {
            char ch = str[ii];
            if(ii == 0 && (ch == '-' || ch == '+')) {
                if(ch == '-') tag = -1;
            }
            else if(ch >= '0' && ch <= '9') ans = ans * 10 + (ch - '0');
            else break;
            if(ans > INT_MAX) break;
        }
        if(tag == 1 && ans >= INT_MAX) return INT_MAX;
        if(tag == -1 && -ans <= INT_MIN) return INT_MIN;
        return ans * tag;
    }
};

Guess you like

Origin blog.csdn.net/weixin_64043217/article/details/132620461