java 大数相乘,LeetCode_43

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 大数相乘,LeetCode_43相关的知识,希望对你有一定的参考价值。

//
//  ViewController.m
//  TestBitInt
//
//  Created by Sunhy on 17/4/18.
//  Copyright © 2017年 Sunhy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSString *Str = [self computeMutlti:99999999 value2:99999999];
    NSString *Str = [self computeMutltiStr:@"12345678987654321" value2:@"12345678987654321"];
    //NSString *Str = [self computeMutltiStr:@"56789" value2:@"56789"];
    NSLog(@"%@", Str);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)computeMutltiStr:(NSString *)strA value2:(NSString *)strB {
    if (!strA || !strB) return nil;
    NSMutableArray *array = [NSMutableArray array];
    for (int i = strB.length-1; i >= 0; i--) {
        NSString *tempStr = [self mutlti:strA singleElement:[[strB substringWithRange:NSMakeRange(i, 1)] intValue]];
        [array addObject:tempStr];
    }
    NSMutableString *result = [NSMutableString string];
    int addi = 0;
    int tempR = 0;
    // 取最后一条数据的长度。最后一条一定是最长的
    for (int j=0; j<[[array lastObject] length] + (array.count - 1); j++) {
        tempR = 0;
        for (int i=0; i<array.count; i++) {
            if (j <= [(NSString *)array[i] length] - 1 + i && j-i>=0) {
                // 左移i位
                tempR += [[array[i] substringWithRange:NSMakeRange(([array[i] length] - 1 - (j - i)), 1)] intValue];
            }
        }
        tempR += addi;
        addi = tempR / 10;
        [result insertString:[NSString stringWithFormat:@"%d", tempR % 10] atIndex:0];
    }
    if (addi > 0) [result insertString:[NSString stringWithFormat:@"%d", tempR % 10] atIndex:0];
    return result;
}

- (NSString *)computeMutlti:(int)value1 value2:(int)value2 {
    NSString *strA = [NSString stringWithFormat:@"%d", value1];
    NSString *strB = [NSString stringWithFormat:@"%d", value2];
    return [self computeMutltiStr:strA value2:strB];
}

- (NSString *)mutlti:(NSString *)strA singleElement:(int)b {
    int addi = 0;
    int temp = 0;
    NSMutableString *result = [NSMutableString string];
    for (int i=(int)strA.length - 1; i >= 0; i--) {
        temp = [[strA substringWithRange:NSMakeRange(i, 1)] intValue] * b + addi;
        addi = temp / 10;
        [result insertString:[NSString stringWithFormat:@"%d", temp % 10] atIndex:0];
    }
    if (addi > 0) [result insertString:[NSString stringWithFormat:@"%d", addi] atIndex:0];
    return result;
}

@end
import java.util.Arrays;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(solution("15", "15")); 
	}
	
	// 乘法
	public static String solution(String strA, String strB) {
		// TODO Auto-generated method stub
		int lenA = strA.length();
		int lenB = strB.length();
		// 最长
		int carry = 0;
		// 结果最长为乘数位数之和
		int[] res = new int[lenA + lenB];
		Arrays.fill(res, 0);
		int i,j,product;
		for (i=lenA-1; i>=0; i--) {
			for (j=lenB-1; j>=0; j--) {
				product = carry + res[i+j+1] + (strA.charAt(i) - '0') * (strB.charAt(j) - '0');
				carry = product / 10;
				res[i+j+1] = product % 10;
			}
			res[i+j+1] = carry;
		}
		int k = 0;
		while(k<res.length -1 && res[k]==0) k++;
		StringBuffer strBuf = new StringBuffer();
		for (i=k;i<res.length;i++) {
			strBuf.append(res[i]);
		}
		return strBuf.toString();
	}
}

以上是关于java 大数相乘,LeetCode_43的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 43 Multiply Strings 大数相乘

LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

精选力扣500题 第72题 LeetCode 43. 字符串相乘c++/java详细题解

大数相乘求和的模运算

大数相乘的快速乘技巧

C# 使用同余代换简化多个大数相乘取模运算