第 23 课

  • [★575. 分糖果](https://leetcode-cn.com/problems/distribute-candies/)
  • [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/)


★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> ans = new ArrayList<>();
        int n = s.length();
        for (int i = 10; i <= n; i++){
            String t = s.substring(i - 10, i);
            if (!set.add(t) && !ans.contains(t)) ans.add(t);           
        }
        return ans;

        // Set set = new HashSet(), ans = new HashSet();
        // for (int i = 0; i <= s.length() - 10; i++){
        //     String t = s.substring(i, i + 10);
        //     if (!set.add(t)) ans.add(t);
        // }
        // return new ArrayList(ans);
    }
}