博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
42. Multiply Strings
阅读量:4626 次
发布时间:2019-06-09

本文共 1512 字,大约阅读时间需要 5 分钟。

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note: The numbers can be arbitrarily large and are non-negative.

---

 09/21, Review

顺序,

最高位是0要去掉

public class Solution {    public String multiply(String num1, String num2) {              if (num1.equals("0") || num2.equals("0"))            return "0";                 int l1 = num1.length();        int l2 = num2.length();        int newlen = l1+l2;               int[] n1 = new int[l1];          int[] n2 = new int[l2];          int[] res = new int[newlen];                // convert string to number array          for (int i=0; i
0 && i

 

 

---

Reference

public class Solution {    public String multiply(String num1, String num2) {              if (num1.equals("0") || num2.equals("0"))            return "0";                 int l1 = num1.length(), l2 = num2.length();        int newlen = l1+l2;               int[] n1 = new int[l1];          int[] n2 = new int[l2];          int[] res = new int[newlen];                // convert string to number array          for (int i=0; i
=0; k--) { sb.append((char)(res[k] % 10 + '0')); if (k>0) res[k-1] += res[k] / 10; } // trim 0 at the biginning if(sb.charAt(newlen- 1) == '0') sb.deleteCharAt(newlen- 1); // reverse sb = sb.reverse(); return sb.toString(); }}

 

转载于:https://www.cnblogs.com/ycled/p/3304685.html

你可能感兴趣的文章
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Xcode10 library not found for -lstdc++ 找不到问题
查看>>
Mysql 8.0.13如何重置密码
查看>>
发布功能完成
查看>>
excel 合并单元格
查看>>
How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
查看>>
待飞笔记(第一天 )
查看>>
解惑好文:移动端H5页面高清多屏适配方案
查看>>
traefik添加多证书
查看>>
忽略UserInterfaceState.xcuserstate
查看>>
ReactNative--Flexbox布局
查看>>
java实现读取文件大全
查看>>
[Cordova] 无法显示Alert视窗
查看>>
借助过度区选择阈值
查看>>
评论列表显示及排序,个人中心显示
查看>>
JavaScript 实现鼠标拖动元素
查看>>
js 模糊查询 (360接口)
查看>>