-
Notifications
You must be signed in to change notification settings - Fork 0
[Hamill] 코드 리뷰 요청 #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
ec7a134
b2b1dc1
98a3b6f
eea0561
e99754a
ecd184a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.codesquad.datastructurestudy.basic; | ||
|
||
public class BinarySearch { | ||
|
||
private static int binarySearch(int[] numbers, int number, int first, int last) { | ||
while (first <= last) { | ||
int mid = (first + last) / 2; | ||
|
||
if (numbers[mid] == number) { | ||
return mid; | ||
} else if (numbers[mid] < number) { | ||
first = mid + 1; | ||
} else { | ||
last = mid - 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] numbers = {1,2,3,4,5,6,7,8,9}; | ||
int number = 2; | ||
int index = binarySearch(numbers, number, 0, numbers.length - 1); | ||
|
||
if (index == -1) { | ||
System.out.println("못찾았다"); | ||
} else { | ||
System.out.println(index + "번째 인덱스에서 찾았다"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.codesquad.datastructurestudy.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class BinarySearchOperationCount { | ||
|
||
private static int binarySearch(int[] numbers, int number, int first, int last) { | ||
int mid; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 약간 윤성우님 책 스타일로 선언을 했네요 ㅋㅋ |
||
int opCount = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 윤성우 책에서 안좋아하는 부분이 명확하지 않은 변수명이에요. |
||
|
||
while (first <= last) { | ||
mid = (first + last) / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 리뷰 참고해서 수정하시길 바랄게요! |
||
|
||
if (numbers[mid] == number) { | ||
return mid; | ||
} else if (numbers[mid] < number) { | ||
first = mid + 1; | ||
} else { | ||
last = mid - 1; | ||
} | ||
opCount++; | ||
} | ||
return opCount; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr1 = new int[500]; | ||
int[] arr2 = new int[5000]; | ||
int[] arr3 = new int[50000]; | ||
int number = 5000; | ||
|
||
Arrays.fill(arr1, 0); | ||
Arrays.fill(arr2, 0); | ||
Arrays.fill(arr3, 0); | ||
|
||
int opCount1 = binarySearch(arr1, number, 0, arr1.length - 1); | ||
int opCount2 = binarySearch(arr2, number, 0, arr2.length - 1); | ||
int opCount3 = binarySearch(arr3, number, 0, arr3.length - 1); | ||
|
||
System.out.println("비교연산횟수 : " + opCount1); | ||
System.out.println("비교연산횟수 : " + opCount2); | ||
System.out.println("비교연산횟수 : " + opCount3); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||
package com.codesquad.datastructurestudy.basic; | ||||||
|
||||||
public class LinearSearch { | ||||||
|
||||||
private static int linearSearch(int[] numbers, int number) { | ||||||
int i; | ||||||
for (i = 0; i < numbers.length; i++) { | ||||||
Comment on lines
+6
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗... 아아... |
||||||
if (numbers[i] == number) { | ||||||
return i; | ||||||
} | ||||||
} | ||||||
return -1; | ||||||
} | ||||||
|
||||||
public static void main(String[] args) { | ||||||
int[] arr = {1,2,3,4,5}; | ||||||
int number = 2; | ||||||
int index = linearSearch(arr, number); | ||||||
|
||||||
if (index == -1) { | ||||||
System.out.println("못찾았다"); | ||||||
} else { | ||||||
System.out.println((index+1) + "번째 에서 찾았다"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
사소한거지만, 이런건 띄어쓰는게 관례입니다. |
||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,85 @@ | ||||||
package com.codesquad.datastructurestudy.list.arraylist; | ||||||
|
||||||
public class ArrayList implements List<Integer> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 특이하네요 ㅋㅋㅋ 제네릭을 이렇게 구현하신 이유가 있을까요? |
||||||
|
||||||
private int[] numbers; | ||||||
private int numOfData; | ||||||
|
||||||
public ArrayList() { | ||||||
this.numbers = new int[100]; | ||||||
this.numOfData = 0; | ||||||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음 가급적이면 숫자 상수도 private static final로 빼서 사용하는 편이 좋지 않을까 싶어요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추가로... public ArrayList(int initialSize) {
this.numbers = new int[initialSize];
} 이런 식으로 만들고 생성자 오버로딩을 통해서 public ArrayList() {
this(INITIAL_SIZE);
} 와 같은 식으로 구현해봤을 것 같네요. |
||||||
} | ||||||
|
||||||
public ArrayList(int[] numbers, int numOfData) { | ||||||
this.numbers = numbers; | ||||||
this.numOfData = numOfData; | ||||||
} | ||||||
Comment on lines
+13
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 살짝 애매한 생성자 같아요. |
||||||
|
||||||
public void add(int index, Integer element) { | ||||||
if (numOfData > this.numbers.length) { | ||||||
System.out.println("배열이 가득 찼습니다"); | ||||||
// throw new ArrayIndexOutOfBoundsException(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안쓰는 주석은 제거해주세요~ |
||||||
} | ||||||
this.numbers[index] = element; | ||||||
numOfData++; | ||||||
} | ||||||
Comment on lines
+18
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드 확실한가요?
|
||||||
|
||||||
public void add(Integer element) { | ||||||
if (numOfData > this.numbers.length) { | ||||||
System.out.println("배열이 가득 찼습니다"); | ||||||
} | ||||||
this.numbers[numOfData] = element; | ||||||
numOfData++; | ||||||
} | ||||||
Comment on lines
+27
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 마찬가지입니다. 위의 코멘트를 참고해서 정확한 로직으로 변경해주세요. |
||||||
|
||||||
public int count() { | ||||||
return this.numOfData; | ||||||
} | ||||||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명은 관례상 |
||||||
|
||||||
public Integer get(int index) { | ||||||
return this.numbers[index]; | ||||||
} | ||||||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
||||||
public Integer remove(int index) { | ||||||
Integer data = this.numbers[index]; | ||||||
|
||||||
for (int i = index; i < this.numOfData - 1; i++) { | ||||||
this.numbers[i] = this.numbers[i+1]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
this.numOfData--; | ||||||
return data; | ||||||
} | ||||||
Comment on lines
+43
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 데이터가 없다면 0을 주겠네요? |
||||||
|
||||||
public int size() { | ||||||
return this.numbers.length; | ||||||
} | ||||||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응? 이거 어떻게보면 맞는 것 같기도 하고 아닌 것 같기도 합니다. |
||||||
|
||||||
public static void main(String[] args) { | ||||||
List<Integer> list = new ArrayList(); | ||||||
|
||||||
// 데이터 삽입 | ||||||
list.add(1); | ||||||
list.add(2); | ||||||
list.add(3); | ||||||
list.add(4); | ||||||
|
||||||
// 저장된 데이터 출력 | ||||||
System.out.println("현재 데이터의 수: " + list.count()); | ||||||
|
||||||
for (int i = 0; i < list.count(); i++) { | ||||||
System.out.println(list.get(i)); | ||||||
} | ||||||
|
||||||
list.remove(2); | ||||||
list.remove(3); | ||||||
|
||||||
System.out.println("삭제 후 데이터의 수: " + list.count()); | ||||||
|
||||||
for (int i = 0; i < list.count(); i++) { | ||||||
System.out.println(list.get(i)); | ||||||
} | ||||||
|
||||||
System.out.println("삭제 후 배열 크기: " + list.size()); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.codesquad.datastructurestudy.list.arraylist; | ||
|
||
public interface List<T> { | ||
|
||
void add(T element); | ||
void add(int index, T element); | ||
|
||
int count(); | ||
|
||
T get(int index); | ||
|
||
T remove(int index); | ||
|
||
int size(); | ||
|
||
// int indexOf(T o); | ||
// int lastIndexOf(T o); | ||
|
||
// ListIterator listIterator(); | ||
// ListIterator listIterator(int index); | ||
|
||
// T set(int index, T element); | ||
|
||
// void sort(Comparator c); | ||
|
||
// List subList(int fromIndex, int toIndex); | ||
Comment on lines
+16
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅋㅋㅋ 나중에 구현할 사항들인가요? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.codesquad.datastructurestudy.list.arraylist; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class ListMain { | ||
|
||
public static void main(String[] args) { | ||
List<Integer> list = new ArrayList<>(); | ||
for (int i = 0; i < 9; i++) { | ||
list.add(i,i+1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전반적으로 java coding convention이 잘 지켜지지 않는 모습이네요. |
||
} | ||
|
||
int sum = 0; | ||
for (int i = 0; i < list.size(); i++) { | ||
sum += list.get(i); | ||
} | ||
System.out.println("리스트의 합계는 : " + sum); | ||
|
||
List list1 = new ArrayList(); | ||
|
||
for (int i = 0; i < list.size(); i++) { | ||
if (!(list.get(i) % 3 == 0 || list.get(i) % 2 == 0)) { | ||
list1.add(list.get(i)); | ||
} | ||
} | ||
|
||
Iterator<Integer> iterator = list1.iterator(); | ||
while (iterator.hasNext()) { | ||
System.out.println(iterator.next()); | ||
} | ||
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enhanced for loop를 사용해도 됩니다. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 int overflow를 조심하셔야해요.
https://endorphin0710.tistory.com/112
여길 참고해보세요.