Skip to content

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

Merged
merged 6 commits into from
Apr 20, 2021
Merged

LeetCode 讀書會第 25 次聚會 #18

merged 6 commits into from
Apr 20, 2021

Conversation

ytyubox
Copy link
Member

@ytyubox ytyubox commented Apr 6, 2021

LeetCode 讀書會第 25 次聚會

  1. 項目: 第 25 次聚會
  2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法
  3. 時間: 4/20 (二) 20:00 ~ 21:00
  4. 地點: google meet 線上 (前 10 分鐘預備鏈接)
  5. 解題項目: Binary search
  6. 共筆: GitHub LeetCode 讀書會第 25 次聚會 #18
  7. 備註: 上次有稍微聊一下 658 題目,這次會重頭開始聊658,會強調如何理解題目、題目預期的答案,與如何實作。

@ytyubox ytyubox changed the title Create 25 20210420.md LeetCode 讀書會第 25 次聚會 Apr 6, 2021
}
};
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```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
}

Copy link
Member Author

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/ 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamTienz 你要不要解釋一下你怎麼想的?可以幫助你思考喔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytyubox
OK 大概是這樣

  1. 92行開始 : 先用binary search看能不能在nums找到target,找不到=>return ans (這時ans為初始值[-1,-1])
  2. 如果有找到target,將找到的target的index為基準,把nums拆成兩邊 (兩邊都含target index)
  3. 兩邊分別執行binary search
  4. ans[0] = 左邊找到nums(index)為 && nums(target - 1)不為target,此時index = First Position of Target
  5. ans[1] = 右邊找到nums(index)為 && nums(target + 1)不為target,此時index = Last Position of Target
  6. return ans

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamTienz
看起來是 func findLeft(_ , _)findRight(_ , _) 裡面有點問題

Copy link
Contributor

@louis222220 louis222220 left a 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

Copy link
Contributor

@louis222220 louis222220 left a 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

@louis222220
Copy link
Contributor

louis222220 commented Apr 14, 2021

Github 的 suggestion 是不是怪怪的?
「```」 會被當成是 suggestion 的結束,而無法辨別 code block 的結束

@ytyubox
Copy link
Member Author

ytyubox commented Apr 14, 2021

Github 的 suggestion 是不是怪怪的?
「```」 會被當成是 suggestion 的結束,而無法辨別 code block 的結束

@louis222220 這個我知道,這個沒有辦法在 code review 階段解決,我目前的解決辦法是在 每次的 meetup 結束之後,統一在 hackmd 手動處理,在 commit 一次

我沒有想到 bot 或是其他更好的辦法。

ytyubox and others added 2 commits April 20, 2021 21:15
Co-authored-by: Louis <louis222220@gmail.com>
@ytyubox
Copy link
Member Author

ytyubox commented Apr 20, 2021

關於 silding window 的文章
https://blog.techbridge.cc/2019/09/28/leetcode-pattern-sliding-window/

@ytyubox ytyubox merged commit fec5cdc into main Apr 20, 2021
@ytyubox ytyubox deleted the meetup-25 branch April 20, 2021 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants