avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

704. Two -point search

  2024-01-01        
字数统计: 194字   |   阅读时长: 1min

https://leetcode.cn/leetbook/read/learning-algorithms-with-leetcode/xsshgi/
Not difficult,Just recursive to take a note

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def search(self, nums: List[int], target: int) -> int:
def recursion(nums, left, right, target):
if left > right:
# Recursive end
return -1
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return recursion(nums, left, right, target)

left = 0
right = len(nums) - 1
return recursion(nums, left, right, target)

https://leetcode.cn/leetbook/read/learning-algorithms-with-leetcode/xssbke/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
# 1 if num is lower than the picked number
# otherwise return 0
# def guess(num: int) -> int:

class Solution:
def guessNumber(self, n: int) -> int:
def recursion(nums, left, right):
if left > right:
# Recursive end
return -1
mid = left + (right - left) // 2
if guess(mid) == 0:
return mid
elif guess(mid) == 1:
left = mid + 1
else:
right = mid - 1
return recursion(nums, left, right)

left = 1
right = n
return recursion(n, left, right)
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
Python beat98.40% collectionsofCounter method!
977.Orderly array square
目录
ahoh, this article has no catalog.

153 篇 | 133k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号