Sueeeeee 2023. 8. 28. 09:04
반응형

2023.08.27

1. Contains Duplicate

import java.util.HashSet;
import java.util.Set;

//0.242ms
public class Solution {

  public boolean containsDuplicate(int[] nums) {
    // TODO: Write your code here
    Set <Integer> set = new HashSet<>();
    for(int i = 0; i<nums.length;i++) {
      if(set.contains(nums[i])){
        return true;
      }else {
        set.add(nums[i]);
      }
    }
    return false;
  }
}

 

It can be implemented by sorting whcih has O(NlogN) for time complexity, but O(1) or O(N) for space complexity

 

2. Panagram

class Solution {
  public boolean checkIfPangram(String sentence) {
    // TODO: Write your code here
    sentence = sentence.toLowerCase();
    char [] arr = sentence.toCharArray();
    Set <Character> set = new HashSet<>();
    for(char ch : arr) {
      if(Character.isLetter(ch)) {
        set.add(ch);
      }
    }
    return set.size() == 26? true : false;
  }
}

3. Sqrt

-> Binary Search approach

-> Since the floor of the square root of a number x lies between 0 and x/2 for all x > 1, we can use binary search within this range to find the square root

반응형