基础的 api 还是不够熟悉啊 5112. 十六进制魔术数字 class Solution { public: char *lltoa(long long num, char *str, int radix) { const char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned long long unum; int i = 0, j, k; if(radix == 10 && num <…
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle…
If you understand the comments below, never will you make mistakes with binary search! thanks to A simple CPP solution with lower_bound and C++ O(logn) Binary Search that handles duplicate, thanks to phu1ku 's answer on the second post. http://en.cpp…
Description Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: Input: [,,,], Output: Example…
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in th…
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5…
凉凉,看来想做好一个题还不容易啊... 有点难受... 1.看看题目吧 Given a sorted array and a target value, return the index if the target is found. If not,return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 给定一个有序数组,依旧是二分查找,…
题目描述: 方法一:二分法 class Solution: def findSpecialInteger(self, arr: List[int]) -> int: span = len(arr)//4 + 1 for i in range(0,len(arr),span): a = bisect.bisect_left(arr,arr[i]) b = bisect.bisect_right(arr,arr[i]) if b - a >= span: return arr[i] return…
题目描述: 方法一:排序O(Nlogn) class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda x:(x[0],-x[1])) res,rmax = len(intervals),intervals[0][1] for i in range(1,len(intervals)): if i > 0 and intervals[…
题目描述: 方法一:动态规划 O(N^3) class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: n = len(arr) for i in range(1,n): for j in range(n): arr[i][j] = arr[i][j] + min(arr[i-1][0:j] + arr[i-1][j+1:]) return min(arr[-1]) 优化:O(n^2logn) class S…
题目描述: 自己的提交: # """ # This is Sea's API interface. # You should not implement it, or speculate about its implementation # """ #class Sea(object): # def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool: # #clas…
题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: List[int]) -> int: dp = [[0, 0]for _ in range(nodes)] p = collections.defaultdict(list) for i, v in enumerate(parent): p[v].append(i) for i in range(node…
题目描述: 自己的提交: class Solution: def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]: res = [] for i in intervals: if i[0] >= toBeRemoved[1] or i[1] <= toBeRemoved[0]: res.append(i) else: if i[0] < toBeR…
自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[2:] res = "" for i in num: ": i = "O" ": i = "I" i = i.upper() if i not in {"A", "B", "C&q…