[leetcode] 415. Add Strings @ python

LeetCode --- 415. Add Strings Given two non-negative integersnum1andnum2represented as string, return the sum ofnum1andnum2. Note: The length of bothnum1andnum2is < 5100. Bothnum1andnum2contains only digits0-... 阅读详情

原题

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

Note:

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

解法1

定义一个函数, 将string转化为int, 我们遍历字符串, 使用ord函数计算每个数字跟’0’的距离. 最后将int转化为string.
Time: 2*O(n)
Space: O(1)

代码

class Solution(object):
    def addStrings(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        return str(self.stringToInt(num1) + self.stringToInt(num2))
    
    def stringToInt(self, s):
        ans = 0
        for i in range(len(s)):
            ans += (ord(s[i]) - ord('0'))*10**(len(s) - 1 - i)
        return ans

解法2

同解法1

代码

class Solution(object):
    def addStrings(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        def getNum(num):
            ans = 0
            for d in num:
                ans = 10*ans + ord(d) - ord('0')
            return ans
        return str(getNum(num1) + getNum(num2))
思必驰离线语音芯片:智能家居控制的核心引擎 思必驰离线语音芯片TH1520通过本地化语音识别技术,为智能家居带来革命性体验。该芯片具备200-600毫秒的快速响应、75分贝抗噪能力和双核异构设计,显著提升设备交互效率。支持自定义指令和10种音色选择,开发周期缩短至3天,BOM成本降低30%,是智能家居控制的核心引擎。 阅读详情

相关推荐

HHD Device Monitoring Studio:破解版在串口调试中的实战应用

本文详细介绍了HHD Device Monitoring Studio破解版在串口调试中的实战应用,包括安装指南、串口监控功能解析、Modbus RTU协议分析案例以及自定义协议解析技巧。通过具体操作步骤和性能优化建议,帮助开发者高效完成端口监控和调试任务,提升工作效率。

weixin_29268331的博客 942

力扣(leetcode)第415题字符串相加(Python)

你不能使用任何內建的用于处理大整数的库(比如 BigInteger), 也不能直接将输入的字符串转换为整数形式。给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和并同样以字符串形式返回。输入:num1 = “11”, num2 = “123”输入:num1 = “456”, num2 = “77”输入:num1 = “0”, num2 = “0”num1 和num2 都只包含数字 0-9。num1 和num2 都不包含任何前导零。

qq_58737789的博客 678

【Telephony】Telephony总纲!Phone进程流程(AOSP)

整个Framework层的Telephony框架运行在一个叫做Phone(com.android.phone)的进程中。 Phone进程启动的过程中,会初始化非常多的分支功能,SIM卡流程初始化就是其中之一,所以我觉得有必要写一下Phone进程的框架

weixin_44513784的博客 1975

Python | Leetcode Python题解之第415题字符串相加

Python | Leetcode Python题解之第415题字符串相加

Mopes__的博客 536

光伏发电预测(LSTM、CNN_LSTM和XGBoost回归模型,Python代码)

一旦结果与预测差距较大,可能存在光伏系统的问题,需要找出原因,然后采取适当措施来修复光伏系统并做出更好的决策。例如,根据准确的预测,光伏系统运营商可以平衡电力消耗,并将多余的电力储备用于紧急情况。然而,光伏系统的发电量估算是一个挑战,因为光伏系统的发电量受到天气条件的极大影响。整个项目文件夹(第一个文件夹为xgboost模型相关的包文件,xg.py是xgboost模型英文注释版本,xg_chinese.py是xgboost模型中文注释版本)表示空气的温度,通常以摄氏度(°C)或华氏度(°F)为单位。

2301_79699110的博客 3032

PyMySQLSaver实战:构建高可靠性的LangGraph状态管理

本文详细介绍了如何使用PyMySQLSaver构建高可靠性的LangGraph状态管理系统。通过检查点机制和MySQL持久化技术,开发者可以确保AI应用在崩溃后从最近状态恢复,支持长时间运行任务和多轮交互。文章包含核心架构解析、生产环境部署指南及实战案例,帮助开发者掌握状态管理的核心技术。

weixin_29310605的博客 494

LeetCode-Python-415. 字符串相加

给定两个字符串形式的非负整数num1和num2,计算它们的和。 注意: num1和num2的长度都小于 5100. num1和num2都只包含数字0-9. num1和num2都不包含任何前导零。 你不能使用任何內建 BigInteger 库,也不能直接将输入的字符串转换为整数形式。 思路: 先排除corner case, 然后调整一下nums1,nums2的相对...

Keep Coding 983

吃透Chisel语言.04.Chisel基础(一)——信号类型和常量

开始肯定是介绍Chisel的基础内容了,《Chisel基础》这一部分主要会介绍数字设计中的两个基本组件:组合电路和触发器。这俩基本组件都不复杂,但是组合在一起就可以得到规模很大、功能也很强大的数字电路了。 第一部分我们就简单地看看Chisel中的信号类型和常量,在上一篇文章中也有相关介绍,其中还包括和Verilog之间的对比。......

github-3rr0r的博客 3822

[LeetCode 415] Add Strings(Python)

题目描述 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

yurenguowang的博客 780

k8s篇之underlay网络和overlay区别

underlay和overlay

小橙子的笔记屋 2143

基于STM32、HAL库的RN8209D电能计量芯片驱动程序设计

/ 假设电压互感器变比为220V:0.5V,电流互感器变比为20A:5mA。// 电压ADC原始值。// 电流ADC原始值。// 有功功率(W)// 电能(kWh)

corlin6688的博客 470

[leetcode: Python]415. Add Strings

Title Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9

cyy2learn的博客 1376

[RK3568 Android11] 开发之蓝牙(AP6275S)

前言 蓝牙是一种支持设备短距离通信(一般10m内)的无线电技术,能在包括移动电话、PDA、无线耳机、笔记本电脑、相关外设等众多设备之间进行无线信息交换。利用“蓝牙”技术,能够有效地简化移动通信终端设备之间的通信,也能够成功地简化设备与因特网Internet之间的通信,从而数据传输变得更加迅速高效,为无线通信拓宽道路。 接下来是在RK3568上调试AP6275S模块蓝牙部分: 一、设备树dts配置 根据硬件原理图,找到对应的控制引脚,配置好dts: ...

~未来可期~ 4072

Leetcode 415. Add Strings (python+cpp)

Leetcode 415. Add Strings题目解法:follow up 1:两个string相减follow up2:两个string相加,但是有可能有负号 题目 解法: 这边默认肯定是不能用强制类型转换的 python: class Solution: def addStrings(self, num1: str, num2: str) -> str: ans = '' pt1 = len(num1)-1 pt2 = len(num2

qq_37821701的博客 450

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【LeetCode】415. Add Strings 字符串相加

给定两个字符串形式的非负整数 `num1` 和 `num2` ,计算它们的和并同样以字符串形式返回。 你不能使用任何內建的用于处理大整数的库(比如 `BigInteger` ), 也不能直接将输入的字符串转换为整数形式。

负雪明烛 2813

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode笔记:415. Add Strings

不直接转为整型来计算两个字符串表示的数字之和

Minecraft 2961

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

【leetcode 题解】415. Add Strings【E】

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is Both num1 and num2 contains only digits 0-9.B

sscssz的博客 814

【LeetCode】415. Add Strings 字符串/大整数相加 - Easy

[LeetCode] 415. Add Strings 字符串相加 Given two non-negative numbersnum1andnum2represented as string, return the sum ofnum1andnum2. Note: The length of bothnum1andnum2is < 5100. Both...

It’s All Uphill From Here 288

LeetCode-415题.Add Strings

代码如下: class Solution { public: string addStrings(string num1, string num2) { //去除字符串中非数字的字符(虽然题目中说是有字符‘0’-‘9’,但是debug时好像不是这样 for(int i = 0; i < num1.size(); i++){ ...

我开始懂了的博客 160

【leetcode】415 Add Strings

Python代码,调了几次才AC,有点乱class Solution(object): def addStrings(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ len1 = len(num1)

u012315428的专栏 403

leetcode415.python字符串相加

给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。注意:num1 和num2 的长度都小于 5100.num1 和num2 都只包含数字 0-9.num1 和num2 都不包含任何前导零。你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。第一种class Solution(object): def addStrings(self, n...

IT_job的博客 1969

leetcode之Add Strings(415)

题目: 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包含任何前导零。 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。 python代码: class Solution: def a...

cuicheng01的博客 212

Leetcode 415. Add Strings

Given two non-negative integersnum1andnum2represented as string, return the sum ofnum1andnum2. Note: The length of bothnum1andnum2is < 5100. Bothnum1andnum2contains only digits0-...

ScutKevinYzy的博客 222

力扣(leetcode) 415. 字符串相加 (反向双指针法)

题目在这:https://leetcode-cn.com/problems/add-strings/ 思路分析: 将字符串像整数那样进行相加。 首先遍历两个字符串,将他们的长度变为一样的,短的哪一个在前置位补0。 然后建立两个空列表 n1和n2,遍历两个字符串,将每个字符变成int型加入到列表中。 使用双指针法,分别指向两个列表的最后一位,向前遍历。注意:这里一定是从后向前遍历,前面一定是在前置位补0. 因为在做加法计算的时候,是从右向左计算,有进位的,往左边进位。 中间使用temp记录进位即可。 完整代

深度不学习!!的博客 404

LeetCode 415. Add Strings(字符串相加)python3版本

代码转载自:点击这里可以查看 我给加了很详细的注解,这样看起来就方便多了,尤其是像我一样的新手 class Solution(object): def addStrings(self, num1, num2): &quot;&quot;&quot; :type num1: str :type num2: str :rtype: str &quot;&quot;...

qq_37002901的博客 443

LeetCode #415: Add Strings

Problem Statement(Source)Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contain

浮生琴弦 367
上一篇: [leetcoe] 387. First Unique Character in a String @ python
下一篇: [leetcode] 235. Lowest Common Ancestor of a Binary Search Tree @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值