-
Notifications
You must be signed in to change notification settings - Fork 2
LeetCode 讀書會第 25 次聚會 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
} | ||
}; | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```swift | |
//swift by SamTienz | |
//上次讀書會聽了神Q的想法先寫了一些,後來再參考louis222220的,自己再寫一遍,不過有些test case過不了 😢 | |
func searchRange(_ nums: [Int], _ target: Int) -> [Int] { | |
var left = 0, right = nums.count - 1 | |
var ans = [-1, -1] | |
if nums.count == 0 {return ans} | |
func findLeft(_ left:Int, _ right:Int) -> Int { | |
if left >= right { return left } | |
let mid = (right - left) / 2 + left | |
if nums[mid] == target { | |
if nums[mid - 1] != target { return mid } | |
else { return findLeft(left, mid - 1) } | |
}else{ | |
return findLeft(mid + 1, right) | |
} | |
} | |
func findRight(_ left:Int, _ right:Int) -> Int { | |
if right <= left { return right } | |
let mid = (right - left) / 2 + left | |
if nums[mid] == target { | |
if nums[mid + 1] != target { return mid } | |
else { return findLeft(mid + 1, right) } | |
}else{ | |
return findLeft(left, mid - 1) | |
} | |
} | |
while true { | |
if left >= right { return ans } //Wrong Answer [1] 1 | |
let mid = (right - left) / 2 + left | |
if nums[mid] == target{ //找到了 切兩邊 | |
ans[0] = findLeft(left, mid) //找左 | |
ans[1] = findRight(mid, right) //找右 | |
return ans | |
}else if nums[mid] > target{ | |
right = mid - 1 | |
}else{ | |
left = mid + 1 | |
} | |
// if left >= right { return ans } Wrong Answer [1,4] 4 | |
} | |
return ans | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SamTienz
幫補充 這個題目順序上有一點問題,其實這題是 MEDIUM 34
* Template III
* Article Binary Search Template III
* MEDIUM 34 Find First and Last Position of Element in Sorted Array https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SamTienz 你要不要解釋一下你怎麼想的?可以幫助你思考喔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ytyubox
OK 大概是這樣
- 92行開始 : 先用binary search看能不能在nums找到target,找不到=>return ans (這時ans為初始值[-1,-1])
- 如果有找到target,將找到的target的index為基準,把nums拆成兩邊 (兩邊都含target index)
- 兩邊分別執行binary search
- ans[0] = 左邊找到nums(index)為 && nums(target - 1)不為target,此時index = First Position of Target
- ans[1] = 右邊找到nums(index)為 && nums(target + 1)不為target,此時index = Last Position of Target
- return ans
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SamTienz
看起來是 func findLeft(_ , _)
與 findRight(_ , _)
裡面有點問題
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution for Pow(x, n) in TS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iterative solution for Pow(x, n) in TS
Github 的 suggestion 是不是怪怪的? |
@louis222220 這個我知道,這個沒有辦法在 code review 階段解決,我目前的解決辦法是在 每次的 meetup 結束之後,統一在 hackmd 手動處理,在 commit 一次 我沒有想到 bot 或是其他更好的辦法。 |
Co-authored-by: Louis <louis222220@gmail.com>
關於 silding window 的文章 |
LeetCode 讀書會第 25 次聚會