대충이라도 하자

Design Gurus(Day1) 본문

꼬꼬마 개발자 노트/Coding Problems

Design Gurus(Day1)

Sueeeeee
반응형

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

반응형

'꼬꼬마 개발자 노트 > Coding Problems' 카테고리의 다른 글

Design Guru(Day2)  (0) 2023.08.29
Leetcode - Median of two sorted Arrays  (0) 2022.07.13
Blind 75 Must Do Leetcode - Two sum  (0) 2022.06.07
Diagonal Traverse  (0) 2022.03.22
Best Time to Buy and Sell Stock ( Leetcode)  (0) 2022.02.23
Comments