From 44f9daab4193c250dfe6efe85be6f8d85e6e7d8f Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 20 Feb 2023 08:24:16 +0330 Subject: 35 --- 35/main.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 35/main.py (limited to '35/main.py') diff --git a/35/main.py b/35/main.py new file mode 100755 index 0000000..b78265d --- /dev/null +++ b/35/main.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +from typing import List + + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + begin = 0 + end = len(nums) - 1 + mid = 0 + while begin <= end: + mid = (end + begin) // 2 + if target < nums[mid]: + end = mid - 1 + elif target > nums[mid]: + begin = mid + 1 + else: + return mid + return begin + + +if __name__ == "__main__": + solution = Solution() + print(solution.searchInsert([1, 3, 5, 6], 5)) + print(solution.searchInsert([1, 3, 5, 6], 2)) + print(solution.searchInsert([1, 3, 5, 6], 7)) -- cgit v1.2.3