sean.jin
Spark Code Blog
sean.jin
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발공부
      • Kotlin
      • LeetCode
      • Algorithm
      • React
    • 주식차트
    • 책리뷰
    • 유틸리티

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 자기개발
  • 주식책리뷰
  • 주식입문자
  • 트리플 위칭데이
  • 쿼드러플위칭데이
  • 경제
  • 책추천
  • 초보
  • 변동성
  • 네마녀의날
  • 부의 추월차선
  • 아빠와 딸의 주식투자 레슨
  • 책리뷰
  • 주식투자
  • 책
  • 오

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
sean.jin

Spark Code Blog

[Kotlin] LeetCode: Intersection of Two Arrays IISolution 풀이
개발공부/LeetCode

[Kotlin] LeetCode: Intersection of Two Arrays IISolution 풀이

2021. 6. 11. 15:52
반응형

문제

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

 

nums1과 nums2 에서 겹치는 원소를 모아서 intArray로 반환해야 합니다.

 

예시

풀이

class Solution {
    fun intersect(nums1: IntArray, nums2: IntArray): IntArray? {
    //기본값들을 initialize 해줍니다
    val list: MutableList<Int> = ArrayList()
    var i = 0
    var j = 0
    
    //순서대로 나열합니다.
    Arrays.sort(nums1)
    Arrays.sort(nums2)
    
    //nums1의 사이즈가 i보다 작을때까지 혹은 j가 nums2의 size보다 작을때 까지 반복합니다
    while (i < nums1.size && j < nums2.size) {
        if (nums1[i] == nums2[j]) {
            list.add(nums1[i])
            i++//i 와 j 는 1씩 증가합니다. 
            j++
        } else if (nums1[i] < nums2[j]) { //sort를 했기때문에 차례로 커집니다.  
            i++
        } else {
            j++
        }
    }
    return list.stream().mapToInt { k: Int? -> k!! }.toArray()
}
}

 

 

반응형

'개발공부 > LeetCode' 카테고리의 다른 글

[Kotlin] String에서 반복되지않은 char찾기 - LeetCode: First Unique Character in a String  (0) 2021.06.20
[Kotlin] LeetCode: PlusOne 쉬운 풀이 해설  (0) 2021.06.14
[Kotlin] Array 에 같은 숫자 찾기, HashMap활용 풀이 - LeetCode: Single Number  (0) 2021.06.10
[Kotlin] Array에 반복되는 아이템 삭제하기 - ArrayList distinct - LeetCode: Contains Duplicate - 풀이 과정  (0) 2021.06.08
[Kotlin] LeetCode - Best Time to Buy and Sell Stock IISolution - 풀이과정  (0) 2021.06.05
    '개발공부/LeetCode' 카테고리의 다른 글
    • [Kotlin] String에서 반복되지않은 char찾기 - LeetCode: First Unique Character in a String
    • [Kotlin] LeetCode: PlusOne 쉬운 풀이 해설
    • [Kotlin] Array 에 같은 숫자 찾기, HashMap활용 풀이 - LeetCode: Single Number
    • [Kotlin] Array에 반복되는 아이템 삭제하기 - ArrayList distinct - LeetCode: Contains Duplicate - 풀이 과정
    sean.jin
    sean.jin
    앱개발, 알고리즘, JS, Kotlin, 미국 취업준비

    티스토리툴바