본문 바로가기

Algorithm20

[알고리즘/LeetCode] Longest Palindromic Substring 앞뒤가 똑같은 🎶 전화번호 🎵주어진 문자를 잘랐을때 앞 뒤의 문자가 똑같으면서 가장 긴 문자를 반환하는 문제입니다. 좌우가 똑같은 문자열이라고 보면 되겠습니다.abab 가 주어졌을때는 좌측 a 우측 a 중간값 b 로해서 aba 가 가장 긴 문자열이 됩니다. 물론 bab 도 동일합니다.babab 가 주어진다면 3번째 요소값인 b 를 중심으로 좌우가 똑같게 됩니다. 3번째 요소값을 좌 우로 a 가 배치되고 한층 더 밖으로 나가면 b 가 동일하게 배치되어 있죠?! 이런식으로 좌우가 동일한 문자로 형성된 문자열중에서 가장 긴 문자열을 반환하는 문제입니다. https://leetcode.com/problems/longest-palindromic-substring Longest Palindromic Substr.. 2026. 1. 29.
[알고리즘] 4. Median of Two Sorted Arrays https://leetcode.com/problems/median-of-two-sorted-arrays 두 배열을 합쳐서 중간값을 구하는 알고리즘 문제입니다. 일단 for 문을 사용해서 리스트를 합치다가 리스트에 존재하는 addAll() 생각이 나서 적용을 해보았습니다.그래서 간단하게 [...배열1, ...배열2].sort() 형태로 간단하게 만들었습니다.class Solution { double findMedianSortedArrays(List nums1, List nums2) { List total = [...nums1, ...nums2]..sort(); int totalLen = total.length; int halfLen = (total.length / 2).toInt(); .. 2026. 1. 25.
[알고리즘] Longest Substring Without Repeating Characters Given a string s, find the length of the longest substring without duplicate characters.https://leetcode.com/problems/longest-substring-without-repeating-characters/ 문자열에서 반복되지 않는 문자가 나오기전까지 문자열의 갯수를 세는 것입니다. 첫번째 도전은 다음과 같이 했습니다.class Solution { int lengthOfLongestSubstring(String s) { if (s.length == 1) return 1; List list = []; int count = 0; for (int i =0; i count) { .. 2026. 1. 22.
[알고리즘] Add Two Numbers - Dart Node 개념을 다루는 문제입니다.https://leetcode.com/problems/add-two-numbers/ 연결이 관련된 노드를 코드로 구현하려고 처음 생각을 하면 생각 이상으로 노드 개념이 머릿속에 자리잡고 있지 않아서 막연하게 어렵다는 생각이 먼저 들었습니다. 연결된 값을 어떻게 이어줘야하지? 라는 생각과 함께 어떻게 구현할까 고민을 했습니다. 기본적인 반복문 이후에 새로 생성한 노드의 값을 첫 노드의 next 에 넣어주는 작업과 동시에 next 에 담긴 ListNode 값에 val 및 next 값을 넣어주는게 관건이네요. 마지막으로 val 값들의 합이 10을 넘을때에 대한 처리를 over 에 값을 넣어서 해결해주고 다음 val 값 합을 할때 사용했는데, l1 과 l2 가 모두 null 일.. 2026. 1. 20.
[Algorithm] Dart로 풀어보는 역방향 연결 리스트 만들기 Reverse Linked ListGiven the head of a singly linked list, reverse the list, and return the reversed listInput: head = [1,2,3,4,5]Output: [5,4,3,2,1]Input: head = [1,2]Output: [2,1]Input: head = []Output: []Definition for singly-linked list.class ListNode { int val; ListNode? next; ListNode([this.val = 0, this.next]);} Node란 연결 리스트(Linked List)에서 데이터를 저장하는 기본 단위각 노드는 자신이 가진 데이터와 함께 다음 노.. 2025. 8. 26.
[Algorithm] Dart로 풀어보는 Summary Ranges 요약 범위 You are given a sorted unique integer array nums.A range [a,b] is the set of all integers from a to b (inclusive).Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.Each range [a,b] in the list should be outpu.. 2025. 8. 4.