알고리즘
-
[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] == ..
-
[leetcode] 977. Squares of a Sorted Array알고리즘 2021. 11. 6. 15:51
Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com [Success] 내림차순으로 정렬된 정수 배열이 주어졌을 때, 내림차순으로 정렬된 각 인덱스 값의 제곱 구하는 문제 [풀이] 각 인덱스의 값의 제곱을 구한 후 해당 인덱스에 다시 할당시킨다. 제곱의 값으로 다 채워진 후에는 해당 배열을 내림차순으로 정렬. class Solution { public int..
-
[leetcode] Find Numbers with Even Number of Digits알고리즘 2021. 11. 6. 14:24
Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com [Success] 정수 배열이 주어졌을 때, 자릿수가 짝수인 값은 몇개인지 반환하는 문제 [풀이] Math.log10 라는 함수를 이용하여 정수 값의 자릿수를 알아 낸 후 자릿수가 짝수인지 확인하여 짝수라면 카운트 값을 증가시킨 후 반환한다. class Solution { public static int ..
-
[leetcode] 485. Max Consecutive Ones알고리즘 2021. 11. 6. 10:31
Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com [Success] 이진수로 구성된 배열이 주어졌을 때, 연속되는 1의 값의 최대 수를 구하는 문제 풀이 배열길이 만큼 돌면서 특정 인덱스 값이 1 이면, count 값을 증가시키고 count 가 최대 값으로 설정될 때마다 maxCount 에 할당해준 후 최종적으로 maxCount 를 return 시킨다. ..
-
[leetcode] Squares of a Sorted Array알고리즘 2021. 9. 3. 22:49
https://leetcode.com/explore/learn/card/fun-with-arrays/523/conclusion/3574/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com Success 오름차순으로 정렬된 배열이 주어졌을 때 , 각 원소의 값을 제곱한 후 오름차순 되어 있는 상태로 반환 해야한다. 풀이 각 원소에 값을 제곱을 하여 새로 값..
-
[leetcode] Find All Numbers Disappeared in an Array알고리즘 2021. 9. 3. 22:00
https://leetcode.com/explore/learn/card/fun-with-arrays/523/conclusion/3270/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com Success 위 문제는 n 만큼의 길이를 가진 배열이 주어졌을 때, 해당 원소의 값이 1부터 ~ n까지로 채워져야 하는데 1~n 사이에 값 중 없는 값을 찾아서 Lis..
-
[leetcode] Third Maximum Number알고리즘 2021. 9. 3. 18:17
https://leetcode.com/explore/learn/card/fun-with-arrays/523/conclusion/3231/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com Success 해당 문제는 배열이 주어졌을 때 세번째로 큰 값을 리턴하는 문제이다. 이때 배열의 길이가 3보다 작을경우엔 가장 큰 값을 리턴하며, 중복된 원소는 하나의 ..