码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 查找排序部分习题 242. 有效的字母异位词 74. 搜索二维矩阵 1. 两数之和 167.两数之和 II


    242. 有效的字母异位词

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
    注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            ss = list(s)
            tt = list(t)
            ss.sort()
            tt.sort()
            return ss == tt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            return sorted(list(s)) == sorted(list(t))
            # sorted()函数返回重新排序的列表,与sort()函数的区别在于sort()函数是list列表中的函数,而sorted()函数可以对所有可迭代对象进行排序操作。并且用sort()函数对列表排序时会影响列表本身,而sorted()函数则不会。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            # 两个字典
            dict1 = {}  # {'a':1 'b':2}
            dict2 = {}
            for ch in s:
                dict1[ch] = dict1.get(ch, 0) + 1
            for ch in t:
                dict2[ch] = dict2.get(ch, 0) + 1
            return dict1 == dict2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    74. 搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
    每行中的整数从左到右按升序排列。
    每行的第一个整数大于前一行的最后一个整数。

    线性查找 or 二分查找

    class Solution(object):
        def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            for line in matrix:
                if target in line:
                    return True
            return False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    class Solution(object):
        def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            h = len(matrix)  # 长度 几行
            if h == 0:
                return False  #[]
            w = len(matrix[0])  # 宽度 几列
            if w == 0:
                return False  # [[], [], []]
            left = 0
            right = w * h - 1
            """
            0 1  2 3
            4 5  6 7
            8 9 10 11
            第9个位置,num//4行,num%4列
            i = num // 4
            j = num % 4
            """
            while left <= right:  #  二分查找代码  候选区有值
                mid = (left + right) // 2
                i = mid // w
                j = mid % w
                if matrix[i][j] == target:
                    return True
                elif matrix[i][j] > target:  # 待查找的值在mid左侧
                    right = mid - 1
                else:  # matrix[mid] < target  待查找的值在mid右侧
                    left = mid + 1
            else:
                return False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    1. 两数之和 167.两数之和 II → 输入无序/有序数组

    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
    你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
    你可以按任意顺序返回答案。

    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):
                    if nums[i] + nums[j] == target:
                        return sorted([i,j])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    若为有序数组,可二分查找

    class Solution(object):
        def binary_search(self, li, left, right, val):  # 二份查找函数
            # left = 0
            # right = len(li) - 1
            while left <= right:  # 候选区有值
                mid = (left + right) // 2
                if li[mid] == val:
                    return mid
                elif li[mid] > val:  # 待查找的值在mid左侧
                    right = mid - 1
                else:  # li[mid] < val  待查找的值在mid右侧
                    left = mid + 1
            else:
                return None
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
    
            for i in range(len(nums)):
                a = nums[i]
                b = target - a
                if b >= a:
                    j = self.binary_search(nums, i + 1, len(nums) - 1, b)
                else:
                    j = self.binary_search(nums, 0, i - 1, b)
                if j:
                    break
            return sorted([i+1, j+1])  # 题目需要输出index
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    无序列表的二分查找

    class Solution(object):
        def binary_search(self, li, left, right, val):  # 二份查找函数
            # left = 0
            # right = len(li) - 1
            while left <= right:  # 候选区有值
                mid = (left + right) // 2
                if li[mid][0] == val:
                    return mid
                elif li[mid][0] > val:  # 待查找的值在mid左侧
                    right = mid - 1
                else:  # li[mid] < val  待查找的值在mid右侧
                    left = mid + 1
            else:
                return None
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            new_nums = [[num, i] for i, num in enumerate(nums)]  # 二维列表 每一行有 数字num 下标i
            new_nums.sort(key = lambda x:x[0]) # 按照数num排序  new_nums[i][0]是数,new_nums[i][1]是原来的下标
    
            for i in range(len(new_nums)):
                a = new_nums[i][0]
                b = target - a
                if b >= a:
                    j = self.binary_search(new_nums, i + 1, len(new_nums) - 1, b)
                else:
                    j = self.binary_search(new_nums, 0, i - 1, b)
                if j:
                    break
            return sorted([new_nums[i][1], new_nums[j][1]])
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    泛在电力物联网形势下考虑多主体投资的配电网运行优化策略
    ACM. HJ45 名字的漂亮度 ●●
    leetcode 416. Partition Equal Subset Sum 分割等和子集(中等)
    软考 - 操作系统
    药物滥用第五篇介绍
    一个技术混子参加《 2022 谷歌开发者大会》的一日游记
    Avue和Element-UI动态三级表头
    gromacs中一些有用的分析命令
    Java 基于微信小程序的考研咨询平台
    二叉树的学习
  • 原文地址:https://blog.csdn.net/zrg_hzr_1/article/details/133428074
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号