본문 바로가기

분류 전체보기

(31)
LeetCode 67. Add Binary 문제 링크: leetcode.com/problems/add-binary/ Add Binary - 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 문제 요약: 두개의 이진수가 문자열 형태(String)으로 주어질 때, 두 수를 더한 값을 출력하시오. (문자열 형태, 이진수형태) 문제 접근: 두 수를 더하는 문제는 보통 overflow만 조심하면 쉽다. 이진수이기 때문에, 맨 끝부터 하나씩 더하면 된다. class Solution { public String addB..
LeetCode 38. Count And Say 문제링크: leetcode.com/problems/count-and-say/ Count and Say - 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 문제 요약: 숫자가 주어질때, 숫자에 대한 String 을 출력. (아래 예제 참고) 예: 1 => "1" (default) 2 => 이전값을 세고, 읽는다. (1을 호출, 한개의 1 => "11") 3 => 이전값(2)을 세고, 읽는다. (2를 호출("11") => 2개의 1 => "21") 4 => 이전값(3..
LeetCode 278. First Bad Version 문제 링크: leetcode.com/problems/first-bad-version/ First Bad Version - 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 이 주어질때, 최초로 불량이 나는 n을 찾아라. isBadVersion(int n)이라는 메소드는 주어진다. 만약 3이 불량(false)이면 3부터 n까지는 모드 불량, 즉 1~n 중에서 최초로 isBadVersion(n)이 false인 수를 찾는문제. 문제 접근: for문으로 1..
LeetCode 35. Search Insert Position 문제링크: leetcode.com/problems/search-insert-position/ Search Insert Position - 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)와 숫자값(target)이 주어졌을때, target이 들어갈 index 위치를 구하시오. 문제 접근: 단순한 정렬문제이다. 시간복잡도 O(n)으로 풀었는데, 조건 잘 맞춰서 루프한번 돌면 끝낼 수 있다. 좀 더 효율적으로는, 배열이 정렬되어있기..
LeetCode 28. Implement strStr() 문제링크: leetcode.com/problems/implement-strstr/ Implement strStr() - 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 문제요약: 문자열이 2개 주어진다. (haystack, needle) haystack문자열 중에서 needle과 일치하는 첫번째 index를 반환하시오. 없다면 -1 반환. 예: haystack: hello, needle: ll -> 2 문제 접근: haystack을 루프 돌면서 needle갯수만큼..
LeetCode 26. Remove Duplicates from Sorted Array 문제링크: leetcode.com/problems/remove-duplicates-from-sorted-array/ 문제 요약: (문제 이해하는게 제일 오래걸렸다.) 정렬된 배열이 주어졌을때, 주어진 배열을 바꾸고(중복을 다 제거하고), 중복아닌 원소 갯수 반환. * 부연설명: 주어진 배열이 [1,1,2,2,3] 이면 중복을빼면 [1,2,3] 인데, 주어진 배열 자체를 [1,2,3,2,3] 뒷인덱스는 신경안쓴다고함. 이런식으로 바꾸고 중복아닌 원소 갯수를 return; 예: [1,1,2] -> [1,2] (내가푼 방식되로 한다면, 원래 배열은 [1,2,2] 이렇게 되있음) 예: [0,0,1,1,1,2,2,3,3,4] -> [0,1,2,3,4] (마찬가지로 원래 배열은 [0,1,2,3,4,2,2,3,3,4..
LeetCode 13. Roman to Integer 문제링크: leetcode.com/problems/roman-to-integer/ Roman to Integer - 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 문제 요약: 문자열(로마숫자)이 주어졌을때, 숫자로 변경하시오. 문제 접근: 로마숫자에 대응되는 숫자를 맵에다 담고, 단순하게 if, else를 여러개 두어서 해결했다. 주의할점은 만약 빼야하는 숫자 (예: IV 5 - 1) 일 경우에는 반복문 변수(i)를 한번 더 더해서 다음 루프에서 다음다음 cha..
LeetCode 337. House Robber III 문제 요약: 이진 트리가 주어졌을때 서로 인접하지 않는 노드들의 최대값을 구하시오. 예: input : [3,2,3,null,3,null,1] output: 7 input: [3,4,5,1,3,null,1] output: 9 문제 접근: root 노드부터 자기자신을 포함했을때와, 안포함했을때의 값을 저장해 둔다음, 자식노드의 값을 비교하여 마지막 노드까지 내려간다. 처음 예제에서 3과 2+3을 가지고 있고 그 자식노드에서 3+3+1과 2+3를 저장해두고 또 자식노드로 가서 비교를 하는 방식으로 구현하면 될 거 같다. 구현 코드: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tr..