Python实现"字符串相加"的一种方法

给定两个字符串形式的非负整数 num1num2 ,计算它们的和。

注意:

  1. num1num2 的长度都小于 5100.
  2. num1num2 都只包含数字 0-9.
  3. num1num2 都不包含任何前导零。
  4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

1:用ord()方法将字符串转asc||码进行计算

def addStrings(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        if len(num1) < len(num2):
            num1, num2 = num2, num1
        num1List = list(num1)       #转列表
        num2List = list(num2)
        num2Len = len(num2List)
        add = 0     #进位
        sum = ""
        for i in range(len(num1List)):
            if i < num2Len:
                temp = ord(num1List[-i-1]) - ord('1') + 1 + ord(num2List[-i-1]) - ord('1') + 1 + add   #注意!!列表倒序访问是从-1开始
                add = temp // 10
                sum = str(temp % 10) + sum
            else:
                temp = ord(num1List[-i-1]) - ord('1') + 1 + add
                add = temp // 10
                sum = str(temp % 10) + sum
        if add == 1:          #判断是否还存在进位
            sum = "1" + sum
        return sum

算法题来自:https://leetcode-cn.com/problems/add-strings/description/

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,有多种方式可以组合字符串一种常见的方式是使用字符串相加操作符(+)来拼接字符串。例如,可以使用以下代码将两个字符串组合在一起: ```python str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # 输出:Hello World ``` 另一种方式是使用字符串的`join()`方法。这个方法接受一个可迭代对象作为参数,并将其中的元素用指定的字符串连接起来。例如,可以使用以下代码将一个列表中的字符串元素连接起来: ```python list_words = \["I", "love", "Python"\] result = " ".join(list_words) print(result) # 输出:I love Python ``` 还有一种方式是使用字符串的格式化方法,例如使用`%`操作符或`str.format()`方法。这些方法允许在字符串中插入变量或表达式的值。例如: ```python name = "Alice" age = 25 result = "My name is %s and I am %d years old." % (name, age) print(result) # 输出:My name is Alice and I am 25 years old. name = "Bob" age = 30 result = "My name is {} and I am {} years old.".format(name, age) print(result) # 输出:My name is Bob and I am 30 years old. ``` 从Python 3.6.4版本开始,还引入了一种新的字符串格式化方式,使用f字符串(f-string)。它允许在字符串中直接嵌入变量或表达式,并使用大括号{}包围。例如: ```python name = "Charlie" age = 35 result = f"My name is {name} and I am {age} years old." print(result) # 输出:My name is Charlie and I am 35 years old. ``` 这些是在Python中组合字符串的几种常见方式。根据具体的需求和场景,可以选择适合的方式来组合字符串。 #### 引用[.reference_title] - *1* [Python编程 字符串组成方式](https://blog.csdn.net/yj11290301/article/details/127777020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [五种方式:Python中拼接字符串的正确方法](https://blog.csdn.net/m0_74872863/article/details/129699625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [学习python字符串组合](https://blog.csdn.net/m0_66722163/article/details/122891107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值