当前位置:网站首页>Leetcode daily question 2022/1/31-2022/2/6
Leetcode daily question 2022/1/31-2022/2/6
2022-07-22 19:13:00 【alphaTao】
The preliminary problem-solving ideas are recorded And local implementation code ; Not necessarily optimal I also hope you can discuss Progress together
Catalog
- 1/31 1342. Turn the number into 0 The number of operations
- 2/1 1763. The longest nice substring
- 2/2 2000. Reverse word prefix
- 2/3 1414. And for K The minimum number of Fibonacci Numbers
- 2/4 1725. The number of rectangles that can form the largest square
- 2/5 1219. Golden Miner
- 2/6 1748. The sum of the only elements
1/31 1342. Turn the number into 0 The number of operations
simulation
def numberOfSteps(num):
""" :type num: int :rtype: int """
ans = 0
while num>0:
if num%2==0:
num=num//2
else:
num=num-1
ans+=1
return ans
2/1 1763. The longest nice substring
low,up Record which characters appear in lowercase and uppercase respectively altogether 26 position
If low==up It indicates that the characters in upper and lower case are consistent
Enumerate all substrings
def longestNiceSubstring(s):
""" :type s: str :rtype: str """
n = len(s)
loc,leng = 0,0
for i in range(n):
low,up = 0,0
for j in range(i,n):
if s[i].islower():
low |=1<<(ord(s[j])-ord('a'))
else:
up |=1<<(ord(s[j])-ord('A'))
if low==up and j-i+1>leng:
loc = i
leng = j-i+1
return s[loc:loc+leng]
2/2 2000. Reverse word prefix
Find the place where it first appeared loc reverse [0:loc]
def reversePrefix(word, ch):
""" :type word: str :type ch: str :rtype: str """
ans = word
loc = -1
for i,c in enumerate(word):
if c==ch:
loc = i
break
if loc>-1:
ans = word[loc::-1]+word[loc+1:]
return ans
2/3 1414. And for K The minimum number of Fibonacci Numbers
Find the longest Fibonacci sequence needed l
stay l In two points, find less than k The maximum of take k Minus the maximum Keep looking for Until for 0
def findMinFibonacciNumbers(k):
""" :type k: int :rtype: int """
l = [1,1]
if k==1:
return 1
while k>l[-1]:
l.append(l[-1]+l[-2])
ans = 0
left,right = 0,len(l)-1
while k>0:
while left<=right:
mid = (left+right)//2
if l[mid]==k:
ans +=1
return ans
elif l[mid]<k:
left = mid+1
else:
right = mid-1
k -= l[right]
left,right = 0,left-1
ans +=1
return ans
2/4 1725. The number of rectangles that can form the largest square
The minimum side of each rectangle is taken as the side length
Count the number of side lengths And record the maximum side length that has occurred
def countGoodRectangles(rectangles):
""" :type rectangles: List[List[int]] :rtype: int """
maxl = 0
m = {
}
for l,w in rectangles:
k = min(l,w)
m[k] = m.get(k,0)+1
maxl = max(maxl,k)
return m[maxl]
2/5 1219. Golden Miner
ans Record the maximum number of gold
Every non 0 All points can be used as a starting point
Search deeply for a starting point to flash back
def getMaximumGold(grid):
""" :type grid: List[List[int]] :rtype: int """
m,n=len(grid),len(grid[0])
ans = 0
def dfs(i,j,gold):
global ans
v = grid[i][j]
gold += v
ans = max(ans,gold)
grid[i][j] = 0
for x,y in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
if 0<=x<m and 0<=y<n and grid[x][y]>0:
dfs(x,y,gold)
grid[i][j] = v
for i in range(m):
for j in range(n):
if grid[i][j]>0:
dfs(i,j,0)
return ans
2/6 1748. The sum of the only elements
Count the number of occurrences of each number Accumulate the number that appears once
def sumOfUnique(nums):
""" :type nums: List[int] :rtype: int """
m = {
}
for num in nums:
m[num] = m.get(num,0)+1
ans = 0
for num in m:
if m[num]==1:
ans+=num
return ans
边栏推荐
- Jvm-jvm overview
- 1. Create a dynamic library of functions, 2. Howto create and deploy a sample DLL using MinGW
- Behind the explosion of collaborative office market: cloud communication capability is the focus of demand
- Mysql5.7 decompression configuration steps
- [yolov5 practice 4] traffic sign recognition system based on yolov5 - model test and evaluation
- LeetCode 每日一题 2021/12/6-2021/12/12
- MIHA tour recruited a large number of new students, and the school enrollment was approved in advance on the last day!
- JUC synchronizer
- Ways of data optimization
- C language static and extern knowledge points
猜你喜欢
【链表技巧汇总】141.环形链表(简单)
Design of hydraulic system for power slide of horizontal single side multi axis drilling combined machine tool
This points to the problem
程序员面试金典面试题 01.05. 一次编辑
1. Package and release procedure of QT (NSIS);
MySQL master-slave replication
paper - A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
服务器磁盘IO性能调优
高手常用的15 种 SQL 优化
互联网通信安全之终端数据保护
随机推荐
融云首席科学家任杰:历练出人才,职场「经历>经验」
二、IDEA搭建JFinal項目+代碼自動生成+數據庫操作測試(三種方式)
1. Create a dynamic library of functions, 2. Howto create and deploy a sample DLL using MinGW
Learning to Incorporate Structure Knowledge for Image Inpainting
Leetcode: 1179. Reformat department table
MySQL execution process and sequence
Prototype object
JVM-系统优化
卧式单面多轴钻孔组合机床动力滑台液压系统的设计
LeetCode 每日一题 2021/12/13-2021/12/19
[yolov5 practice 4] traffic sign recognition system based on yolov5 - model test and evaluation
程序员面试金典面试题 01.01. 判定字符是否唯一
代码—递归
OSI seven layer network model
Date operation in shell script
Flink learning notes (IV) Flink runtime architecture
服务器运维环境安全体系(上篇)
LeetCode 每日一题 2022/2/21-2022/2/27
Three ways to restrict IP access between micro services
1. Package and release procedure of QT (NSIS);