- 浏览: 188237 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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.
计算两个大数的乘法,如果直接计算,就会溢出。假设两个数的长度分别为m, n。那么它们相乘之后的长度肯定为m+n(有进位), 或m + n - 1(没有进位)。我们建立一个长度为m + n 的数组,用来求解每一位上对应的值,维护一个进位。代码如下:
Note: The numbers can be arbitrarily large and are non-negative.
计算两个大数的乘法,如果直接计算,就会溢出。假设两个数的长度分别为m, n。那么它们相乘之后的长度肯定为m+n(有进位), 或m + n - 1(没有进位)。我们建立一个长度为m + n 的数组,用来求解每一位上对应的值,维护一个进位。代码如下:
public class Solution { public String multiply(String num1, String num2) { if(num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0) return ""; int[] digit = new int[num1.length() + num2.length()]; for (int i = num1.length() - 1; i >= 0; i--) { int a = num1.charAt(i) - '0'; for (int j = num2.length() - 1; j >= 0; j--) { int b = num2.charAt(j) - '0'; digit[i + j + 1] += a * b; } } StringBuilder sb = new StringBuilder(); for (int i = digit.length - 1; i > 0; i--) { int num = digit[i] % 10; int carry = digit[i] / 10; sb.append(num); digit[i - 1] += carry; } //检查最高位是否有进位,如果有进位,将进位加入字符串中 if(digit[0] > 0) sb.append(digit[0]); /* 检查最后一位是否为0,如果为0说明字符串一定为一个 全0的序列。因为在上一步我们已经判断是否有进位,如 果为0说明没有进位,只有全为0的情况下,字符串倒数第 二位才可能为0 */ if(sb.charAt(sb.length() - 1) == '0') return "0"; return sb.reverse().toString(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 292You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 395Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 597Merge k sorted linked lists and ... -
N-Queens II
2016-03-06 03:06 694Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 494The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 610Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 949Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 625Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 895Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 808You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 749For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 471There are n bulbs that are init ...
相关推荐
在LeetCode平台上,针对字符串和数学计算的题目中,Multiply Strings(字符串乘法)是一个常见的编程挑战。在这个问题中,需要实现一个函数,该函数接收两个仅包含数字字符的字符串,返回它们的乘积,同样也是一个...
本文将详细介绍LeetCode上编号为43的算法题目,名为“Multiply Strings”,即字符串乘法问题,并提供相应的JavaScript语言实现的题解。 43号问题“Multiply Strings”要求我们实现一个字符串乘法的函数,该函数接受...
在C语言的世界里,文件"C语言-leetcode题解之43-multiply-strings.c"指明了它所关注的内容:解决一个特定的编程题目,即leetcode平台上的第43题,任务是编写一个函数来实现两个大数的字符串形式乘法。C语言的复杂性...
在本压缩包中,主题聚焦于使用Python解决LeetCode第43题——“字符串相乘”(Multiply Strings)。这是一道常见的编程面试题,旨在测试应聘者的字符串处理和算法设计能力。下面将详细探讨该题目的背景、解题思路、...
第43题:字符串相乘(Multiply Strings) 题目描述: 给定两个非负整数num1和num2,它们的长度相同,用字符串形式表示。你的任务是计算它们的乘积,并返回结果字符串。例如,"2" 和 "3" 相乘等于 "6"。 解决方案:...
22. **Multiply Strings**:两个字符串表示的数相乘。同样模拟笔算乘法,使用二维数组存储中间结果。 23. **Rotate Array**:将数组顺时针旋转指定步数。可以先反转整个数组,再反转前半部分和后半部分。 24. **...
- “Multiply Strings(字符串相乘)”是数学和字符串处理结合的题目。 3. **题目解决情况**: - 文件中提到用户已解决149/234道题目,这表示用户在解题过程中拥有一定的实践经验和对算法题目的熟悉度。 4. **...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
- Multiply Strings: 给定两个非负整数字符串形式的数,要求进行乘法运算。 - Wildcard Matching: 实现支持'?'和'*'的通配符匹配。 - Jump Game / Jump Game II: 第一个问题要求判断是否能到达数组的最后一个位置,...
Multiply Strings 066 Add Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: ...Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
<<<<<<< HEAD#周三9/16的概述 回顾昨天,去做功课 介绍数组 在课堂练习中(成对) ...//1....//2....//3.... Add, subtract, multiply and divide them. ... Add, subtract, multiply and divide the strings
filtered_strings = list(filter(is_not_empty, strings)) print(filtered_strings) ``` 输出结果为:`['hello', 'world', 'Python']` 通过上述示例可以看出,`map()`、`reduce()` 和 `filter()` 这三个内置函数在...
filtered_strings = filter(not_empty, strings) # 输出结果:['abc', 'def', 'ghi'] ``` 综上所述,`map()`, `reduce()`和`filter()`是Python中非常实用的高阶函数,它们简化了对列表数据的操作,提高了代码的...
Multiply two numbers without using arithmetic operators Chapter 13. Compute the two’s complement for a given integer Chapter 14. Isolate the rightmost bit set to 1 Chapter 15. Create a mask for ...
- `strings.xml`:用于定义界面上显示的文本字符串。 2. **设计用户界面**: - 在`main.xml`中定义应用的用户界面布局。 - 示例代码如下所示: ```xml android:orientation="vertical" android:layout_...
0043-multiply-strings-字符串相乘,这个文件解决了一个算术问题——大数乘法。当数字无法用标准的整型变量表示时,算法必须找到一种处理大数乘法的方式。 整体来看,这些文件覆盖了数据结构、图论、算法设计、字符...
对于计算逻辑,源码可能包含了多个函数,如`add()`, `subtract()`, `multiply()`, `divide()`,分别对应加减乘除运算。 此外,Android应用的生命周期管理也是一个关键知识点。在这个计算器中,你需要了解如何在...