从零开始的CPP(21)——链表与字符串/向量的转换&字符串与整数的转换

leetcode2

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例 1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.

突发奇想想折腾成字符串/向量去做

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    string str1;
    string str2;
    //cout << "val:" << l1->val<<endl;
    str1 = List2Str(l1);
    reverse(str1.begin(), str1.end());
    //cout << "str1:" << str1<<endl;
    str2 = List2Str(l2);
    reverse(str2.begin(), str2.end());
    //cout << "str2:" << str2 << endl;
    long long num1 = stoll(str1);
    //cout << "num1:" << num1 << endl;
    long long num2 = stoll(str2);
    //cout << "num2:" << num2 << endl;
    long long sum;
    sum = num1 + num2;
    //cout<<"sum:" << sum<<endl;
    string str_sum;
    str_sum = to_string(sum);
    reverse(str_sum.begin(), str_sum.end());
    ListNode* head = Str2List(str_sum);
    return head;

}
string List2Str(ListNode* head) {
    string str;
    while (head != nullptr) {
        str.push_back(head->val+'0');
        head = head->next;
    }
    for (auto num : str) {
        cout << num << ' ';
    }
    cout << endl;
    return str;
}
ListNode* Str2List(string str) {
    int n = str[0] - '0';
    ListNode* head = new ListNode(n);
    ListNode* head1 = head;
    for (int i = 1; i < str.length(); i++) {
        head1->next = new ListNode(str[i]-'0');
        head1 = head1->next;
    }
    return head;
}
};

这是转换成字符串的做法,再用stoll把字符串变成数再相加。当然,因为long long也有长度限制,很多过长的测试用例是无法通过的。

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        vector<int> vec1;
        vector<int> vec2;
        vec1 = List2Vector(l1);

        vec2 = List2Vector(l2);

        vector<int> vec3;
        vec3 = AddVector(vec1,vec2);

        ListNode* head = new ListNode(vec3[0]);
        makeList(head, vec3);
        return head;

    }
    void add(vector<int>& vec, int a, int b,int &c) {
        int sum = a + b+c;
        if (sum >= 10) {
            vec.push_back(sum % 10);
            c = sum / 10;
        }
        if (sum < 10) {
            vec.push_back(sum);
            c = 0;
        }
    }
    vector<int> AddVector(vector<int> vec1, vector<int> vec2) {
        int length1 = vec1.size();
        int length2 = vec2.size();
        int length = min(length1, length2);
        int c = 0;
        vector<int> vec;
        for (int i = 0; i < length; i++) {
            add(vec, vec1[i], vec2[i],c);
        }
        if (length1 > length2) {
            for (int i = length; i < length1; i++) {
                add(vec, vec1[i], 0, c);
            }
        }
        if (length1 < length2) {
            for (int i = length; i < length2; i++) {
                add(vec, vec2[i], 0, c);
            }
        }
        if (c != 0) {
            vec.push_back(c);
        }
        return vec;
    }
    
    vector<int> List2Vector(ListNode* head) {
        vector<int> vec;       
        while (head != nullptr) {
            vec.push_back(head->val);
            head = head->next;
        }
        for (auto num : vec) {
            cout << num << ' ';
        }
        cout << endl;
        return vec;
    }
    void makeList(ListNode* head, vector<int> l) {

        for (int i = 1; i < l.size(); i++) {
            head->next = new ListNode(l[i]);
            head=head->next;
        }
    }
    void showList(ListNode* head) {
        ListNode* head1 = head;
        while (head1->next != nullptr) {
            cout << head1->val << "->";
            head1 = head1->next;
        }
        cout << head1->val << endl;
    }
    
};
int main()
{
    Solution solution;
    vector<int> l1 = { 9,9,9};
    vector<int> l2 = { 9,9,9,9,9,9,9,9,9 };
    ListNode* head1 = new ListNode(l1[0]);
    solution.makeList(head1, l1);
    cout << "l1:";
    solution.showList(head1);
    ListNode* head2 = new ListNode(l2[0]);
    solution.makeList(head2, l2);
    cout << "l2:";
    solution.showList(head2);
    ListNode* head3 = solution.addTwoNumbers(head1, head2);
    cout << "l3:";
    solution.showList(head3);
    return 0;
}

 这是转换成向量再对向量进行相加的做法,也不会受到长度的限制。

(两种做法都很没必要

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值