当前位置:网站首页>Niuke daily question -day1
Niuke daily question -day1
2022-06-12 05:37:00 【Violence produces miracles】
Add big numbers
Title Description
Read in two numbers as a string , Write a function to calculate their sum , Returns... As a string .
( The string length is not greater than 100000, Ensure that the string consists only of ’0’~'9’ this 10 Characters make up )
Example 1
Input
“1”,“99”
Return value
“100”
explain
1+99=100
type , simulation , difficulty , samsung , Time consuming 20 minute
My solution : Use three stacks + A little simulation thinking
import java.util.*;
public class Solution {
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * Calculate the sum of two numbers * @param s string character string Represents the first integer * @param t string character string Represents the second integer * @return string character string */
public String solve (String s, String t) {
// write code here
Stack<Integer> stack1 = new Stack();
Stack<Integer> stack2 = new Stack();
if(s.length()<t.length()){
String temp =s;
s=t;
t=temp;
}
for(int i=0;i<s.length();i++){
stack1.push(s.charAt(i)-'0');
}
for(int i=0;i<t.length();i++){
stack2.push(t.charAt(i)-'0');
}
int te=0;
Stack<Integer> res = new Stack();
while(!stack1.isEmpty()&&!stack2.isEmpty()){
int a=stack1.pop();
int b=stack2.pop();
int c=a+b+te;
if(c>=10){
te=c/10;
c=c%10;
}else{
te=0;
}
res.push(c);
}
while(!stack1.isEmpty()){
int a=stack1.pop();
int b=te;
int c=a+b;
if(c>=10){
te=c/10;
c=c%10;
}else{
te=0;
}
res.push(c);
}
if(te>0){
res.push(te);
}
StringBuilder str = new StringBuilder();
while(!res.isEmpty()){
str.append(String.valueOf(res.pop()));
}
return str.toString();
}
}
边栏推荐
- Simple introduction to key Wizard
- 19. regular expression matching
- XML parameter schema, the same MTK SW version is compatible with two different sets of audio parameters
- MySQL Linux Installation mysql-5.7.24
- Webrtc AEC process analysis
- 【长时间序列预测】Aotoformer 代码详解之[4]自相关机制
- 62. the last number left in the circle
- Introduction to Internet Protocol
- Details of FPGA syntax
- 59 - I. maximum value of sliding window
猜你喜欢

38. 外观数列

分公司负责人需要承担的法律责任

Esp32-who face detection

Halcon 3D 1 读取3d数据

FPGA语法的细节
![[gin] gin framework for golang web development](/img/15/68c4fd217555f940b3cd3d10fcd54f.jpg)
[gin] gin framework for golang web development

Automated test - dark horse headline test project

How Wireshark decrypts WiFi data packets

Matlab: halftone and dither conversion

The most commonly used objective weighting method -- entropy weight method
随机推荐
Introduction to Internet Protocol
17. print from 1 to the maximum n digits
Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ not found; SQL statement:
Individual application for ov type SSL certificate
Go interface oriented programming practice
Lldp protocol
[gpio] how to modify / display GPIO status through ADB shell
Kubernetes certificate online update
How long is the company's registered capital subscribed
UBI details and JFFS2 square FS UBIFS
Is the individual industrial and commercial door a legal person enterprise
flex/fixed上中下(移動端)
GRE protocol details
Halcon 用点来拟合平面
Special materials | household appliances, white electricity, kitchen electricity
Qs100 at command mqtt access thingsboard
Research Report on market supply and demand and strategy of China's digital camera lens industry
Go interface implementation principle [advanced level]
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
Nbiot module me3616 at command mqtt connecting thingsboard