Skip to content

Commit 9c88f84

Browse files
committed
배열
0 parents  commit 9c88f84

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Array/Array169.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int majorityElement(int[] nums) {
5+
Arrays.sort(nums);
6+
7+
int target = 0;;
8+
int targetCount = 0;
9+
int maxElement = 0;
10+
int maxElementCount = 0;
11+
12+
for (int i = 0; i < nums.length; i++) {
13+
if (i == 0) {
14+
target = nums[i];
15+
targetCount++;
16+
} else {
17+
if (target != nums[i]) {
18+
if (maxElementCount < targetCount) {
19+
maxElement = target;
20+
maxElementCount = targetCount;
21+
}
22+
23+
target = nums[i];
24+
targetCount = 1;
25+
} else {
26+
targetCount++;
27+
}
28+
}
29+
}
30+
31+
if (targetCount > maxElementCount) {
32+
return target;
33+
} else {
34+
return maxElement;
35+
}
36+
}
37+
}

Array/Array283.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public void moveZeroes(int[] nums) {
3+
int temp;
4+
int idx = 0;
5+
6+
for (int i = 0; i < nums.length - 1; i++) {
7+
if (idx == nums.length) {
8+
break;
9+
}
10+
11+
if (nums[i] == 0) {
12+
temp = nums[i];
13+
14+
for (int j = i; j < nums.length - 1; j++) {
15+
nums[j] = nums[j + 1];
16+
}
17+
18+
nums[nums.length - 1] = temp;
19+
20+
i--;
21+
idx++;
22+
}
23+
}
24+
}
25+
}

Array/ReadMe.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 배열
2+
---
3+
4+
- 논리적 순서와 물리적 순서가 같음
5+
- 인덱스(index)를 통해 데이터에 접근 가능
6+
- 크기가 고정되어 있음
7+
- 데이터 삽입/삭제에 대한 비용이 있음
8+
9+
---
10+
11+
- Access : O(1)
12+
- Search : O(n)
13+
- Insert/Delete : O(n)
14+
15+
---
16+
17+
# Array-169 Majority Element
18+
---
19+
20+
```
21+
Given an array nums of size n, return the majority element.
22+
23+
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
24+
```
25+
26+
## 내 풀이 방식
27+
---
28+
29+
1. 배열을 오름차순으로 정렬
30+
2. 각 요소를 반복하여 이전 요소와 같으면 계속해서 카운트를 증가시키고 다르면 이전 요소의 개수가 저장된 Max 요소와 비교 후 Max 요소 갱신
31+
32+
## 더 나은 풀이 방식
33+
---
34+
35+
1. 배열을 오름차순으로 정렬
36+
2. 가운데 있는 요소를 출력
37+
38+
-> 해당 방법이 가능한 이유가 가장 많은 개수의 요소는 배열의 크기의 절반보다 많다고 나와있다.
39+
40+
# Array-283 Move Zeroes
41+
---
42+
43+
```
44+
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
45+
```
46+
47+
## 내 풀이 방식
48+
---
49+
50+
1. 배열의 요소가 0이면 해당 요소는 가장 뒤로 보내고 나머지 요소를 한칸씩 당긴다.
51+
2. 해당 반복은 배열의 사이즈 만큼 반복된다.
52+
53+
## 더 나은 풀이 방식
54+
---
55+
56+
1. 배열의 요소가 0이 아니면 다른 배열에 순서대로 저장
57+
2. 남은 사이즈 만큼 0으로 채움

ReadMe.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 1주차 자료구조와 알고리즘
2+
---
3+
4+
- [배열](./Array)
5+
- leetcode 169
6+
- leetcode 283
7+
- leetcode 78

0 commit comments

Comments
 (0)