当前位置:网站首页>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
边栏推荐
猜你喜欢

Basic use of CTF web shrink template injection nmap

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO

数据库实时同步利器——CDC(变化数据捕获技术)

What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit

Real time monitoring of dog walking and rope pulling AI recognition helps smart city

Complex network modeling (I)

Fast parsing intranet penetration escorts the document encryption industry

Main window in QT learning 27 application

jeeSite 表单页面的Excel 导入功能

央视太暖心了,手把手教你写HR最喜欢的简历
随机推荐
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
Network learning (II) -- Introduction to socket
让Livelink初始Pose与动捕演员一致
Lattice coloring - matrix fast power optimized shape pressure DP
海信电视开启开发者模式
Zsh shell adds automatic completion and syntax highlighting
Complex network modeling (III)
game攻防世界逆向
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
拓维信息使用 Rainbond 的云原生落地实践
[step on the pit series] H5 cross domain problem of uniapp
Content of string
Avatary的LiveDriver试用体验
Register of assembly language by Wang Shuang
一文了解如何源码编译Rainbond基础组件
Recursive method to verify whether a tree is a binary search tree (BST)
Real time monitoring of dog walking and rope pulling AI recognition helps smart city
Network learning (III) -- highly concurrent socket programming (epoll)
eBPF Cilium实战(2) - 底层网络可观测性
Bayes' law