当前位置: 首页 > news >正文

两数之和 - 简单

*************

Python

topic: 1. 两数之和 - 力扣(LeetCode)

*************

Give the topic an inspection.

How is the weekend? I have been absent the pronunciation practice for about 1 month. That is not good. Let's talk about conments under the posts, vedios and vlogs. People do silly thing. You will see people will type words like ohh this phone is awful under a post, but they never use it. People say that this car is really good and they buy the old-school cars without driving experience. They may ask which car is better or even try them, but then buy the silly ones. There is a slang called you never criticize people because you never wear them shoes.I can easily swear them because I donot want to waste my time walking their journey. Life is never fair, that is it, no big deal, just no fair. Be professional.

Back to the topic. Iterate through the arrat and every two elements are grouped, which means need two loops.

class Solution(object):def twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""n = len(nums)i = 0while i < n:j = i + 1while j < n:if nums[i] + nums[j] == target:return[i, j]j = j + 1i = i + 1return[]

but in python, range are elegent.

range(stop)               # 生成 0 到 stop-1 的整数
range(start, stop)        # 生成 start 到 stop-1 的整数
range(start, stop, step)  # 生成 start 到 stop-1 的整数,步长为 step

Then the code should be rewrite.

class Solution(object):def twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""n = len(nums)for i in range(n):for j in range(i + 1, n):if nums[i] + nums[j] == target:return [i, j]return []

This is not enough, in a real progect, function is separed.

class TwoSumChecker:def __init__(self, nums, target): # _init_ 是构造函数,可以被直接调用self.nums   = nums   # 保存到成员变量self.target = target # 可以避免参数重复传递def check_pair(self, i, j):"""检查 nums[i] + nums[j] 是否等于 target"""return self.nums[i] + self.nums[j] == self.targetclass Solution:def twoSum(self, nums, target):checker = TwoSumChecker(nums, target)  # 创建检查器实例n = len(nums)for i in range(n):for j in range(i + 1, n):if checker.check_pair(i, j):  # 调用检查逻辑return [i, j]return []

相关文章:

  • 面试题之进程 PID 分配与回收算法:从理论到 Linux 内核实现
  • 【NLP】35. 构建高质量标注数据
  • 质检LIMS系统检测数据可视化大屏 全流程提效 + 合规安全双保障方案
  • 本地部署Immich系统结合Cpolar技术实现安全跨设备影像管理方案
  • 【爬虫】DrissionPage-8.1
  • 【深度学习新浪潮】如何入门人工智能?
  • VDC、SMC、MCU怎么协同工作的?
  • upload-labs靶场通关详解:第10关
  • 【算法专题十四】BFS解决FloodFill算法
  • Web前端开发:@media(媒体查询)
  • 解决使用@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss“, timezone = “GMT+8“)时区转换无效的问题
  • 测试开发面试题:Python高级特性通俗讲解与实战解析
  • 5个开源MCP服务器:扩展AI助手能力,高效处理日常工作
  • 永磁同步电机高性能控制算法(22)——基于神经网络的转矩脉动抑制算法为什么低速时的转速波动大?
  • JavaScript 系列之:数组、树形结构等操作
  • Android设备 显示充电速度流程
  • 掌握Git:版本控制与高效协作指南
  • netcore项目使用winforms与blazor结合来开发如何按F12,可以调出chrome devtool工具辅助开发
  • 深入浅出IIC协议 -- 第二篇:FPGA数字接口设计方法论
  • 基于Java在高德地图面查询检索中使用WGS84坐标的一种方法-以某商场的POI数据检索为例
  • 首付款12.5亿美元!三生制药与辉瑞就国产双抗达成合作协议
  • 特朗普与普京开始进行电话会谈,稍后还将致电泽连斯基
  • 聘期三年已至:37岁香港青年叶家麟卸任三亚市旅游发展局局长
  • 西域都护府博物馆今日在新疆轮台县开馆
  • 九江银行落地首单畜牧业转型金融业务,助推传统农业绿色智能
  • 柬埔寨果农:期待柬埔寨榴莲走进中国市场