분류 전체보기
-
[leetcode] 26. Remove Duplicates from Sorted Array알고리즘 2021. 11. 16. 22:29
Description 내림차순 정수 배열이 주어졌을때, 해당 배열에서 중복을 모두 제거 한 후 배열의 길이를 출력하는 문제 Input: arr = [1, 1, 2] Output: [1, 2, _] Input: arr = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] Output: [0, 1, 2, 3, 4, _, _, _, _, _] Success 두개의 변수를 사용하여 각각 다른 인덱스를 바라보게 한다. 두 변수가 가르키고 있는 인덱스의 값이 다르면 특정 변수가 가르키고 있는 인덱스에 값을 할당하여 중복이 제거될 수 있도록 한다. class Solution { public int removeDuplicates(int[] nums) { int j= 0; for(int i=1; i
-
[leetcode] Replace Elements with Greatest Element on Right Side알고리즘 2021. 11. 13. 15:49
Description 정수 배열이 주어졌을때, 특정 인덱스를 순회하여 자신보다 큰 값이 나오면 특정 인덱스부터 큰 값의 인덱스 사이의 값들을 모두 큰 값으로 바꿔주는 문제. 단, 마지막 원소는 항상 -1 값을 넣어주어야 한다. Input: arr = [17, 18, 5, 4, 6, 1] Output: [18, 6, 6, 6, 1, -1] Input: arr = [400] Output: [-1] Success 배열 뒤에서 부터 순회하며 현재 값이 앞에 있는 값들보다 큰 경우 모두 현재 값으로 바꿔준다. 배열 원소가 하나남았을때 다시 반대로 순회하며 배열을 한칸씩 앞으로 채워 -1 값이 들어갈 자리를 만들어준다. class Solution { public int[] replaceElements(int[] a..
-
[leetcode] 941. Valid Mountain Array알고리즘 2021. 11. 12. 18:43
Description 정수 배열이 주어졌을때 , 산(mountain) 의 형태로 된 정렬이라면 true 를 반환하는 문제 (오름차순 -> 내림차순) Input: arr = [2, 1] Output: false Input: arr = [3, 5, 5] Output: false Input: arr = [0, 3, 2, 1] Output: true Success 배열의 원소는 최소 2개 이상이여야 하고 최초 시작은 무조건 오름차순으로 가야하기 때문에 [0] 번째 원소가 [1] 번째 원소보다 크면 안된다. 첫번째 while 문을 통해 이전 인덱스가 현재 인덱스 보다 클 때까지 계속 반복한다. 만약, 이전 인덱스가 현재 인덱스보다 크다면 종료 후 다음 while 문으로 이동하여 현재 인덱스부터 계속 값이 작은지를..
-
[leetcode] Check If N and Its Double Exist알고리즘 2021. 11. 12. 16:19
Description 정수 배열이 주어지면 특정 원소 i 를 하나 선택하여 i x 2 를 하였을 때 해당 값이 배열 안에 포함되어 있는지를 찾는 문제 Input: arr = [10, 2, 5, 3] Output: true Input: arr = [7, 1, 14, 11] Output: true Success 이중 for 문을 통하여 순차적으로 특정 값에 2배를 한 후 해당 결과가 배열안에 있는지 탐색한다. class Solution { public boolean checkIfExist(int[] arr) { if (arr == null && arr.length == 0) return false; for(int i=0; i
-
[leetcode] 26. Remove Duplicates from Sorted Array카테고리 없음 2021. 11. 10. 08:06
Description 내림차순으로 정렬된 배열이 주어졌을때 배열 안에 중복된 값들을 제거하는 문제. 단, 새로운 배열 추가 없이 기존 배열에서 중복이 제거되어야 한다. Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Success cnt 라는 변수를 하나 두고 for문이 적용된 i 변수의 인덱스의 값과 cnt 변수의 인덱스 값을 비교하여 다른 경우에만 앞에서부터 새롭게 할당시킨다. class Solution { public int removeDuplicates(int[] nums) { if (nums == null) return -1..
-
[leetcode] 27. Remove Element알고리즘 2021. 11. 9. 22:53
Description 정수로 이루어진 nums 배열과 특정 값 val 이 매개변수로 주어졌을때 배열 안에 val 과 동일한 값이 있으면 해당 배열에서 제거한 후 배열안에 유효한 값이 몇개인지를 출력하는 문제 Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Success cnt와 i 라는 두 개의 변수를 두어 nums[i] 의 값이 val 과 같지 않으면 cnt 가 가르키는 인덱스로 값을 채워 나간다. 그렇게 값을 채워나가다 보면 cnt 에는 val 의 값에 해당되지 않는 유효한 값들만 존재하게되고 cnt 는 ..
-
[leetcode] 88. Merge Sorted Array알고리즘 2021. 11. 8. 21:52
Description nums1 배열과 해당 배열에 유효한 값이 몇개인지를 나타내는 수 m nums2 배열과 해당 배열에 유효한 값이 몇개인지를 나타내는 수 n 위 값들이 매개변수로 들어왔을때 새로운 배열 생성없이 두 배열을 하나의 배열로 합친 후 내림차순으로 정렬하라. Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Input: nums1 = [0], m = 0, nums2 = [1], n = 1 Output: [1] Success m 부터 m+n 까지 반복문을 돌려 nums1 배열의 끝 지점에 nums2..
-
[leetcode] 1089. Duplicate Zeros알고리즘 2021. 11. 7. 21:04
Description 정수 배열이 주어졌을 때 0이 발생할 때마다 0을 하나 더 생성하고 나머지 요소를 오른쪽으로 이동한다. 이 때 새로운 배열을 생성하지 않고 기존 배열에서 값 수정이 일어나야 한다. Input: arr = [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] Input: arr = [1,2,3] Output: [1,2,3] Success 인덱스의 값이 0 이면, 뒤에서 두번째 인덱스부터 뒤로 한칸씩 보낸 후 0 이 나온 인덱스 앞에 값을 0 으로 수정해준다. class Solution { public void duplicateZeros(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] == ..