首页 > 数据库 >[Oracle] LeetCode 415 Add Strings

[Oracle] LeetCode 415 Add Strings

时间:2022-08-26 05:44:07浏览次数:168  
标签:string num2 int s2 415 Add Oracle pos2 num1

Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Solution

其实就是高精度加法,这里按照竖式加法即可,其中 \(top\) 表示进位

点击查看代码
class Solution {
private:
    string ans="";
    
public:
    string addStrings(string num1, string num2) {
        int n1 = num1.size();
        int n2 = num2.size();
        int top = 0;
        int pos1=n1-1, pos2=n2-1;
        while(1){
            int s1=0, s2=0;
            if(pos1>=0)s1=num1[pos1]-'0';
            if(pos2>=0)s2=num2[pos2]-'0';
            int sum = s1+s2+top;
            if(sum<10){
                ans= char('0'+sum)+ans; top=0;
            }
            else{
                ans= char(sum%10+'0')+ans;top=1;
            }
            pos1--;pos2--;
            if(pos1<0 && pos2<0) break;
        }
        if(top){
            ans=char('0'+top)+ans;
        }
        //reverse(ans.begin(), ans.end());
        return ans;
    }
};

标签:string,num2,int,s2,415,Add,Oracle,pos2,num1
From: https://www.cnblogs.com/xinyu04/p/16626339.html

相关文章

  • P1415 题解
    前言题目传送门!更好的阅读体验?这题是一道挺好的\(\texttt{dp}\)题啊,但大家的题解都写得不够详细。所以,我来补一篇\(\LaTeX\)题解,希望能帮助大家。思路首先是读......
  • 2. Add Two Numbers
    2.AddTwoNumbers题目Youaregiventwonon-emptylinkedlistsrepresentingtwonon-negativeintegers.Thedigitsarestoredinreverseorderandeachofthe......
  • [Oracle] LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60
    Youaregivenalistofsongswheretheithsonghasadurationoftime[i]seconds.Returnthenumberofpairsofsongsforwhichtheirtotaldurationinsecon......
  • 解决方案——padding影响框的大小怎么办
    方法:再输入一行box-sizing:border-box;解释:1、没有设置box-sizing:border-box属性,宽高会加上padding和border的值,需要我们手动去计算,减去padding和border的值,并调整cont......
  • [Oracle] LeetCode 1161 Maximum Level Sum of a Binary Tree
    Giventherootofabinarytree,thelevelofitsrootis1,thelevelofitschildrenis2,andsoon.Returnthesmallestlevelxsuchthatthesumofallth......
  • oracle时间类型、时区及其函数
    一、oracle时间类型oracle有date、timestamp、intervalyeartomonth和intervaldaytosesond四种类型,可通过nls_date_format来设置我们想要的日期格式。1、date存储年......
  • [Oracle] LeetCode 1326 Minimum Number of Taps to Open to Water a Garden
    Thereisaone-dimensionalgardenonthex-axis.Thegardenstartsatthepoint0andendsatthepointn.(i.eThelengthofthegardenisn).Therearen+1......
  • oracle中pivot函数的用法
    pivot函数:对查询结果行转列进行统计示例:比如我想查每个用户投资的各种类型基金的分别有多少份额平常的写法:selectuserID,fundtype,sum(shares)fromuserassetgroup......
  • 修改Oracle共享池大小
    1. sysdba登录数据库[oracle@ufdb165~]$sqlplus/nologSQL*Plus:Release11.2.0.4.0ProductiononWedAug2415:12:202022Copyright(c)1982,2013,Oracl......
  • oracle 怎么查看用户对应的表空间
    oracle怎么查看用户对应的表空间?查询用户:查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system:select*fromdba_users;查看你能管理的所有用户:select......