当前位置:网站首页>NC1 大数加法
NC1 大数加法
2022-08-06 10:49:00 【皓瑞 =)】
NC1 大数加法

class Solution {
public:
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */
string solve(string s, string t) {
auto preprocess = [](string& s, string& t) -> void
{
// 解决字符串为空的情况
if (s.empty()) s = "0";
if (t.empty()) t = "0";
// 使 s 的长度恒长为 t
if (s.size() < t.size())
{
string tmp;
tmp.swap(s);
tmp.swap(t);
s.swap(tmp);
}
// 预留多一位,以防数组越界
s = "0" + s;
};
auto process = [](string& s, string& t)
{
constexpr uint32_t kCharZero = '0';
constexpr uint32_t kCharTen = '9' + 1;
string& res = s;
// s + t
size_t maxLoopTime = t.size(), sSize = s.size(), tSize = t.size();
for (int i = 0; i<maxLoopTime; i++)
{
res[sSize - i -1] = s[sSize - i -1] + t[tSize -i -1] - kCharZero;
// 进位
if (res[sSize - i -1] >= kCharTen)
{
res[sSize - i -1] -= 10;
res[sSize - i -2] += 1;
}
}
// s 进位
maxLoopTime = s.size() - 1;
for (int i =0; i<maxLoopTime; i++)
{
if (res[sSize - i -1] >= kCharTen)
{
res[sSize - i -1] -= 10;
res[sSize - i -2] += 1;
}
}
};
auto postprocess = [](string& s)
{
if (!s.empty() && s[0]=='0')
{
s = s.substr(1, s.size()-1);
}
};
preprocess(s, t);
process(s, t);
postprocess(s);
return s;
}
};
边栏推荐
- LeetCode-622. 设计循环队列
- Hexagon_V65_Programmers_Reference_Manual(17)
- 阿里云ECS搭建Kubernetes集群踩坑记
- HCIP第十五天笔记
- Fluid -1- Theme Installation
- 【js基础】js实现订阅者-发布者模式
- 为Kubernetes dashboard访问用户添加权限控制
- UE5 使用Mesh Editor 查看骨骼相对于root的坐标系
- Notion Chinese: client, web-side localization solution
- Synchronous modification when fastadmin searches
猜你喜欢

How to use wireless serial communication module to realize long-distance communication between touch screen and PLC?

How Kubernetes schedules and manages CPU

HCIP——VLAN实验

HCIP第十五天笔记

数据治理(一):为什么要数据治理

【 machine learning bayesian classifier

Small program product list paging implementation

【RTOS训练营】定时器的机制、源码分析和晚课提问

ABP详细教程——模块类

三面(技术+HR面试)网易,分享我的面试经验!(已拿offer)
随机推荐
浏览器的缓存机制 强制缓存 && 协商缓存
手把手教你写一个通用的helm chart
TCP/UDP协议抓包-工具wireshark与tcp侦听工具
【js基础】js实现订阅者-发布者模式
Kubernetes 部署策略
Kubernetes PV在Retain策略Released状态下重新分配到PVC恢复数据
【 machine learning bayesian classifier
【C# 学习笔记 ①】C# 程序结构和基本语法(VSCode工具配置、数据类型、修饰符、构造函数)
接口的安全设计要素有哪些?
HCIP第十五天笔记
【剑指offer】JZ79 判断是不是平衡二叉树
el-table 列内容溢出 显示省略号 悬浮显示文字
二维数组的各种翻转
kubernetes flannel 切换 calico 后节点 pod 无法通信
Mini Program Commodity List: Commodity Collection Implementation
Su Qiugui: The Secret of Google's Subdivision Deployment of Foreign Trade Enterprises
《ABP Framework 极速开发》 - 教程首发
HCIP第十四天笔记
Hexagon_V65_Programmers_Reference_Manual(19)
WebRTC Directory Structure