当前位置:网站首页>Offer harvester: add and sum two long string numbers (classic interview algorithm question)
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
2022-07-07 08:12:00 【Extraordinary~】
In the interview , To ease the embarrassment of the interview , In some interviews, you will write simple algorithm problems to ease the atmosphere, such as bubbling, simple algorithm problems , Today, I bring you an algorithm problem I encountered, and share it with him , Ideas are written in the comments of the corresponding code , If you don't understand, you can have a look , You can point out what is wrong with the code
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() The method is to reverse the string .
//StringBuffer Class is a variable string class , It can be used to store strings .
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 two strings are not the same length , Then take the length of the larger string .
if (len1 < len2) {
// intend : If len1<len2, be len=len2, otherwise len=len1.
len = len2;
// use len2-len1 To fill in len1 The vacancy of .
int count = len2 - len1;
// Complete the shorter string with the longer string .
while (count-- > 0)
// Fill that vacancy . Additional
s1.append("0");
} else {
// If two strings have the same length , Then take the length of the larger string .
len = len1;
// use len1-len2 To fill in len2 The vacancy of .
int count = len1 - len2;
// Complete the shorter string with the longer string .
while (count-- > 0)
s2.append("0");
}
int overflow = 0;
int num;
// Traverse each character of two strings , Add two characters .
for (int i = 0; i < len; i++) {
//charAt() The method is to return the character at the specified index .
//num It's string ASCII Code value .
num = s1.charAt(i) - '0' + s2.charAt(i) - '0' + overflow;
// If the added number is greater than 9, Then subtract 10, And will overflow Set to 1.
if (num >= 10) {
// Subtract... From the added number 10.
overflow = 1;
// Convert the added numbers into characters .
num = num - 10;
} else {
// If the added number is less than 10, Will overflow Set to 0.
overflow = 0;
}
// Convert the added numbers into characters .
res.append(String.valueOf(num));
}
// If the added number is greater than 9, Then subtract 10, And will overflow Set to 1.
if (overflow == 1)
// Convert the added numbers into characters .
res.append("1");
// Convert the result to a string .
System.out.println(res.reverse().toString());
}
}
This is the main idea, which is to sum through inversion
边栏推荐
- Vulnerability recurrence fastjson deserialization
- 船载雷达天线滑环的使用
- ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
- Téléchargement des données de conception des puces
- buureservewp(2)
- Leetcode 187 Repeated DNA sequence (2022.07.06)
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- Real time monitoring of dog walking and rope pulling AI recognition helps smart city
- Linux server development, MySQL transaction principle analysis
- Pytorch(六) —— 模型调优tricks
猜你喜欢
Avatary's livedriver trial experience
buureservewp(2)
互动送书-《Oracle DBA工作笔记》签名版
Jmeter 的使用
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
QT learning 26 integrated example of layout management
Force buckle 145 Binary Tree Postorder Traversal
数据库实时同步利器——CDC(变化数据捕获技术)
LeetCode简单题之判断一个数的数字计数是否等于数位的值
利用 Helm 在各类 Kubernetes 中安装 Rainbond
随机推荐
贝叶斯定律
Fast parsing intranet penetration escorts the document encryption industry
Avatary的LiveDriver试用体验
Complex network modeling (III)
Chip design data download
Complete linear regression manually based on pytoch framework
Game attack and defense world reverse
Padavan manually installs PHP
Codeforce c.strange test and acwing
Vulnerability recurrence fastjson deserialization
Thinkcmf6.0 installation tutorial
Linux Installation MySQL 8.0 configuration
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
Linux server development, MySQL transaction principle analysis
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
拓维信息使用 Rainbond 的云原生落地实践
复杂网络建模(三)
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Content of string