116_leetcode_Multiply Strings
来源:互联网 发布:速算计算器软件 编辑:程序博客网 时间:2024/06/07 01:43
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.
1:特殊情况;2:设置数组,字符串相乘,注意进位;3:注意某一位是0的情况;3:最后处理一下边界情况string multiply(string num1, string num2) { if(num1.length() == 0 || num2.length() == 0) { return ""; } int size1 = (int)num1.length(); int size2 = (int)num2.length(); if(size1 == 1) { if(num1[0] == '0') { return "0"; } else if(num1[0] == '1') { return num2; } } if(size2 == 1) { if(num2[0] == '0') { return "0"; } else if(num2[0] == '1') { return num1; } } string result; for(int i = 0; i < size1 + size2; i++) { result.push_back('0'); } int count = 0; for(int i = size1 - 1; i >= 0; i--) { if(num1[i] == '0') { continue; } count = 0; for(int j = size2 - 1; j >= 0; j--) { int value = (num1[i] - '0') * (num2[j] - '0' ) + count + result[i+j+1] - '0'; int temp = value % 10; count = value / 10; result[i+j+1] = temp + '0'; } result[i] = count + '0'; } if(result[0] == '0') { int i; for(i = 1; i < size1 + size2; i++) { result[i-1] = result[i]; } result.pop_back(); } return result; }
0 0
- 116_leetcode_Multiply Strings
- Strings
- Strings
- strings
- Strings
- Strings
- Strings
- strings
- Strings
- leetcode 116: Multiply Strings (uncompleted)
- Foundation - strings
- Localizable.strings
- C++ Strings
- strings.cpp
- strings.h
- C++ Strings
- Splitting strings
- Casting Strings
- POJ 1915 Knight Moves
- 如何查看一个端口被那个进程所占用
- OpenGL API 简介 .
- android背景选择器selector用法汇总
- MySQL 入门教程
- 116_leetcode_Multiply Strings
- 使用Xcode和Instruments调试解决iOS内存泄露
- Jodd - Java世界的瑞士军刀
- 网上免费平台学习美术
- delphi2007中如何安装ActiveX控件
- Netty系列之Netty可靠性分析
- 笑死大学么?.2N脑上谥
- IOS_改变UITextField placeHolder颜色、字体
- 3.17 MonoForAndroid用户人机界面--关于(About)程序信息 Menu功能菜单程序设计