Algorithm/LeetCode
-
[array] two sumAlgorithm/LeetCode 2025. 1. 25. 13:51
✅ Problemhttps://leetcode.com/problems/two-sum/description/ ✅ Approach & Solution방식1) 브루트 포스로 계산더보기배열 순회하여 모든 조합 확인class Solution { public int[] twoSum(int[] nums, int target) { for (int i = 0; i 방식2) 첫 번째 수를 뺀 결과 키 조회더보기(target - nums[i])에 해당하는 값이 배열에 존재하는지 확인 (Map 사용)class Solution { public int[] twoSum(int[] nums, int target) { Map numsMap = new HashMap(); // 키와 값을..
-
[string] longest palindromic substringAlgorithm/LeetCode 2025. 1. 19. 13:26
✅ Problemhttps://leetcode.com/problems/longest-palindromic-substring/description/ ✅ Approach & Solution방식)더보기투 포인터 방식을 사용한다.offset, limit 사용 (아래 코드에서 left가 offset, maxLen이 limit이 됨)팰린드롬이 될 수 있는 최소 길이(1 초과)의 값을 기준으로 양 옆으로 확장해나간다.최소 길이 : 2 or 3ex) aa, abaclass Solution { int left, maxLen; public String longestPalindrome(String s) { // 문자 길이 저장 int len = s.length(); /..
-
[string] group anagramsAlgorithm/LeetCode 2025. 1. 18. 14:36
✅ Problemhttps://leetcode.com/problems/group-anagrams/description/ ✅ Approach & Solution방식1)더보기주어진 문자열 하나씩 비교한다.이중 for문 형태이미 그룹에 포함된 문자열은 이후 비교할 때 제외한다.두 문자열이 애너그램인지 어떻게 확인할까?문자열의 길이가 다른 경우 애너그램이 아니다.문자열의 길이가 같은 경우철자가 동일한지 확인한다.방안1) 알파벳 등장 횟수 비교 : Map 사용방안2) 문자열 사전식 정렬 후 두 문자열이 동일한지 확인class Solution { public List> groupAnagrams(String[] strs) { List> result = new ArrayList(); in..