当前位置:网站首页>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();
}
}
边栏推荐
- CCF noi2022 quota allocation scheme
- Lesson 5: data warehouse construction (III)
- A solution for PHP to implement image login verification code
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
- 虚函数与纯虚函数的关系
- AddUser add user and mount hard disk
- Why can't NAND flash be used as RAM while nor flash can
- The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
- Individual application for ov type SSL certificate
- Flex / fixed Upper, Middle and Lower (Mobile end)
猜你喜欢

ESP8266 Arduino OLED
![[grpc development] go language builds simple server and client](/img/24/06c3c1219ecad7e117f4df152e9ce7.jpg)
[grpc development] go language builds simple server and client

WiFi protocol and ieee905 protocol learning details

yolov5

Performance test - Analysis of performance test results

The combined application of TOPSIS and fuzzy borde (taking the second Dawan District cup and the national championship as examples, it may cause misunderstanding, and the Dawan District cup will be up

个人申请OV类型SSL证书

Beginning is an excellent emlog theme v3.1, which supports emlog Pro
![[fastapi] use pycharm to configure and use environment variables for fastapi projects](/img/a5/47cabfed3f12bf70b4b047ef29fc9d.jpg)
[fastapi] use pycharm to configure and use environment variables for fastapi projects
![[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e](/img/2e/97310ec36aeb1fc1e9c82361141a36.jpg)
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
随机推荐
MySQL Linux Installation mysql-5.7.24
Details of FPGA syntax
国企为什么要上市
Research Report on market supply and demand and strategy of China's digital camera lens industry
16. 最接近的三數之和
About architecture (in no particular order)
C language - how to define arrays
Nature | 给全球的新冠伤亡算一笔账
WiFi band resources
[daily question on niuke.com] two point search
38. 外观数列
67. convert string to integer
Field xxxxDAO in com. nero. hua. service. impl. LoginServiceImpl required a bean of type
GRE protocol details
[road of system analyst] collection of wrong topics in software engineering chapters
Wireshark filter rule
Nature | make an account of the new crown casualties in the world
Go interface implementation principle [advanced level]
Multi thread learning 4. Sleep, wait, yield, join (), ThreadGroup control the running of threads
ESP8266 Arduino OLED