算法Coding练习--leetcode【Python 第二天】 (字符的加减乘除)

2.1 LeetCode[2]  Add Two Numbers:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

链表的基本操作,2->4->3分别对应个位\十位\百位数的数值,对应位数的数值和进位值分别为:

val, flag = (l1.val+l2.val + 前一位的flag)%10, (l1.val+l2.val)//10

temp.next = ListNode(flag%10),将值放入链表中

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        flag = 0
        r_node = temp = ListNode(0)
        while l1 or l2 or flag:
            if l1:
                flag = flag + l1.val
                l1 = l1.next
            if l2:
                flag = flag + l2.val
                l2 = l2.next
            temp.next = ListNode(flag%10)
            flag = flag//10
            temp = temp.next
        return r_node.next

2.2 LeetCode[43]  Multiply Strings:

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"

Note:
1. The length of both num1 and num2 is < 110.
2. Both num1 and num2 contain only digits 0-9.
3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

注意不能直接将string转换为int型,但是可以每个string中取出char,然后char转换为int再进行计算。计算的过程中需要将num1和num2分别取值进行乘法操作,与上一题不同的地方是乘法结果直接相加就行了,而不需要再考虑是否需要进位的事情。

class Solution(object):
    def addStrings(self, num1, num2):
        r_num = 0
        k = 1
        while num1 or num2 or flag:
            if num1:
                flag = flag + int(num1[-1])
                num1 = num1[:-1]
            if num2:
                flag = flag + int(num2[-1])
                num2 = num2[:-1]
            r_num = r_num + (flag%10) * k
            flag = flag//10
            k = k * 10
        return str(r_num)

2.3 LeetCode[415]  Add Strings:

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

与上一题解题思路一致,只不过在该题中处理的两个值为字符串而非链表

class Solution(object):
    def addStrings(self, num1, num2):
        
        flag = 0
        r_num = 0
        k = 1
        while num1 or num2 or flag:
            if num1:
                flag = flag + int(num1[-1])
                num1 = num1[:-1]
            if num2:
                flag = flag + int(num2[-1])
                num2 = num2[:-1]
            r_num = r_num + (flag%10) * k
            flag = flag//10
            k = k * 10
        return str(r_num)

2.4 LeetCode[989]  Add to Array-Form of Integer:

For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1].Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

Example 1:
Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:
Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:
Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

Example 4:
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000

Note:
    1 <= A.length <= 10000
    0 <= A[i] <= 9
    0 <= K <= 10000
    If A.length > 1, then A[0] != 0

解题思路同上面两题,只不过此时的两个相加的数变为[一个list + 一个数值] 

class Solution(object):
    def addToArrayForm(self, A, K):
        """
        :type A: List[int]
        :type K: int
        :rtype: List[int]
        """
        
        flag = 0
        r_num = []
        while A or K>0 or flag:
            if A:
                flag = flag + A[-1]
                A = A[:-1]
            if K>0:
                flag = flag + K%10
                K = K//10
            nums = [flag%10]
            flag = flag//10
            nums.extend(r_num)
            r_num = nums
        return r_num

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值