当前位置:网站首页>牛客网:大数加法
牛客网:大数加法
2022-06-21 16:02:00 【lsgoose】

本质上就是算术的一次模拟:
我们在计算的时候首先要对齐最低位,然后再逐位相加,相加过程每次只取一位相加,记录这一次的进位,加到下一次里边去。
代码如下所示:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算两个数之和
* @param s string字符串 表示第一个整数
* @param t string字符串 表示第二个整数
* @return string字符串
*/
string solve(string s, string t) {
if(s=="") return t;
if(t=="") return s;
int carry=0;
//保证前面字符串长度长
if(s.length()<t.length()){
swap(s, t);
}
for(int i=s.length()-1;i>=0;i--){
int sum=s[i]-'0'+carry;
//求出对应位在较短字符串t中的位置
int j=(t.length()-s.length())+i;
if(j>=0){
sum+=t[j]-'0';
}
carry=sum/10;
sum=sum%10;
s[i]='0'+sum;
}
if(carry){
s='1'+s;
}
return s;
}
};边栏推荐
- 海外new things | 美国人工智能初创「Zoovu」新一轮融资1.69亿美元,为消费者优化线上的“产品发现”体验
- 【SQLite】解决unrecognized token:“‘“
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers
- About product operation strategy
- 碳排放的计算
- 云原生监控系统·夜莺近期新功能一览,解决多个生产痛点
- I do 3D restoration for the aircraft carrier: these three details are shocking
- 微信小程序开发入门介绍-布局组件
- Come and watch – tpt18 new report
猜你喜欢

很多软件公司,其实都是“笑话”

新增Razor组件支持代理连接RDP,JumpServer堡垒机v2.23.0发布
![[ROS2 基础] Navigation2 导航系统介绍](/img/7f/ad6af972460d7a78fb28d9b4c24477.png)
[ROS2 基础] Navigation2 导航系统介绍

Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022

网购网站(期末大作业)

PowerPoint 教程,如何在 PowerPoint 中更改页面方向、幻灯片大小?

Oracle中实现获取指定行内容——Rownum和Row_number()

Overseas new things | software developer "dynaboard" seed round raised US $6.6 million to develop low code platform to connect design, products and developers

使用 Guzzle 中间件进行优雅的请求重试

Come and watch – tpt18 new report
随机推荐
Notice on printing and distributing the Interim Measures of Beijing Municipality for the administration of housing with common property rights
三色标记清除法
In 2021 database market, aerospike competes with top manufacturers
Elegant request retry using guzzle Middleware
Fidder工具使用笔记
Unittest框架的测试日志
Pytest--生成测试报告
Kotlin 中list set map
How to write test cases
Huawei (13) - route introduction
rtmp webrtc 协议 openssl 等安装
云原生监控系统·夜莺近期新功能一览,解决多个生产痛点
The Google play academy team PK competition officially begins!
qtcreator報錯解决
[mathematical modeling] Matlab Application Practice Series (95) - application cases of time series prediction (with matlab code)
Yaml file details
7 tips for writing effective help documents
Recruitment order | data visualization development platform "flyfish" and "super experience officer" are recruited!
Ares Ares I pledged LP mining crowdfunding model DAPP smart contract customization
集成底座方案演示说明