본문 바로가기
코딩테스트

[kotlin] leetcode - Binary Search

by 얘리밍 2022. 4. 26.
320x100
728x90

704. Binary Search : https://leetcode.com/problems/binary-search/submissions/

 

Binary Search - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    fun search(nums: IntArray, target: Int): Int {
            //nums에 특정 값 있는 지 확인
            var check = nums.contains(target) 
            if(check == true){
                var idx = nums.indexOf(target) //배열의 인덱스를 확인 
                return idx 
            }
            else{
                return -1
            }  
        }
}
Runtime: 324 ms, faster than 74.40% of Kotlin online submissions for Binary Search.
Memory Usage: 40.1 MB, less than 74.40% of Kotlin online submissions for Binary Search.

 

1)  contains : 특정 값 포함 여부를 확인

2) indexOf : 특정 데이터 인덱스 값을 확인

 

코틀린 공부하기..

728x90
반응형