当前位置:网站首页>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();
}
}
边栏推荐
- @Configurationproperties value cannot be injected
- 关于架构(排名不分先后)
- 37. serialized binary tree
- Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ not found; SQL statement:
- Project requirements specification
- 20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song
- About architecture (in no particular order)
- Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
- Conversion of Halcon 3D depth map to 3D image
- 16. Somme des trois plus proches
猜你喜欢

20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song

Word frequency statistics using Jieba database

Role and understanding of proc/cmdline

利用jieba库进行词频统计

按键精灵的简单入门

论文阅读_图神经网络GIN

Multi thread learning 4. Sleep, wait, yield, join (), ThreadGroup control the running of threads

Computer network connected but unable to access the Internet

Beginning is an excellent emlog theme v3.1, which supports emlog Pro

Multi thread learning III. classification of threads
随机推荐
Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ not found; SQL statement:
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
38. arrangement of strings
43. Number of occurrences of 1 in 1 ~ n integers
[Speech] 如何根据不同国家客制化ring back tone
MySQL Linux Installation mysql-5.7.24
Test work summary - performance test indicators
tkinter使用WebView2网页组件(续篇)
Please remove any half-completed changes then run repair to fix the schema history
Nature | 给全球的新冠伤亡算一笔账
Performance & interface test tool - JMeter
60. points of N dice
XML参数架构,同一MTK SW版本兼容两套不同的音频参数
The most commonly used objective weighting method -- entropy weight method
[gpio] how to modify / display GPIO status through ADB shell
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song
What is the difference between ArrayList and LinkedList?
The way to promote software test engineer
个体工商户是不是法人企业