leetcode题解日练--2016.8.30

###不给自己任何借口

今日题目:

1、字符串乘法;

2、两个链表加法

今日摘录:

年轻吗 不要紧
听过几首歌 爱过几个人 就老了

——独木舟《深海里的星星Ⅱ》

43. Multiply Strings | Difficulty: Medium

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.
Converting the input string to integer is NOT allowed.
You should NOT use internal library such as BigInteger.
tag:数学|字符串
题意:字符串之间的乘法,不允许转换为整型。

思路:
1、题目明确说了不能转整型,可以考虑https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation/2的解法

class Solution {
public:
    string multiply(string num1, string num2) {
        int m = num1.size(),n=num2.size();
        int *pos = new int[m+n]();


        for(int i=m-1;i>=0;i--)
        {
            for(int j=n-1;j>=0;j--)
            {
                int posHigh = i+j,posLow = i+j+1;
                int mul = (num1[i]-'0')*(num2[j]-'0')+pos[posLow];
                pos[posHigh] += mul/10;
                pos[posLow] = (mul)%10;
            }
        }

        string res="";
        for(int i=0;i<m+n;i++)
            res =res+char(pos[i]-0+'0');
        while(res[0]=='0')
        {
            res = res.substr(1,res.size()-1);
        }
        return res.size()==0?"0":res;
    }
};

结果:8ms

2. Add Two Numbers | Difficulty: Medium

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

tag:链表|数学

题意:两个链表都是逆序存储数位,求它们的加法
思路:
1、两个链表的长度可能相等也可能不相等,分两种情况讨论:
链表长度相等的时候,直接依次访问链表中元素,一定会同时访问完两个链表,最后判断下有没有进位来决定是否还需要加一个1.
链表长度不相等的时候,一定最后还有一个链表没有访问完毕,继续对该链表的元素进行访问,最后判断下有没有进位来决定是否还需要加一个1.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if(l1==NULL || l2==NULL) return l1==NULL?l2:l1;
        ListNode *res = new ListNode(INT_MIN);
        ListNode*cur = res;
        int c=0;
        while(l1 && l2)
        {
            cur->next = l2;
            int digit = l1->val+l2->val+c;
            l2->val = digit%10;
            c = digit/10;
            cur = cur->next;
            l1 = l1->next;
            l2 = l2->next;
        }
        if(l1||l2)  
        {
            if(l1)  cur->next = l1;
            cur = cur->next;
            while(c&&cur)
            {
                int digit = (cur->val+c);
                cur->val = digit%10;
                c = digit/10;
                if(cur->next)  cur = cur->next;
                else    break;
            }
        }
        if(c)  {cur->next=new ListNode(1);}
        return res->next;
    }
};

结果:48ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值