当前位置:网站首页>offer收割机:两个长字符串数字相加求和(经典面试算法题)
offer收割机:两个长字符串数字相加求和(经典面试算法题)
2022-07-07 05:02:00 【不凡~】
在面试中,为了缓解面试的尴尬,有些面试会然你写一写简单算法题来缓解气氛比如冒泡啊简单的算法题,今天给大家带了一道我遇到的一道算法题分享给他就,思路都写在对应代码的注释上,不懂可以看一下,有什么写的不对的大家可以指出来上代码
package com.interview;
import com.sun.javafx.image.BytePixelSetter;
public class Test2 {
public static void main(String[] args) {
String str1 = "568658123123312123123123123123131233132123123123131231233123131231231231231";
String str2 = "132121231231231312312312312312312312312312312312313123123101";
//reverse()方法是将字符串反转。
//StringBuffer类是一个可变字符串类,它可以用来存储字符串。
StringBuffer s1 = new StringBuffer(str1).reverse();
StringBuffer s2 = new StringBuffer(str2).reverse();
StringBuffer res = new StringBuffer();
int len1 = s1.length();
int len2 = s2.length();
int len;
//如果两个字符串长度不一样,则取长度较大的那个字符串的长度。
if (len1 < len2) {
//意思是:如果len1<len2,则len=len2,否则len=len1。
len = len2;
//用len2-len1的值来填充len1的空位。
int count = len2 - len1;
//将长度较小的字符串补全为长度较大的字符串的长度。
while (count-- > 0)
//对那个空位进行填充。追加
s1.append("0");
} else {
//如果两个字符串长度一样,则取长度较大的那个字符串的长度。
len = len1;
//用len1-len2的值来填充len2的空位。
int count = len1 - len2;
//将长度较小的字符串补全为长度较大的字符串的长度。
while (count-- > 0)
s2.append("0");
}
int overflow = 0;
int num;
//遍历两个字符串的每一个字符,将两个字符相加。
for (int i = 0; i < len; i++) {
//charAt()方法是返回指定索引处的字符。
//num是字符串的ASCII码值。
num = s1.charAt(i) - '0' + s2.charAt(i) - '0' + overflow;
//如果相加后的数字大于9,则将相加后的数字减去10,并将overflow设置为1。
if (num >= 10) {
//将相加后的数字减去10。
overflow = 1;
//将相加后的数字转换为字符。
num = num - 10;
} else {
//如果相加后的数字小于10,则将overflow设置为0。
overflow = 0;
}
//将相加后的数字转换为字符。
res.append(String.valueOf(num));
}
//如果相加后的数字大于9,则将相加后的数字减去10,并将overflow设置为1。
if (overflow == 1)
//将相加后的数字转换为字符。
res.append("1");
//将结果转换为字符串。
System.out.println(res.reverse().toString());
}
}
这是主要思路是通过反转来求和
边栏推荐
- Lattice coloring - matrix fast power optimized shape pressure DP
- [UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
- json 数据展平pd.json_normalize
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- Linux server development, MySQL index principle and optimization
- 青龙面板-今日头条
- Linux Installation MySQL 8.0 configuration
- [Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
- 【踩坑系列】uniapp之h5 跨域的问题
猜你喜欢

Linux server development, detailed explanation of redis related commands and their principles

QT learning 26 integrated example of layout management

快解析内网穿透助力外贸管理行业应对多种挑战

Yugu p1020 missile interception (binary search)

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

LeetCode 40:组合总和 II

Leetcode 40: combined sum II

LeetCode简单题之字符串中最大的 3 位相同数字

互动送书-《Oracle DBA工作笔记》签名版

2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
随机推荐
Cnopendata geographical distribution data of religious places in China
Detailed explanation of Kalman filter for motion state estimation
Jmeter 的使用
Pytorch(六) —— 模型调优tricks
芯片 设计资料下载
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
buureservewp(2)
Linux server development, MySQL stored procedures, functions and triggers
Ansible
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
互动送书-《Oracle DBA工作笔记》签名版
Chip information website Yite Chuangxin
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
【踩坑系列】uniapp之h5 跨域的问题
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Quickly use Jacobo code coverage statistics
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
Yugu p1020 missile interception (binary search)
C语言航班订票系统
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!