본문 바로가기

JAVA/Coding Test Study151

[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.
[Easy] LeetCode - no.383 Ransom Note : Java https://leetcode.com/problems/ransom-note/description/?envType=study-plan-v2&envId=top-interview-150  문제magazine의 알파벳들로 ransomNote를 만들 수 있는지 여부를 판단하여 true 또는 false를 반환할 것이 때 magazine의 알파벳은 각각 한 번만 사용 가능Example 2:Input: ransomNote = "aa", magazine = "ab" Output: falseExample 3:Input: ransomNote = "aa", magazine = "aab" Output: true   풀이import java.util.*;class Solution { public boolean canCons.. 2024. 10. 14.
[Easy] LeetCode - no.392 Is Subsequence : Java https://leetcode.com/problems/is-subsequence/description/?envType=study-plan-v2&envId=top-interview-150  문제2개의 String 값인 s와 t가 주어졌을 때, s가 t의 subsequence라면 true를 반환, 아니라면 false를 반환할 것이 때  subsequence란 t의 순서를 유지하면서, 특정 문자를 지움으로써 s를 만들 수 있는지 여부이다.Example 1:Input: s = "abc", t = "ahbgdc" Output: trueExample 2:Input: s = "axc", t = "ahbgdc" Output: false   풀이class Solution { public boolean isSubse.. 2024. 10. 14.