File tree 5 files changed +87
-16
lines changed
find-minimum-in-rotated-sorted-array
longest-substring-without-repeating-characters
5 files changed +87
-16
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // 시간 : O(logN) 공간 : O(1)
3
+ // 이분탐색.
4
+ fun findMin (nums : IntArray ): Int {
5
+ var left = 0
6
+ var right = nums.lastIndex
7
+
8
+ while (left <= right){
9
+ val mid = (left + right)/ 2
10
+ when {
11
+ nums[mid- 1 ] > nums[mid] -> {
12
+ return nums[mid]
13
+ }
14
+ nums[0 ] < nums[mid] -> {
15
+ left = mid + 1
16
+ }
17
+ else -> {
18
+ right = mid - 1
19
+ }
20
+ }
21
+ }
22
+ return nums[0 ]
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // 시간 : O(n)
3
+ // 세트에 head.val 값을 추가하면서 동일한 값이 있는지 체크. 동일한 값이 존재하면 순회한다고 판단.
4
+ fun hasCycle (head : ListNode ? ): Boolean {
5
+ val set = mutableSetOf<Int >()
6
+ var next = head
7
+ while (next != null ) {
8
+ if (set.contains(next.`val `)) {
9
+ return true
10
+ }
11
+ set.add(next.`val `)
12
+ next = next.next
13
+ }
14
+ return false
15
+ }
16
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun maxProduct (nums : IntArray ): Int {
3
+ var max = 1
4
+ var min = 1
5
+ var result = nums[0 ]
6
+ nums.forEach { num ->
7
+ val v1 = min * num
8
+ val v2 = max * num
9
+ min = min(min(v1, v2), num)
10
+ max = max(max(v1, v2), num)
11
+ result = max(max, result)
12
+ }
13
+ return result
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun minWindow (s : String , t : String ): String {
3
+ if (s.length < t.length) {
4
+ return " "
5
+ }
6
+ val containIndexList = mutableListOf<Int >()
7
+ for (i in s.indices) {
8
+ if (t.contains(s[i])) {
9
+ containIndexList.add(i)
10
+ }
11
+ }
12
+ var answer = " "
13
+ val regex =
14
+ t.toCharArray().joinToString(separator = " " , prefix = " ^" , postfix = " .+$" ) { """ (?=.*${it} )""" }.toRegex()
15
+ for (i in 0 .. containIndexList.size - t.length) {
16
+ val startIndex = containIndexList[i]
17
+ for (l in t.length.. containIndexList.size - i) {
18
+ val endIndex = containIndexList[(i + l) - 1 ] + 1
19
+ val subStr = s.substring(startIndex, endIndex)
20
+ if (regex.containsMatchIn(subStr)) {
21
+ if (answer.isEmpty()) {
22
+ answer = subStr
23
+ } else if (subStr.length < answer.length) {
24
+ answer = subStr
25
+ }
26
+ break
27
+ }
28
+ }
29
+ }
30
+ return answer
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments