刷啊刷

1313. 解压缩编码列表
给你一个以行程长度编码压缩的整数列表 nums 。

考虑每对相邻的两个元素 [a, b] = [nums[2i], nums[2i+1]] (其中 i >= 0 ),每一对都表示解压后有 a 个值为 b 的元素。

请你返回解压后的列表。

示例:

输入:nums = [1,2,3,4]
输出:[2,4,4,4]
解释:第一对 [1,2] 代表着 2 的出现频次为 1,所以生成数组 [2]。
第二对 [3,4] 代表着 4 的出现频次为 3,所以生成数组 [4,4,4]。
最后将它们串联到一起 [2] + [4,4,4]= [2,4,4,4]。

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        res=[]
        if len(nums)<2:
            return []
        for i in range(len(nums)//2):
            tmp=[]
            a= 2*i
            b =2*i+1
            tmp.append(nums[b])
            tmp=tmp*nums[a]
            res+=tmp
        return res

1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        HashMap={}
        for index,num in enumerate(nums):
            another_num=target-num
            if another_num in HashMap:
                return[HashMap[another_num],index]
            HashMap[num]=index
        return None
        

```java
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer>HashMap = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            int temp=target-nums[i];
            if(HashMap.containsKey(temp)){
               return new int[]{HashMap.get(temp),i};
            }
            HashMap.put(nums[i],i);
        }
        return null;
    }
}

39. 组合总和**
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。

输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ]

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        n=len(candidates)
        res=[]
        def helper(i,tmp_sum,tmp):
            if tmp_sum > target or i == n:
                return 
            if tmp_sum==target:
                res.append(tmp)
                return
            helper(i,tmp_sum+candidates[i],tmp + [candidates[i]])
            helper(i+1,tmp_sum,tmp)
        helper(0,0,[])`在这里插入代码片`
        return res

78. 子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
示例:

输入: nums = [1,2,3] 输出:
[ [3], [1], [2], [1,2,3], [1,3],
[2,3], [1,2], [] ]

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        res.add(new ArrayList<>());
        for(int i=0;i<nums.length;i++){
            int all =res.size();
            for(int j=0;j<all;j++){
                List<Integer> tmp = new ArrayList<>(res.get(j));
                tmp.add(nums[i]);
                res.add(tmp);
            }
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值