经过昨天一天,还有今天的学习,终于是把第一个程序给搞出来,虽然是借鉴他人的,但是还是收获颇多。现在把

这个程序的问题,和解决方案在下面贴出:

MultiplyStrings

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.

给定用字符串表示的两个数字,计算其乘积:

public class MultiplyStrings {//这种算法是模拟了正常的乘积运算解出答案。
<span style="white-space:pre">  </span>//原始解法中问号部分,我认为是不需要的,不太理解是什么意思,
<span style="white-space:pre">    </span>//我在Leecode中测试是可以通过的。public static void main(String[] args) {String a1 = "12888";String b1 = "13131555";String  s = multiply(a1, b1);System.out.println("s =" + s);}public static String multiply(String num1, String num2) {StringBuilder s1 = new StringBuilder(num1).reverse();StringBuilder s2 = new StringBuilder(num2).reverse();int l1 = s1.length();int l2 = s2.length();int[] result = new int[l1 + l2 - 1];for(int i = 0; i < l1; i++){for(int j = 0; j < l2; j++){result[i+j] += (s1.charAt(i) - '0') * (s2.charAt(j) - '0');}}//System.out.println(Arrays.toString(result));StringBuilder s = new StringBuilder();int carry = 0;for(int i = 0; i < result.length; i++){int currDigit = (result[i] + carry) % 10;carry = (result[i]+carry)/10;s.insert(0, currDigit);System.out.println(s);}if(carry!=0){s.insert(0, carry);}
/*???????????????????????????????????????????????????????int i = 0;while( i < s.length() && s.charAt(i) == '0'){i++;}if(i ==  s.length() ) s.delete(0,i-1);else s.delete(0, i);
*/return s.toString();}
}

转载于:https://www.cnblogs.com/sunbinbin/p/5122560.html

LeeCode_MultiplyStrings相关推荐

最新文章

  1. python避免深度嵌套的if-else_避免嵌套If语句?(Python 2.7)
  2. 【springboot中使用拦截器】
  3. 前端学习(2814):小程序学习之建立第一个项目
  4. LeetCode 415. 字符串相加(大数加法)
  5. 阵列卡直通模式和raid模式_DNF:希洛克Raid攻坚流程攻略
  6. C# WPF 生成随机数添加到DataGrid表格
  7. BGP通告路由的方法以及BGP静态路由下放
  8. Atitit.月度计划日程表 每月流程表
  9. App山寨疯狂 爱加密Apk加密平台防破解
  10. PHP8.0环境详细安装教程
  11. 运用regedit编辑器恢复清空回收站之后的文件
  12. 疫情期间,在家办公,这几款高效远程办公协作工具解决燃眉之急
  13. 教妹学Java(十四):switch 语句详解
  14. 中国计算机学会会刊变动公告
  15. 例说STM32F7高速缓存——Cache一致性问题(一)
  16. n维椭球体积公式_物质的量浓度计算公式都有什么?
  17. channel error:reply-code=404, reply-text=NOT_FOUND - no exchange ‘console‘ in vhost ‘/‘, class-id=40
  18. ffmpeg 视频倍速播放 和 慢速播放
  19. POJ 2657 Comfort
  20. 三个数比大小 输出最大值

热门文章

  1. java中字符串的截取
  2. c++常量函数的理解
  3. kubectl bash补全
  4. 【opencv】边缘提取或通过二值图片提取对应的三维图像(python)
  5. 【R语言-20行代码】牛顿迭代法求伽马函数极大似然估计法的参数估计
  6. Python爬虫,通过特定的函数来筛选标签
  7. Python实现微信消息防撤回
  8. 什么是Java内存模型中的happens-before
  9. Class.getResources()和classLoader.getResources()区别
  10. ListView实现分页