leetcode练习
have a good day.
这个作者很懒,什么都没留下…
展开
-
leetcode — 1237. 找出给定方程的正整数解
一、暴力法遍历class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: res = [] f = customfunction.f for x in range(1, z + 1): for y in range(1, z + 1): if f(x原创 2020-09-23 20:19:03 · 129 阅读 · 0 评论 -
leetcode — 山脉数组的峰顶索引
执行用时:40 ms内存消耗:14.5 MBclass Solution: def peakIndexInMountainArray(self, arr: List[int]) -> int: left = 0 right = len(arr) - 1 while left <= right: mid = left + (right - left) // 2 if arr[mid-1] &原创 2020-09-22 17:23:40 · 137 阅读 · 0 评论 -
leecode — 找到比目标字母大的最小字母(二分查找)
看到题目想到了二分查找的运行时间为O(logn)class Solution:def nextGreatestLetter(self, letters: List[str], target: str) -> str: left = 0 right = len(letters) - 1 while left <= right: mid = left + (right-left) // 2 if le原创 2020-09-22 17:02:42 · 61 阅读 · 0 评论 -
leetcode — 两数之和
第一次做,优化不够好。方法一:暴力遍历class Solution(object): def twoSum(self, nums, target): for i in range(len(nums)): # 这里用i+1:因为遍历的是同一个列表,不存在遍历元素缺失的问题, # 从i+1开始反而会让程序运行更快速 for j in range(i + 1, len(nums)):原创 2020-08-06 20:44:03 · 92 阅读 · 0 评论