【数据结构与算法】task01

0001. Two Sum [Easy]

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.
Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]
.
Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.

在这里插入图片描述

# 方法一
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # initiate 一个dict去存 target与当前值的差,和当前的index
        # 例如 {5:0} 当前值在等一个5,来与之共同凑成target
        temp = {}
        # 用emuerate遍历nums数组
        for i, n in enumerate(nums):
            # 如果n在temp中,证明n刚好是之前遇到的某树缺的那个能与之求和等于target的值
            if n in temp:
                return [temp[n], i]
            # 否则,就把当前值在等的那个数,和它自己的index存在dict中
            temp[target-n] = i
         
# 方法二(一样的)
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        temp = {}
        for i, n in enumerate(nums):
            if target-n in temp:
                return [temp[target-n], i]
            temp[n] = i

时间复杂度:因为遍历了一遍nums,所以是O(n)
空间复杂度:用了额外的dict去存储,所以也是O(n)

1929. Concatenation of Array [Easy]

Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).

Specifically, ans is the concatenation of two nums arrays.

Return the array ans.
Example 1:

Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: The array ans is formed as follows
ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
ans = [1,2,1,1,2,1]

Example 2:
Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
Explanation: The array ans is formed as follows:
ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
ans = [1,3,2,1,1,3,2,1]

Constraints:

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

在这里插入图片描述

# 方法一
class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        ans = []
        for i in range(2):
            for n in nums:
                ans.append(n)
        return ans
        
# 方法二
class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        ans = nums + nums
        return ans
        
# 方法三
class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        return nums*2

方法一和二比较慢,用时大约40ms,第三种方法快很多,可能是python在底层做了优化不是用的循环遍历吧。总的来说,循环遍历O(n), 空间复杂度不算输出arr的话是O(1)

0771. Jewels and Stones [Easy]

You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

Letters are case sensitive, so “a” is considered a different type of stone from “A”.

Example 1:
Input: jewels = “aA”, stones = “aAAbbbb”
Output: 3

Example 2:
Input: jewels = “z”, stones = “ZZ”
Output: 0

Constraints:

  • 1 <= jewels.length, stones.length <= 50
  • jewels and stones consist of only English letters.
  • All the characters of jewels are unique.

在这里插入图片描述

class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
    	# initiate 最后的返回值
        res = 0
        # 遍历string stones,看每颗s是否是jewel
        for s in stones:
        	# 如果s是jewel,则res+1
            if s in jewels:
                res += 1
            # 这里对s不是jewel就不写了,因为不在乎
        return res

时间复杂度:遍历了一遍stone算O(n),对每次s遍历了一遍jewel(一次遍历jewel算O(m)的话),那总共是O(mn)
空间复杂度:O(1)

因为用s去和jewel比对的时候花费了较多不必要的时间,我们可以针对这一点去优化。比如说找一个看s是不是在jewel里面这种情况是O(1)的数据结构去替代掉原始的jewel。那set就立马跳出来啦,因为我们知道在 x in set的平均时间复杂度是O(1)

在这里插入图片描述
快了大概10ms

class Solution:
    def numJewelsInStones(self, jewels: str, stones: str) -> int:
        jewels = set(jewels)
        res = 0
        for s in stones:
            if s in jewels:
                res += 1
        return res

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构与算法设计项目01背包问题是指在算法设计与分析课程中,学生需要解决一个背包问题。该问题要求在给定的一组物品和背包容量的情况下,选择一些物品放入背包中,使得被选中物品的总价值最大化,同时保证它们的总重量不超过背包的容量。 在数据结构与算法课程设计实验中,学生通常会进行一系列实践项目,比如定时/计数技术应用程序设计、图形变换程序设计、代码转换程序设计等。这些项目旨在帮助学生巩固和应用所学的数据结构与算法知识,提高他们的编程能力和解决实际问题的能力。在解决01背包问题的项目中,学生需要设计和实现一个算法,以有效地选择物品放入背包,从而达到最优化的目标。 为了解决01背包问题,学生可以使用动态规划算法。这种算法通过建立一个二维数组来存储子问题的解,并利用子问题的解来构建更大规模的问题的解。具体来说,学生可以先初始化一个二维数组,然后使用循环遍历物品和背包容量的组合,并根据当前物品的重量和价值来更新数组中对应位置的值。通过动态规划算法,学生可以找到最优解,即选择哪些物品放入背包以达到最大的总价值。 除了动态规划算法,学生还可以尝试其他的算法来解决01背包问题,比如贪心算法和回溯算法。贪心算法通过选择当前最优解来构建整体最优解,而回溯算法则通过穷举所有可能的解来找到最优解。每种算法都有其优缺点,学生可以根据实际情况选择合适的算法来解决01背包问题。 总之,数据结构与算法设计项目01背包问题是一个让学生应用所学知识解决实际问题的项目。学生可以使用动态规划算法或其他算法来解决该问题,并通过编程实践提高自己的算法设计和分析能力。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [算法设计与分析课程的背包问题](https://download.csdn.net/download/you_best_honor/12924018)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [数据结构与算法课程设计报告](https://download.csdn.net/download/nibashangtian/10164603)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值