首页 > 其他分享 >leetcode-415-easy

leetcode-415-easy

时间:2022-10-19 07:46:05浏览次数:53  
标签:num1 num2 int StringBuilder 415 length easy leetcode String

Add String
思路一: 模拟加法运算,字符串前面填零

public String addStrings(String num1, String num2) {
    int max = Math.max(num1.length(), num2.length());
    num1 = pad(num1, max);
    num2 = pad(num2, max);

    int p = 0;
    StringBuilder result = new StringBuilder();
    for (int length = num1.length() - 1; length >= 0; length--) {
        int sum = Integer.parseInt(Character.toString(num1.charAt(length))) +
                Integer.parseInt(Character.toString(num2.charAt(length))) + p;
        if (sum >= 10) {
            p = 1;
            result.insert(0, sum % 10);
        } else {
            p = 0;
            result.insert(0, sum);
        }
    }
    if (p == 1) {
        result.insert(0, '1');
    }

    return result.toString();
}

public static String pad(String str, int len) {
    if (str.length() >= len) return str;

    StringBuilder strBuilder = new StringBuilder(str);
    for (int i = 0; i < (len - str.length()); i++) {
        strBuilder.insert(0, '0');
    }
    return strBuilder.toString();
}

思路二: 做法和思路一一样,细节优化:双指针,指针指向字符串尾部,另外 ‘1’-'0' 可以直接算 char 类型 ‘1’ 对应的 int 值

public String addStrings(String num1, String num2) {
    StringBuilder res = new StringBuilder("");
    int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
    while(i >= 0 || j >= 0){
        int n1 = i >= 0 ? num1.charAt(i) - '0' : 0;
        int n2 = j >= 0 ? num2.charAt(j) - '0' : 0;
        int tmp = n1 + n2 + carry;
        carry = tmp / 10;
        res.append(tmp % 10);
        i--; j--;
    }
    if(carry == 1) res.append(1);
    return res.reverse().toString();
}

标签:num1,num2,int,StringBuilder,415,length,easy,leetcode,String
From: https://www.cnblogs.com/iyiluo/p/16804873.html

相关文章

  • leetcode-389-easy
    FindtheDifference思路一:xor两个字符串publiccharfindTheDifference(Strings,Stringt){charresult=0;for(inti=0;i<s.length();i++){......
  • leetcode-226-easy
    InvertBinaryTree思路一:递归,交换左右。这题比较出名,个人感觉面试的题目和实际工作中遇到的问题还是不太一样的,所以一点准备都不做就跑去面试,答不上来很正常。一般能力......
  • leetcode-219-easy
    ContainsDuplicateII思路一:for循环遍历,结果超时publicbooleancontainsNearbyDuplicate(int[]nums,intk){intleft=-1;for(inti=0;i<nums......
  • leetcode-448-easy
    思路一:用数组记录publicList<Integer>findDisappearedNumbers(int[]nums){int[]m=newint[nums.length];for(intnum:nums){if(num-1......
  • leetcode-495-easy
    TeemoAttacking思路一:对两个前后攻击序列,完全分类只可能出现两种情况,只要把重叠的时间都减去,就是所求时间。此分类对三个以上的重叠时间依旧成立没有重叠,攻击时间累......
  • [LeetCode] 1700. Number of Students Unable to Eat Lunch
    Theschoolcafeteriaofferscircularandsquaresandwichesatlunchbreak,referredtobynumbers0and1respectively.Allstudentsstandinaqueue.Eachstu......
  • leetcode 380. Insert Delete GetRandom O(1) O(1) 时间插入、删除和获取随机元素 (
    一、题目大意实现RandomizedSet类:RandomizedSet()初始化RandomizedSet对象boolinsert(intval)当元素val不存在时,向集合中插入该项,并返回true;否则,返回false......
  • IDEA必备开发神器之EasyCode
    IDEA必备开发神器之EasyCode目录1、前言2、安装(EasyCode)3、建立数据库4、在IDEA配置连接数据库5、开始生成代码6、pom.xml7、Application.yml8、启动项目1、前......
  • LeetCode 61旋转链表
    LeetCode61旋转链表题目描述:给你一个链表的头节点head,旋转链表,将链表每个节点向右移动k个位置。示例1:输入:head=[1,2,3,4,5],k=2输出:[4,5,1,2,3]示例2:输......
  • Codeforces Round #828 (Div. 3) E1. Divisible Numbers (easy version)(数学/暴力)
    https://codeforces.com/contest/1744/problem/E1题目大意:给定a,b,c,d;让我们选择从(a,b]中选出一个x,在(c,d]中选出一个y;满足(x*y)/(a*b)是一个整数。input51......