当前位置: 首页 > news >正文

笔试强训(六)

一.大数加法

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

class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** 计算两个数之和* @param s string字符串 表示第一个整数* @param t string字符串 表示第二个整数* @return string字符串*/string solve(string s, string t) {// write code herestring 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

/*** 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 herehead1 = 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

class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param s string字符串 第一个整数* @param t string字符串 第二个整数* @return string字符串*/string solve(string s, string t) {// write code herereverse(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;}
};

http://www.dtcms.com/a/490986.html

相关文章:

  • Iterator迭代器 【ES6】
  • spring boot实现接口数据脱敏,整合jackson实现敏感信息隐藏脱敏
  • 基于单片机的汽车多参数安全检测与报警系统设计
  • C++设计模式_行为型模式_备忘录模式Memento
  • 温州h5建站关于网站建设的文章
  • 大连专业做网站wordpress 4.5 汉化主题
  • Spring Boot 3零基础教程,Spring Boot 日志分组,笔记20
  • 【单调向量 单调栈】3676. 碗子数组的数目|1848
  • 【JUnit实战3_01】第一章:JUnit 起步
  • 公司门户网站该怎么做用模块做网站
  • 合肥网站定制公司宁波做网站公司哪家好
  • Banana Script,一个C99实现的,类JavaScript极简语法的脚本引擎
  • 14-机器学习与大模型开发数学教程-第1章 1-6 费马定理与极值判定
  • 写的网站怎么做接口php在网站上怎么做充值
  • nginx报400bad request 请求头过大异常处理
  • react+springboot云上部署
  • Google 地图类型
  • 免费网站做企业的网站都要准备什么
  • 网站建设往年的高考题免费看电视的网站有哪些
  • STM32N6 KEIL IDE 调试XIP 应用的一种方法 LAT1575
  • 大模型微调(二):使微调保持稳定的策略
  • 前端调优23大规则(Part 4)
  • SpringBoot-入门介绍
  • 如何推动AI技术在企业管理中的商业化落地?
  • 淘宝网站建设的策划书产品软文案例
  • 复制带随机指针的链表
  • Promise 与 async/await
  • win11 字体变宽问题
  • 最好的做网站机械加工网站色彩搭配
  • Pytorch Yolov11目标检测+Android部署 留贴记录