0
点赞
收藏
分享

微信扫一扫

Java 第 22 课 187. 重复的DNA序列

滚过红尘说红尘 2022-04-13 阅读 38
pythonjava

第 22 课

217. 存在重复元素

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        v = set()
        for n in nums:
            if n in v: return True
            v.add(n)
        return False

        # return len(nums) != len(set(nums))
class Solution {
    public boolean containsDuplicate(int[] nums) {
        var set = new HashSet();
        for (int i : nums){
            if (!set.add(i)) return true; // 巧用返回值
            // if (set.contains(i)) return true;
            // set.add(i);
        }        
        return false;
    }
}

219. 存在重复元素 II

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:        
        n = len(nums)
        # 1、双 for 超时
        # for i in range(n):
        #     for j in range(max(0,i-k),i):
        #         if nums[i] == nums[j]:
        #             return True

        # 2、in
        # for i in range(n):
            # if nums[i] in set(nums[i+1:i+1+k]):return True

        # 3、set
        s, c = set(), 0
        for i, num in enumerate(nums):
            if num in s: return True
            s.add(num)
            # 1) 计数
            # c += 1
            # if c > k:
            #     s.remove(nums[i - k])
            #     c -= 1
            # 2) 计数
            # if c < k: c += 1
            # else: s.remove(nums[i - k])
            # 3) len 
            if len(s) > k: s.remove(nums[i - k])

        return False
class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Set s = new HashSet();
        for ( int i = 0; i < nums.length; i++){
            if (i > k) s.remove(nums[i - k - 1]); // 滑动窗口
            // if (s.contains(nums[i])) return true;
            // s.add(nums[i]); 
            if (!s.add(nums[i])) return true;           
        }
        return false;
    }
}

基础知识

set.add
set.contains

第 22 课


第 22 课


第 22 课


第 22 课

★575. 分糖果

class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:        
        return min(len(candyType)//2, len(set(candyType)))
class Solution {
    public int distributeCandies(int[] candyType) {
        Set set = new HashSet();
        int n = candyType.length, h = n / 2;
        for (int i : candyType) {
            set.add(i);
            if (set.size() >= h) return h;
        }
        return set.size();

        // for (int candy : candyType) set.add(candy);
        // return Math.min(set.size(), candyType.length / 2);
    }
}

187. 重复的DNA序列

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        ans, d = set(), set()
        for i in range(10, len(s) + 1):
            x = s[i - 10:i]
            if x in d: ans.add(x)
            else: d.add(x)
                
        return list(ans)
class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        Set<String> set = new HashSet<>();        
        ArrayList<String> list = new ArrayList<>();
        int n = s.length();
        for (int i = 10; i <= n; i++){
            String t = s.substring(i - 10, i);
            if(!set.add(t)) {
                if (!list.contains(t)) list.add(t);
            };
            set.add(t);
        }
        return list;
    }
}

888. 公平的糖果棒交换

720. 词典中最长的单词

2006. 差的绝对值为 K 的数对数目

★1700. 无法吃午餐的学生数量

1897. 重新分配字符使所有字符串都相等

1748. 唯一元素的和

1365. 有多少小于当前数字的数字

1331. 数组序号转换

1189. “气球” 的最大数量

1002. 查找共用字符

914. 卡牌分组

389. 找不同

229. 求众数 II

169. 多数元素

举报

相关推荐

0 条评论