본문 바로가기
728x90

Kotlin10

[알고리즘] 투포인터(Kotlin) 투 포인터 알고리즘이란 리스트에 순차적으로 접근해야 할 때 2개의 점의 위치를 기록하면서 처리하는 알고리즘이다. 사용 예제 1. 특정한 합을 가지는 부분 연속 수열 찾기 부분 연속 수열의 시작점(start)과 끝점(end)의 위치를 기록 특정한 부분합을 M이라 가정할 때 알고리즘은 아래와 같다. 시작점(start)과 끝점(end)이 첫 번째 원소의 인덱스(0)를 가리키도록 한다. 현재 부분합이 M과 같다면 카운트한다. 현재 부분합이 M보다 작으면 끝점(end)를 1 증가시킨다. 현재 부분합이 M보다 크거나 같으면 시작점(start)을 1 증가시킨다. 모든 경우를 확인할 때까지 2번부터 4번 과정을 반복한다. fun getTwoPointerCount(sum: Int) { val data = arrayOf(.. 2021. 5. 24.
[Kotlin] LiveData를 유연하게 사용하는 map, switchMap map inline fun LiveData.map(crossinline transform: (X) -> Y): LiveData = Transformations.map(this) { transform(it) } 내부에서 Transformations.map을 사용한다. Transformations.map 함수는 아래와 같습니다. @MainThread @NonNull public static LiveData map( @NonNull LiveData source, @NonNull final Function mapFunction) { final MediatorLiveData result = new MediatorLiveData(); result.addSource(source, new Observer() { @Ov.. 2021. 1. 1.
[Sunflower] Sunflower 프로젝트 살펴보기(using Jetpack) Google Developer에서는 Architecture Components를 위한 다양한 양질의 training 자료들을 안내한다. Github Sample, Codelabs, Google Develper Blog, YouTube Videos etc... 이렇게 다양하게 안내한다. 더욱 밀도 깊은 스터디를 위해 샘플을 꼼꼼하게 분석하기로 결심하였다. 그중에서 Github Sample의 Sunflower 프로젝트에 대해서 분석하고자 한다. (자료를 참고하여 스터디한 자료이므로 오류가 있을 수 있습니다.) Sunflower 프로젝트 자료는 아래 링크에 있습니다. https://github.com/android/sunflower android/sunflower A gardening app illustrat.. 2020. 12. 30.
[kotlin] 수 반올림하기 val num = 123.456 println(num.roundToInt()) println(num.roundToLong()) Output 123 123 2020. 12. 19.
728x90