笔试强训(六)

一.大数加法

https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475?tpId=196&tqId=37176&ru=/exam/oj

cpp 复制代码
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    string solve(string s, string t) {
        // write code here
        string ret;
        int i = s.size() - 1, j = t.size() - 1;
        int tmp = 0;
        while (i >= 0 || j >= 0 || tmp)
        {
            if (i >= 0)
            {
                tmp += s[i--] - '0';
            }
            if (j >= 0)
            {
                tmp += t[j--] - '0';
            }
            ret += tmp % 10 + '0';
            tmp /= 10;
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};

二.链表相加(二)

https://www.nowcoder.com/practice/c56f6c70fb3f4849bc56e33ff2a50b6b?tpId=196&tqId=37147&ru=/exam/oj

cpp 复制代码
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    ListNode* reverse(ListNode* head)
    {
        ListNode* newhead = new ListNode(0);
        ListNode* cur = head;
        while (cur)
        {
            ListNode* next = cur->next;
            cur->next = newhead->next;
            newhead->next = cur;
            cur = next;
        }
        cur = newhead->next;
        delete newhead;
        return cur;
    }


    ListNode* addInList(ListNode* head1, ListNode* head2) {
        // write code here
        head1 = reverse(head1);
        head2 = reverse(head2);
        int t = 0;
        ListNode* cur1 = head1;
        ListNode* cur2 = head2;
        ListNode* ret = new ListNode(0);
        ListNode* prev = ret;
        while (cur1 || cur2 || t)
        {
            if (cur1)
            {
                t += cur1->val;
                cur1 = cur1->next;
            }
            if (cur2)
            {
                t += cur2->val;
                cur2 = cur2->next;
            }
            prev = prev->next = new ListNode(t % 10);
            t /= 10;
        }
        cur1 = ret->next;
        ret->next = nullptr;
        delete ret;
        return reverse(cur1);
    }
};

三.大数相乘

https://www.nowcoder.com/practice/c4c488d4d40d4c4e9824c3650f7d5571?tpId=196&tqId=37177&ru=/exam/oj

cpp 复制代码
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 第一个整数
     * @param t string字符串 第二个整数
     * @return string字符串
     */
    string solve(string s, string t) {
        // write code here
        reverse(s.begin(), s.end());
        reverse(t.begin(), t.end());
        int m = s.size(), n = t.size();

        vector<int> tmp(m + n);
        // 1. 无进位相乘相加
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                tmp[i + j] += (s[i] - '0') * (t[j] - '0');
            }
        }

        // 2. 处理进位
        int c = 0;
        string ret;
        for(auto x : tmp)
        {
            c += x;
            ret += c % 10 + '0';
            c /= 10;
        }
        while(c)
        {
            ret += c % 10 + '0';
            c /= 10;
        }

        // 3. 处理前导零
        while(ret.size() > 1 && ret.back() == '0') ret.pop_back();

        reverse(ret.begin(), ret.end());
        return ret;
    }
};
相关推荐
王璐WL2 分钟前
【数据结构】单链表的经典算法题
数据结构·算法
m0_495562785 分钟前
Swift-Enum
java·算法·swift
青山的青衫8 分钟前
【前后缀】Leetcode hot 100
java·算法·leetcode
Zzzzmo_21 分钟前
Java数据结构:二叉树
java·数据结构·算法
CoovallyAIHub27 分钟前
结构化数据迎来“ChatGPT时刻”!LimitX:一个模型统一所有表格任务
深度学习·算法·计算机视觉
普普通通的南瓜1 小时前
网站提示 “不安全”?免费 SSL 证书一键解决
网络·数据库·网络协议·算法·安全·iphone·ssl
聆风吟º1 小时前
【数据结构入门手札】数据结构基础:从数据到抽象数据类型
数据结构·数据类型·逻辑结构·数据对象·物理结构·数据项·数据元素
啊吧怪不啊吧1 小时前
二分查找算法介绍及使用
数据结构·算法·leetcode
知识搬运工人1 小时前
对比 DeepSeek(MLA)、Qwen 和 Llama 系列大模型在 Attention 架构/算法层面的核心设计及理解它们的本质区别。
算法
蒋星熠2 小时前
全栈开发实战指南:从架构设计到部署运维
运维·c++·python·系统架构·node.js·devops·c5全栈