- 博客(79)
- 收藏
- 关注
原创 187. 重复的DNA序列
class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: res = [] # 滑动窗口 i = 0 j = 10 dic = {} while j <= len(s): if s[i:j] not in dic: dic[s[i:j]] = 1 .
2021-04-29 12:32:39 99
原创 SQL(四)
# Write your MySQL query statement belowSELECT Department.Name AS Department, e1.Name AS Employee, SalaryFROM Employee e1 INNER JOIN DepartmentON e1.DepartmentId = Department.Idwhere 3 >(select count(distinct e2.Salary)from Employee e2where e2..
2021-04-29 12:31:48 103
原创 1013. 将数组分成和相等的三个部分
class Solution: def canThreePartsEqualSum(self, arr: List[int]) -> bool: total = sum(arr) if total % 3 != 0: return False part1 = 0 part2 = 0 part3 = -1 for i in range(len(arr)-1): .
2021-04-29 10:31:33 126
原创 875. 爱吃香蕉的珂珂
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: left = 1 right = sum(piles) while left < right: mid = (left + right) // 2 h = self.check(piles, mid) if h >.
2021-04-29 08:22:16 98
原创 1011. 在 D 天内送达包裹的能力
首先这艘船的重量是固定的, 货物的运输顺序必须按照数组weights的顺序来, 不能打乱顺序. 那么我们可以想到这艘船最小的运载能力必须是所有货物中最重的那一件max(weights), 否则无法完成运输的任务. 这艘船最大的运载能力就是一次性把所有的货都搬走, 就是sum(weights). 所以答案就在区间[max(weights), sum(weights)]之间啦!题目的考点最后落到二分搜索. 这里有一些明显的使用二分搜索的特征:答案区间已经有序;最优解在答案区间内class Solut...
2021-04-29 08:12:14 76
原创 1010. 总持续时间可被 60 整除的歌曲
class Solution: def numPairsDivisibleBy60(self, time: List[int]) -> int: ans = 0 dic = {} for i in range(len(time)): if time[i] % 60 in dic: dic[time[i] % 60] = dic[time[i] % 60] + 1 e.
2021-04-27 12:37:03 85
原创 1007. 行相等的最少多米诺旋转
class Solution: def minDominoRotations(self, A: List[int], B: List[int]) -> int: def check(x): rotations_a = rotations_b = 0 for i in range(n): if A[i] != x and B[i] != x: ..
2021-04-27 11:37:15 96
原创 双栈系列(计算表达式)
class Solution: op_priority = {'+': 0, '-': 0, '*': 1, '/': 1, '%': 1, '^': 2} def calculate(self, s: str) -> int: s = "(" + s.replace(" ", "").replace("(-", "(0-") + ")" n = len(s) # operators & numbers op_.
2021-04-26 22:34:12 288
原创 728. 自除数
class Solution: def selfDividingNumbers(self, left: int, right: int) -> List[int]: def func(num): str_num = str(num) for char in str_num: if char == "0" or num % int(char) != 0: re.
2021-04-26 21:51:04 100
原创 SQL(三)
180. 连续出现的数字暴力三表连接# Write your MySQL query statement belowSELECT DISTINCT(l1.Num) AS ConsecutiveNumsFROM Logs l1, Logs l2, Logs l3WHERE l1.Id = l2.Id - 1 AND l2.Id = l3.Id - 1 AND l1.Num = l2.Num AND l2.Num = l3.Num..
2021-04-26 21:32:37 128
原创 窗口函数+经典topN问题SQL笔记
SQL窗口函数这类问题其实就是常见的:每组最大的N条记录(topN)。问题:查找每个学生成绩最高的2个科目
2021-04-26 21:14:40 370 2
原创 724. 寻找数组的中心下标
class Solution: def pivotIndex(self, nums: List[int]) -> int: nums = [0] + nums + [0] left = 0 right = sum(nums) for i in range(1, len(nums)-1): left = left + nums[i-1] right = right - nums[i..
2021-04-26 15:51:56 94
原创 720. 词典中最长的单词
记录每一个单词的前缀,看前缀是否在集合中,如果前缀都在集合中,则添加单词,最后求最长的最小单词。class Solution(object): def longestWord(self, words): ans = [] wordset = set(words) for word in words: flag = 1 for i in range(1,len(word)): .
2021-04-26 11:51:33 108 1
原创 用逻辑回归来排序
用逻辑回归对商品属性排序背景:对商品质量的排序,假设我们有最近半年的每个商品的的销量,每个商品的收藏数量,每个商品的点击次数三种数据。目标:对商品打分,分数越高越好。 Score=a1∗销量+a2∗收藏数量\ Score=a_1*销量+a_2*收藏数量 Score=a1∗销量+a2∗收藏数量目标是求出 a1,a2\ a_1,a_2 a1,a2衡量好商品的办法:如果商品被点击次数超过10次,就认为是好商品开始排序:商品销量收藏量
2021-04-25 22:41:25 766
原创 777. 在LR字符串中交换相邻字符
class Solution: def canTransform(self, start: str, end: str) -> bool: if start.replace('X', "") != end.replace("X", ""): return False i, j = 0, 0 while i < len(start) and j < len(end): while i &.
2021-04-25 22:14:52 101
原创 775. 全局倒置与局部倒置
class Solution: def isIdealPermutation(self, nums: List[int]) -> bool: min_A = [0] * len(nums) min_A[-1] = nums[-1] for i in range(len(nums)-2, -1, -1): min_A[i] = min(min_A[i+1], nums[i]) for i in range(.
2021-04-25 21:36:19 102
原创 773.滑动谜题
class Solution: def slidingPuzzle(self, board): res = [] board_res = [[1, 2, 3], [4, 5, 0]] ans = 0 if board == board_res: return ans q = [board] new = [[-1, 0], [0, 1], [1, 0], [0, -1]] ..
2021-04-25 19:48:19 104
原创 SQL(二)
SELECT A.dept_no, emp_no, maxSalary FROM(SELECT dept_no, salaries.emp_no AS emp_no,salaryFROM salaries JOIN dept_empON salaries.emp_no = dept_emp.emp_no) AS AJOIN (SELECT dept_no, MAX(salary) AS maxSalaryFROM salaries JOIN dept_empON salaries.emp_n..
2021-04-25 16:30:46 83
原创 599. 两个列表的最小索引总和
class Solution: def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]: dic1 = {} dic2 = {} for i in range(len(list1)): dic1[list1[i]] = i for j in range(len(list2)): dic2[..
2021-04-24 11:06:59 82
原创 598. 范围求和 II
class Solution: def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int: if not ops: return m * n min_row = min(row[0] for row in ops) min_col = min(col[1] for col in ops) return (min_row * min_c..
2021-04-24 10:48:12 90
原创 460. LFU 缓存
class Node: def __init__(self, key=-1, val=-1): self.key = key self.val = val self.freq = 1 self.prev = None self.next = Noneclass DLinkedList: def __init__(self): self.head = Node() self...
2021-04-23 11:52:15 90
原创 146. LRU 缓存机制
class DLinkNode: def __init__(self, key=0, value=0): self.key = key self.value = value self.pre = None self.next = Noneclass LRUCache: def __init__(self, capacity): self.cache = {} # 使用伪头部和伪尾部节点 ..
2021-04-23 11:07:34 87
原创 221. 最大正方形
class Solution: def maximalSquare(self, matrix: List[List[str]]) -> int: dp = [[0] * len(matrix[0]) for _ in range(len(matrix))] for i in range(len(matrix)): if matrix[i][0] == '1': dp[i][0] = 1 f.
2021-04-22 23:02:13 103
原创 牛客——把数字翻译成字符串
注意0## 解码# @param nums string字符串 数字串# @return int整型#class Solution: def solve(self , nums ): # write code here l = len(nums) if l == 1: if nums[0] == "0": return 0 else: .
2021-04-22 22:59:31 233
原创 890. 查找和替换模式
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: def check(word,pattern): if len(word)!=len(pattern): return False for i in range(len(word)): .
2021-04-20 11:55:43 103
原创 889. 根据前序和后序遍历构造二叉树
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def constructFromPrePost(self, pre, post): def dfs(pre,p.
2021-04-20 11:16:08 99
原创 888. 公平的糖果棒交换
class Solution: def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: sumA = sum(A) sumB = sum(B) total_sum = sumA + sumB for i in range(len(A)): if total_sum // 2 -(sumA - A[i]) in B: ..
2021-04-20 11:14:47 60
原创 1247. 交换字符使得字符串相同
class Solution: def minimumSwap(self, s1: str, s2: str) -> int: s = s1 + s2 x = s.count("x") if len(s) % 2 == 1 or x % 2 == 1 or (len(s) - x) % 2 == 1: return -1 pair1 = 0 pair2 = 0 for i..
2021-04-18 18:38:11 68
原创 SQL(一)
查询语句包含部分:select … from …where … group by …having … order by …limit ;
2021-04-16 22:59:17 104
原创 cifar-10保存图片
import cv2import numpy as npimport osimport pickle# 解压文件,返回解压缩后的字典def unpickle(file): with open(file, 'rb') as f: dicts = pickle.load(f, encoding='bytes') return dicts# 创建文件夹data_dict = {0:"airplane",1:"automobile",2:"bird",3:"c.
2021-04-15 17:22:57 258 1
原创 139. 单词拆分
class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: dp = [False] * (len(s) + 1) dp[0] = True s = "#" + s for i in range(1, len(s)): for j in range(i,len(s)): i.
2021-04-14 19:48:45 58
原创 235. 二叉搜索树的最近公共祖先
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'Tree.
2021-04-14 17:22:35 47
原创 233. 数字 1 的个数
class Solution: def countDigitOne(self, n: int) -> int: digit = 1 high = n // 10 low = 0 cur = n % 10 res = 0 while high != 0 or cur != 0: if cur == 0: res = res + high * d.
2021-04-14 16:26:03 73
原创 231. 2的幂
位运算class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and n & (n - 1) == 0
2021-04-14 11:01:17 65
原创 center loss代码PyTorch
import torchfrom torch import nnclass CenterLoss(nn.Module): """Center loss. Reference: Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016. Args: num_classes (int): number of classes.
2021-04-14 10:43:31 1082 2
原创 73. 矩阵置零
class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ # 记录第一行和第一列是否有 0 m = len(matrix) # m行 n = len(matrix[0]) ...
2021-04-13 20:18:22 87
原创 参数初始化kaiming
前向传播的时候, 每一层的卷积计算结果的方差为1.反向传播的时候, 每一 层的继续往前传的梯度方差为1(因为每层会有两个梯度的计算, 一个用来更新当前层的权重, 一个继续传播, 用于前面层的梯度的计算.)def weights_init_kaiming(m): classname = m.__class__.__name__ if classname.find('Linear') != -1: nn.init.kaiming_normal_(m.weight,..
2021-04-13 12:53:35 901
原创 牛客——输出二叉树的右视图
## 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可# 求二叉树的右视图# @param xianxu int整型一维数组 先序遍历# @param zhongxu int整型一维数组 中序遍历# @return int整型一维数组#class Solution: def solve(self , xianxu , zhongxu ): # write code here root = self.reConstructBina.
2021-04-10 21:14:22 154
原创 124. 二叉树中的最大路径和
class Solution: def maxPathSum(self, root: TreeNode) -> int: self.maxSum = float("-inf") def maxGain(node): if not node: return 0 # 递归计算左右子节点的最大贡献值 # 只有在最大贡献值大于 0 时,才会选取对应子节点 ..
2021-04-10 17:40:53 76
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人