목록js (166)
N
https://leetcode.com/problems/repeated-substring-pattern/ Repeated Substring Pattern - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열 패턴 찾기 문제. 이중 for문으로 쉽게 해결할 수 있다. 먼저 pattern을 0번째 인덱스부터 i번째 인덱스까지 자른다. 그리고 isRepeated라는 flag를 설정하고, 내부 for문이 모두 돌게 되면 isRepated는 바뀌지 않아 패턴 문자열이라..
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ Find All Numbers Disappeared in an Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com nums 배열에서 없는 수를 answer에 담아 리턴하면 된다. 1 부터 nums.length 까지의 수가 nums에 담겨있다. check라는 배열을 length + 1 크기로 선언하고 false로 초기화한다. n..
https://leetcode.com/problems/arranging-coins/ Arranging Coins - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 계단식으로 코인을 쌓을 때, n 개의 코인이 몇층까지 완벽하게 채워져있는지 확인하는 문제다. sum과 row 두 변수를 이용해서 풀 수 있다. sum은 한 층마다 놓은 코인의 총 합이다. row 층마다 코인을 row 개 놓을 수 있으니, while문을 이용해 sum < n이라면 sum += row를 하..
https://leetcode.com/problems/add-strings/ Add Strings - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열로 이뤄진 두 수를 더한 값을 문자열로 다시 리턴하면 된다. 각 자리수를 number형으로 바꾸고 더한 값을 answer 배열에 push하는 과정이다. 먼저 두 수의 자릿수를 맞춘다. num1과 num2의 길이를 비교하여 더 작은 길이를 num1으로 바꿔준다. 이후 num1은 num2의 길이만큼 0을 앞에 붙여..
https://leetcode.com/problems/third-maximum-number/ Third Maximum Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com map을 활용. nums에는 중복된 수가 있을 수 있기 때문에, 중복을 제거하기 위해 map을 선언한다. 그리고 nums를 순회하면서 map에 nums[i]에 해당하는 수가 없으면 nums[i]를 key로 하여 value를 true로 해준다. 순회가 끝나면 다음은 map을 순회한다. ..
https://leetcode.com/problems/longest-consecutive-sequence/ Longest Consecutive Sequence - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Union-Find와 Map을 활용 parent라는 map을 선언. nums[i]를 순회하면서 key와 value를 모두 자기 자신인 nums[i]로 초기화한다. 이후 연속된 수의 부모를 하나로 합치는 작업을 진행. nums[i]를 순회하면서 unionPa..
https://programmers.co.kr/learn/courses/30/lessons/81303 코딩테스트 연습 - 표 편집 8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z"] "OOOOXOOO" 8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"] "OOXOXOOO" programmers.co.kr Node class를 선언하여, 양방향 리스트 형태로 문제를 풀었다. Node class는 val, prev, next를 갖는다. 생성자로 val은 매개변수로 받아오며, prev와 next는 null을 가지도록 한다. n개의 크기로 answer와 node를 선언한다. answer는 "O"로 초기화, node는 n..
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/ Flatten a Multilevel Doubly Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 주어진 양방향 리스트를 평탄화를 하여 리턴해야 한다. 우선 예외처리로 head가 비어있다면 그대로 비어있는 head를 리턴한다. 그렇지 않다면 DFS로 모든 노드를 배열에 담아주도록 한다. 담는 순서는 현재 방문한 노..