Coder Social home page Coder Social logo

algo-baekjoon-kotlin's Introduction

-

가끔 쓰는 문법

입출력

val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.out))

출력으로 시간초과나면 => StringBuilder 등 사용해섯 한 방에 출력

리스트 정렬

        val list: MutableList<Rank> = ArrayList<Rank>()
        score.forEachIndexed{ index, i -> list.add(Rank(index, i)) }
        list.sortWith (Comparator{ a, b ->
            when {
                a.score == b.score -> a.index - b.index
                else -> b.score - a.score
            }
        })

2차원 배열 초기화

val reshapeMatrix: Array<IntArray> = Array(r) { i -> IntArray(c) { j -> 0 } }

kotlin에서 3항 연산자(ternary operator)

  • 따로 없어서 if, else 나 when으로 처리해야 함
var prev = if (i == 0) 0 else tmp[i - 1]
var next = if (i == flowerbed.size - 1) 0 else tmp[i + 1]

empty list 생성 및 Comparator를 이용한 정렬

        val reorderLogList: MutableList<ReorderLog> = mutableListOf()
        reorderLogList.sortWith { a, b ->
            if (a == b) a.log.compareTo(b.log)
            else a.log.compareTo(b.log)
        }      
        reorderLogList.sortWith(
            Comparator { a, b ->
                if (a == b) a.log.compareTo(b.log)
                else a.log.compareTo(b.log)
            }
        )

Comparator 사용

  • 조건 여러 개면 when 으로 처리
    val customComparator = Comparator<Int> { a, b ->
        when {
            (a == null && b == null) -> 0
            (a == null) -> -1
            else -> 1
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.