Leetcode 415 Add Strings

来源:互联网 发布:电子烟推荐 知乎2017 编辑:程序博客网 时间:2024/05/16 14:53
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 24px; line-height: 26.4px;"></span><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Given two non-negative numbers <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num1</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num2</code> represented as string, return the sum of <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num1</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num2</code>.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><span style="box-sizing: border-box; font-weight: 700;">Note:</span></p><ol style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><li style="box-sizing: border-box;">The length of both <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num1</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num2</code> is < 5100.</li><li style="box-sizing: border-box;">Both <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num1</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num2</code> contains only digits <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">0-9</code>.</li><li style="box-sizing: border-box;">Both <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num1</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">num2</code> does not contain any leading zero.</li><li style="box-sizing: border-box;">You <span style="box-sizing: border-box; font-weight: 700;">must not use any built-in BigInteger library</span> or <span style="box-sizing: border-box; font-weight: 700;">convert the inputs to integer</span> directly.</li></ol><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"></p><div style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p></div>
public class Solution {    public String addStrings(String num1, String num2) {        StringBuffer buffer = new StringBuffer();//建一个buffer来存放结果        int L1=num1.length();          int L2=num2.length();                  char[] number1 = num1.toCharArray();//将字符串转换为字符数组        char[] number2 = num2.toCharArray();                        int carry = 0;                for(int i=0;i<L1||i<L2;i++){//因为两个字符串的长度可能不一样 所以这个地方用或           int s1= i<L1?number1[L1-1-i]-48:0;//注意ascii码的转换 要有48的bias           int s2= i<L2?number2[L2-1-i]-48:0;                      int sum = s1+s2+carry;//设置carry是因为要考虑10-18的进位问题                      buffer.append(sum%10);//把每一位数字塞进buffer           carry = sum/10;        }                if(carry==1){            buffer.append("1");//最后一位carry 也就是最高位        }                return buffer.reverse().toString();//注意进buffer的顺序 需要reverse 最后再转换为string            }} 

0 0
原创粉丝点击