04 2020 档案

摘要:方法:二分查找 def binary_search(mountain, target, l, r, key=lambda x: x): target = key(target) while l <= r: mid = (l + r) // 2 cur = key(mountain.get(mid)) 阅读全文
posted @ 2020-04-30 09:01 oldby 阅读(171) 评论(0) 推荐(0)
摘要:题目描述: 方法一:分组异或 class Solution { public int[] singleNumbers(int[] nums) { int sum = 0; int [] res = new int[2]; for(int num:nums){ sum ^= num; } int lo 阅读全文
posted @ 2020-04-28 12:43 oldby 阅读(161) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def maxScore(self, s: str) -> int: d = {"0":0,"1":0} for i in s: d[i] += 1 res = 0 l,r = {"0":0,"1":0},d for v,i in enume 阅读全文
posted @ 2020-04-27 09:58 oldby 阅读(157) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交:O(N) class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: copy1 = [0] + cardPoints[:] copy2 = [0] + cardPoints[::-1] 阅读全文
posted @ 2020-04-27 09:54 oldby 阅读(140) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution: def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: a,s,m=[(0,0)],[nums[0][0]],len(nums) while a: b=[] for i,j 阅读全文
posted @ 2020-04-27 09:49 oldby 阅读(130) 评论(0) 推荐(0)
摘要:题目描述: 方法一:动态规划 O(n) class Solution: def constrainedSubsetSum(self, nums, k: int): dp = [0]*len(nums) dp[0] = nums[0] arr = [(nums[0],0)] for i in rang 阅读全文
posted @ 2020-04-26 17:55 oldby 阅读(143) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: def helper(num,node,temp): if node.left: helper(num+node.lef 阅读全文
posted @ 2020-04-26 09:36 oldby 阅读(144) 评论(0) 推荐(0)
摘要:题目描述: 方法一:递归 最坏O(N^2) class Solution: def verifyPostorder(self, postorder: List[int]) -> bool: def recur(i,j): if i >= j:return True p = i while posto 阅读全文
posted @ 2020-04-25 23:07 oldby 阅读(142) 评论(0) 推荐(0)
摘要:题目描述: 方法一:归并排序 O(nlogn) class Solution: def reversePairs(self, nums: List[int]) -> int: def mergeSort(l=0, r=len(nums)):#左闭右开区间 if r - l > 1: mid = (l 阅读全文
posted @ 2020-04-24 14:58 oldby 阅读(125) 评论(0) 推荐(0)
摘要:题一: 提交:层序遍历bfs O(N) O(N) class Solution: def levelOrder(self, root: TreeNode) -> List[int]: if not root : return [] queue = [root] res = [] while queu 阅读全文
posted @ 2020-04-23 21:40 oldby 阅读(138) 评论(0) 推荐(0)
摘要:题目描述: 方法:动态规划 完全背包问题 class Solution: def waysToChange(self, n: int) -> int: coins = [1,5,10,25] dp = [0] * (n+1) dp[0] = 1 for coin in coins: for i in 阅读全文
posted @ 2020-04-23 20:25 oldby 阅读(178) 评论(0) 推荐(0)
摘要:题目描述: 方法一:递归 class Solution: def mirrorTree(self, root: TreeNode) -> TreeNode: if not root:return root.left,root.right = self.mirrorTree(root.right),s 阅读全文
posted @ 2020-04-21 19:41 oldby 阅读(108) 评论(0) 推荐(0)
摘要:题目描述: 方法:动态规划: class Solution: def numOfArrays(self, n: int, m: int, k: int) -> int: mod = 10 ** 9 + 7 dp = [[[0] * (k + 1) for _ in range(m + 1)] for 阅读全文
posted @ 2020-04-21 11:17 oldby 阅读(233) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: c = collections.Counter() ans = 0 st = "croak" for s in croakOfFrog 阅读全文
posted @ 2020-04-20 20:50 oldby 阅读(345) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: table = set() table_l = [] food = set() food_l = [] f 阅读全文
posted @ 2020-04-20 20:45 oldby 阅读(209) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: class Solution: def reformat(self, s: str) -> str: digit = [] alpha = [] for i in s: if i.isdigit(): digit.append(i) elif i.isalpha(): al 阅读全文
posted @ 2020-04-20 20:29 oldby 阅读(169) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution: def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int: if n1 == 0: return 0 s1cnt, index, s2cnt = 0, 0, 0 阅读全文
posted @ 2020-04-20 13:09 oldby 阅读(129) 评论(0) 推荐(0)
摘要:题目描述: 解法:动态规划 class Solution: def numberOfArrays(self, s: str, k: int) -> int: n = len(s) dp = [0] * (n + 1) dp[0] = 1 mod = 10**9 +7 for i in range(1 阅读全文
posted @ 2020-04-19 12:57 oldby 阅读(266) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def getHappyString(self, n: int, k: int) -> str: strlist = [] def helper(n, s): if len(s) == n: strlist.append(s) return for 阅读全文
posted @ 2020-04-19 10:29 oldby 阅读(382) 评论(0) 推荐(0)
摘要:题目描述: 解法:贪心,每次减去<k的最大斐波那契数 class Solution: def findMinFibonacciNumbers(self, k: int) -> int: fib = [1, 1] while fib[-1] <= k: fib.append(fib[-1] + fib 阅读全文
posted @ 2020-04-19 10:06 oldby 阅读(211) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def minStartValue(self, nums: List[int]) -> int: res = 1 s = 0 for i in nums: s += i res = min(res,s) if res >= 0: return 1 阅读全文
posted @ 2020-04-19 09:50 oldby 阅读(114) 评论(0) 推荐(0)
摘要:题目描述: 方法一:O(MN) O(M) class Solution: def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool: def equal(A,B): if not B:return True if not A or A.va 阅读全文
posted @ 2020-04-17 15:11 oldby 阅读(94) 评论(0) 推荐(0)
摘要:方法一:迭代 class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; ListNode tmp = null; while(cur!=null){ 阅读全文
posted @ 2020-04-16 16:29 oldby 阅读(127) 评论(0) 推荐(0)
摘要:题目描述: java:快慢指针: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-04-15 23:50 oldby 阅读(286) 评论(0) 推荐(0)
摘要:题目描述: 方法一:dfs O(MN) O(MN) class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m,n = len(matrix),len(matrix[0]) dist = 阅读全文
posted @ 2020-04-15 23:43 oldby 阅读(202) 评论(0) 推荐(0)
摘要:方法一:栈 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solut 阅读全文
posted @ 2020-04-14 22:29 oldby 阅读(143) 评论(0) 推荐(0)
摘要:题目描述: 方法一:逻辑判断法:O(N) class Solution: def isNumber(self, s: str) -> bool: s = s.strip() met_dot = met_e = met_digit = False for i, char in enumerate(s) 阅读全文
posted @ 2020-04-14 22:02 oldby 阅读(156) 评论(0) 推荐(0)
摘要:题目描述: java: package test; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; 阅读全文
posted @ 2020-04-14 07:15 oldby 阅读(207) 评论(0) 推荐(0)
摘要:题目描述: 方法: class Solution: def intersection(self, start1, end1, start2, end2): x1, y1, x2, y2, x3, y3, x4, y4 = *start1, *end1, *start2, *end2 det = la 阅读全文
posted @ 2020-04-13 10:10 oldby 阅读(122) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution: def numOfWays(self, n: int) -> int: mod = int(1e9+7) s = ['ryr','ryg','rgr','rgy','yrg','ygr','ygy','yry','gry','grg','gyr' 阅读全文
posted @ 2020-04-12 20:09 oldby 阅读(297) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def entityParser(self, text: str) -> str: text = text.replace("&quot;","\"") text = text.replace("&apos;","\'") text = te 阅读全文
posted @ 2020-04-12 19:46 oldby 阅读(162) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: l = [i for i in range(1,m+1)] ans = [] for i in querie 阅读全文
posted @ 2020-04-12 19:33 oldby 阅读(104) 评论(0) 推荐(0)
摘要:题目描述: 自己的提交: class Solution: def stringMatching(self, words: List[str]) -> List[str]: def strStr(haystack: str, needle: str) -> int: if not needle:ret 阅读全文
posted @ 2020-04-12 19:27 oldby 阅读(147) 评论(0) 推荐(0)
摘要:javaO(N) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class So 阅读全文
posted @ 2020-04-11 23:19 oldby 阅读(146) 评论(0) 推荐(0)
摘要:题目描述: 方法一:动态规划+二分搜索O(knlogn) O(kn) class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(k, n): if (k, n) not in memo: if n 阅读全文
posted @ 2020-04-11 20:08 oldby 阅读(261) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: class Solution: def cuttingRope(self, n: int) -> int: ans = 0 for m in range(2,max(n//2,3)): j = 1 a = n//m b = n%m for _ in range(b): j 阅读全文
posted @ 2020-04-09 11:40 oldby 阅读(289) 评论(0) 推荐(0)
摘要:题目描述: 方法一:dfs/bfs class Solution: def movingCount(self, m: int, n: int, k: int) -> int: def digitsum(n): ans = 0 while n: ans += n %10 n//=10 return a 阅读全文
posted @ 2020-04-08 23:28 oldby 阅读(196) 评论(0) 推荐(0)
摘要:题目描述: 方法:从后向前dp class Solution(object): def stoneGameIII(self, A): n = len(A) dp = [-float('inf')] * n for i in range(n-1,-1,-1): dp[i] = max(dp[i], s 阅读全文
posted @ 2020-04-05 19:32 oldby 阅读(175) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交 python: class Solution: def longestDiverseString(self, a: int, b: int, c: int) -> str: res = "" l = [["a", a], ["b", b], ["c", c]] l.sort( 阅读全文
posted @ 2020-04-05 19:07 oldby 阅读(148) 评论(0) 推荐(0)
摘要:题目描述: 排序加后缀和 class Solution: def maxSatisfaction(self, satisfaction: List[int]) -> int: satisfaction.sort() t_s = 0 res = 0 for i in satisfaction[::-1 阅读全文
posted @ 2020-04-05 10:31 oldby 阅读(178) 评论(0) 推荐(0)
摘要:题目描述: 统计字符奇偶次数即可 class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): return False c = collections.Counter(s) o = sum(i % 2 阅读全文
posted @ 2020-04-05 10:27 oldby 阅读(100) 评论(0) 推荐(0)
摘要:题目描述: class Solution: def checkOverlap(self, radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool: a = max(0, x1 - x 阅读全文
posted @ 2020-04-05 10:23 oldby 阅读(229) 评论(0) 推荐(0)
摘要:题目描述: 恢复内容开始 恢复内容开始 选择排序 O(N^2) class Solution { public int[] sortArray(int[] nums) { int len = nums.length; for(int i = 0; i < len -1; i++){ int minI 阅读全文
posted @ 2020-04-03 11:54 oldby 阅读(112) 评论(0) 推荐(0)
摘要:恢复内容开始 选择排序 O(N^2) class Solution { public int[] sortArray(int[] nums) { int len = nums.length; for(int i = 0; i < len -1; i++){ int minIndex = i; for 阅读全文
posted @ 2020-04-03 09:32 oldby 阅读(129) 评论(0) 推荐(0)
摘要:题目描述: java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class 阅读全文
posted @ 2020-04-02 17:06 oldby 阅读(121) 评论(0) 推荐(0)
摘要:题目描述: 方法一:java class Solution { public String replaceSpace(String s) { StringBuilder res = new StringBuilder(); for(Character c: s.toCharArray()){ if( 阅读全文
posted @ 2020-04-02 16:55 oldby 阅读(165) 评论(0) 推荐(0)
摘要:解法一:O(MN)O(MN))Java版 class Solution { public void gameOfLife(int[][] board) { int [] neighbors = {0,1,-1}; int rows = board.length; int cols = board[0 阅读全文
posted @ 2020-04-02 16:45 oldby 阅读(188) 评论(0) 推荐(0)
摘要:题目描述: 方法一:pythonO(N)O(1) class Solution: def maxDepthAfterSplit(self, seq: str) -> List[int]: d = 0 ans = [] for s in seq: if s == "(": d += 1 ans.app 阅读全文
posted @ 2020-04-01 12:13 oldby 阅读(193) 评论(0) 推荐(0)
摘要:恢复内容开始 题目描述:给定一个数组,返回排序后的数组。 python 归并排序: class Solution: def sortArray(self, nums: List[int]) -> List[int]: def merge_sort(nums): if len(nums) <= 1: 阅读全文
posted @ 2020-04-01 11:05 oldby 阅读(176) 评论(0) 推荐(0)