본문 바로가기

전체 글379

[Easy] LeetCode - no.219 Contains Duplicate II : Java https://leetcode.com/problems/contains-duplicate-ii/description/?envType=study-plan-v2&envId=top-interview-150  문제int 배열 nums가 주어졌을 때, 똑같은 숫자끼리의 인덱스 차이가 k 이하인지 여부 판단Example 1:Input: nums = [1,2,3,1], k = 3 Output: true Example 2:Input: nums = [1,0,1,1], k = 1 Output: true Example 3:Input: nums = [1,2,3,1,2,3], k = 2 Output: false   풀이1 public static boolean containsNearbyDuplicate(int[] nums,.. 2024. 10. 17.
[Easy] LeetCode - no.202 Happy Number : Java https://leetcode.com/problems/happy-number/description/?envType=study-plan-v2&envId=top-interview-150  문제숫자 n이 happy number인지 여부 판단happy number란 각 자리 숫자들을 각각 제곱한 값을 계속 더해가다가 결국 1이 되는 숫자를 말함Example 1:Input: n = 19 Output: true Explanation: 1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 Example 2:Input: n = 2 Output: false   풀이1 public static boolean isHappy(int n) { i.. 2024. 10. 17.
[Easy] LeetCode - no.1 Two Sum : Java https://leetcode.com/problems/two-sum/description/?envType=study-plan-v2&envId=top-interview-150  문제int 배열 nums 내의 2개의 숫자를 더했을 경우 target이 된다면, 각각의 인덱스를 int 배열로 반환Example 1:Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].   풀이1 public static int[] twoSum(int[] nums, int target) { int[] answer = new int[2]; for (int i = 0; i .. 2024. 10. 17.
[Easy] LeetCode - no.242 Valid Anagram : Java https://leetcode.com/problems/valid-anagram/description/?envType=study-plan-v2&envId=top-interview-150  문제String s와 t가 주어졌을 때, t가 s의 anagram인지 여부 판단Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s = "rat", t = "car"Output: false    풀이1 public static boolean isAnagram(String s, String t) { Map map = new HashMap(); if (s.length() != t.length()) { return false.. 2024. 10. 17.
[Easy] LeetCode - no.290 Word Pattern : Java https://leetcode.com/problems/word-pattern/description/?envType=study-plan-v2&envId=top-interview-150  문제String pattern과 s가 서로 일대일 대응(bijection) 관계에 있는지 여부를 판단이 때 중복된 문자는 같은 단어에 대응해야 하고, 다른 문자는 다른 단어에 대응해야 함두 문자가 같은 단어에 대응하거나, 두 단어가 같은 문자에 대응하면 안 됨Example 1:Input: pattern = "abba", s = "dog cat cat dog"Output: trueExplanation:The bijection can be established as:'a' maps to "dog".'b' maps to "cat.. 2024. 10. 16.
[Easy] LeetCode - no.205 Isomorphic Strings : Java https://leetcode.com/problems/isomorphic-strings/description/?envType=study-plan-v2&envId=top-interview-150  문제String s와 t가 동형(isomorphic)인지 판별하는 문제이 때 두 문자열이 동형이라는 것은, 각 문자가 서로 일대일로 매핑되어야 하며, 동일한 문자가 다른 위치에서도 동일하게 매핑되어야 함을 의미함Example 1:Input: s = "egg", t = "add"Output: trueExplanation:The strings s and t can be made identical by:Mapping 'e' to 'a'.Mapping 'g' to 'd'.Example 2:Input: s = "foo",.. 2024. 10. 16.