当前位置:网站首页>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
边栏推荐
- Blob 對象介紹
- Zsh shell adds automatic completion and syntax highlighting
- Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
- 电池、电机技术受到很大关注,反而电控技术却很少被提及?
- 复杂网络建模(一)
- ZCMU--1492: Problem D(C语言)
- 藏书馆App基于Rainbond实现云原生DevOps的实践
- Linux server development, MySQL transaction principle analysis
- Recursive method to verify whether a tree is a binary search tree (BST)
- Chip design data download
猜你喜欢
buureservewp(2)
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Réplication de vulnérabilité - désrialisation fastjson
在Rainbond中一键部署高可用 EMQX 集群
JS复制图片到剪切板 读取剪切板
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
Dedecms collects content without writing rules
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Uniapp mobile terminal forced update function
随机推荐
Excel import function of jeesite form page
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Empire CMS collection Empire template program general
Find the mode in the binary search tree (use medium order traversal as an ordered array)
Recursive method to verify whether a tree is a binary search tree (BST)
C language queue
芯片 設計資料下載
[step on the pit series] H5 cross domain problem of uniapp
在 Rainbond 中一键安装高可用 Nacos 集群
在Rainbond中一键部署高可用 EMQX 集群
Implementation of replacement function of shell script
Leetcode 187 Repeated DNA sequence (2022.07.06)
Game attack and defense world reverse
Myabtis_ Plus
Li Kou interview question 04.01 Path between nodes
快解析内网穿透助力外贸管理行业应对多种挑战
Summary of redis functions
Zsh shell adds automatic completion and syntax highlighting
Network learning (II) -- Introduction to socket