当前位置:网站首页>Offer harvester: add and sum two long string numbers (classic interview algorithm question)

Offer harvester: add and sum two long string numbers (classic interview algorithm question)

2022-07-07 08:12:00 Extraordinary~

  In the interview , To ease the embarrassment of the interview , In some interviews, you will write simple algorithm problems to ease the atmosphere, such as bubbling, simple algorithm problems , Today, I bring you an algorithm problem I encountered, and share it with him , Ideas are written in the comments of the corresponding code , If you don't understand, you can have a look , You can point out what is wrong with the code

package com.interview;

import com.sun.javafx.image.BytePixelSetter;

public class Test2 {
    public static void main(String[] args) {
        String str1 = "568658123123312123123123123123131233132123123123131231233123131231231231231";
        String str2 = "132121231231231312312312312312312312312312312312313123123101";
        //reverse() The method is to reverse the string .
        //StringBuffer Class is a variable string class , It can be used to store strings .
        StringBuffer s1 = new StringBuffer(str1).reverse();
        StringBuffer s2 = new StringBuffer(str2).reverse();
        StringBuffer res = new StringBuffer();
        int len1 = s1.length();
        int len2 = s2.length();
        int len;
        // If two strings are not the same length , Then take the length of the larger string .
        if (len1 < len2) {
            // intend : If len1<len2, be len=len2, otherwise len=len1.
            len = len2;
            // use len2-len1 To fill in len1 The vacancy of .
            int count = len2 - len1;
            // Complete the shorter string with the longer string .
            while (count-- > 0)
                // Fill that vacancy . Additional 
                s1.append("0");
        } else {
            // If two strings have the same length , Then take the length of the larger string .
            len = len1;
            // use len1-len2 To fill in len2 The vacancy of .
            int count = len1 - len2;
            // Complete the shorter string with the longer string .
            while (count-- > 0)
                s2.append("0");
        }

        int overflow = 0;
        int num;
        // Traverse each character of two strings , Add two characters .
        for (int i = 0; i < len; i++) {
            //charAt() The method is to return the character at the specified index .
            //num It's string ASCII Code value .
            num = s1.charAt(i) - '0' + s2.charAt(i) - '0' + overflow;
            // If the added number is greater than 9, Then subtract 10, And will overflow Set to 1.
            if (num >= 10) {
                // Subtract... From the added number 10.
                overflow = 1;
                // Convert the added numbers into characters .
                num = num - 10;
            } else {
                // If the added number is less than 10, Will overflow Set to 0.
                overflow = 0;
            }
            // Convert the added numbers into characters .
            res.append(String.valueOf(num));
        }
        // If the added number is greater than 9, Then subtract 10, And will overflow Set to 1.
        if (overflow == 1)
            // Convert the added numbers into characters .
            res.append("1");
        // Convert the result to a string .
        System.out.println(res.reverse().toString());
    }
}

This is the main idea, which is to sum through inversion

原网站

版权声明
本文为[Extraordinary~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070501502576.html