`
hcx2013
  • 浏览: 83320 次
社区版块
存档分类
最新评论

Multiply Strings

 
阅读更多

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 Solution {
    public String multiply(String num1, String num2) {
    	String st1 = new StringBuilder(num1).reverse().toString();
    	String st2 = new StringBuilder(num2).reverse().toString();
    	
    	int[] res = new int[st1.length()+st2.length()];
    	for (int i = 0; i < st1.length(); i++) {
    		for (int j = 0; j < st2.length(); j++) {
    			res[i+j] += (st1.charAt(i)-'0')*(st2.charAt(j)-'0');
    		}
    	}
    	StringBuilder sb = new StringBuilder();
    	for (int i = 0; i < res.length; i++) {
    		int digit = res[i]%10;
    		int carry = res[i]/10;
    		if (i+1 < res.length) {
    		    res[i+1] += carry;
    		}
    		sb.insert(0, digit);
    	}
    	while (sb.charAt(0)=='0' && sb.length()>1) {
    		sb.deleteCharAt(0);
    	}
    	return sb.toString();
    }
}

 

分享到:
评论

相关推荐

    cpp-算法精粹

    Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle II Spiral Matrix Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on ...

    LeetCode最全代码

    ...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....|-----|---------------- | --------------- |...

    leetcodepython001-LeetCode:力码

    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题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: ...Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi

    week1-wednesday

    &lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD#周三9/16的概述 回顾昨天,去做功课 介绍数组 在课堂练习中(成对) ...//1....//2....//3.... Add, subtract, multiply and divide them. ... Add, subtract, multiply and divide the strings

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    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 ...

    AT&T Assembly Language

    Multiply by shifting 224 Dividing by shifting 225 Rotating bits 226 Decimal Arithmetic 227 Unpacked BCD arithmetic 227 Packed BCD arithmetic 229 Logical Operations 231 Boolean logic 231 Bit testing ...

    EurekaLog_7.5.0.0_Enterprise

    5)....Added: Improvements for call stack of dynarrays/strings allocations (leaks) 6)....Added: "Elem size" when reporting leaks in dynarrays 7)....Added: Streaming unpacked debug info into temporal ...

    ARM® Compiler v5.06 for µVision® armasm User Guide

    8.3 Fused Multiply-Add extension for VFP 8.4 Extension register bank mapping in VFP 8.5 VFP views of the extension register bank 8.6 Load values to VFP registers 8.7 Conditional execution of VFP ...

    一个win32下的ARM开源编译器

    FASMARM v1.42 This package is an ARM assembler add-on for FASM. FASMARM currently supports the full range of instructions for 32-bit and 64-bit ARM processors and coprocessors up to and including v8...

    深入理解计算机系统(英文版)

    2.1.5 Representing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.6 Representing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.7...

Global site tag (gtag.js) - Google Analytics