- 丑数 Leecode 264
编写一个程序,找出第 n 个丑数。丑数就是只包含质因数 2, 3, 5 的正整数。习惯上把 1 当作第一个丑数。
#三指针法
def nthUglyNumber(self, n: int) -> int:
if not n: return
p2,p3,p5=0,0,0
res=[1]
for _ in range(1,n):
min_num=min(res[p2]*2,res[p3]*3,res[p5]*5)
res.append(min_num)
if res[p2]*2==min_num:
p2+=1
if res[p3]*3==min_num:
p3+=1
if res[p5]*5==min_num:
p5+=1
return res[-1]
50.1. 第一个只出现一次的字符 leetcode 387
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
def firstUniqChar(self, s: str) -> int:
if not s: return -1
count={} #dict
for x in s:
if x not in count:
count[x]=1
else:
count[x]+=1
for key in count:
if count[key]==1:
return s.index(key) #!!!虽然在for循环中,但return可保证返回第一个 值的要求
return -1
50.2.
字符流中第一个只出现一次的字符 牛客网
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。返回对应的字符
字符流: 字符一个一个不断地进来
方法一
class Solution:
# 返回对应char
def __init__(self):
self.s=""
self.count={}
def FirstAppearingOnce(self):
for x in self.s: #!!!要按字符流的顺序 output;不能为self.count
if self.count[x]==1:
return x
return "#"
def Insert(self,char):
self.s+=char
if char in self.count:
self.count[char]+=1
else:
self.count[char]=1
每读出一个字符,将该字符的
读取位置
存入字符对应数组下标中。
方法二
class Solution_2:
def __init__(self):
self.count=[-1]*256 #类型为数组
self.index=0 # 记录当前字符的个数,第几个读取
def FirstAppearingOnce(self):
min_value=self.index
ch="#"
for i in self.count:
if self.count[i]>=0 and self.count[i]<min_value:
min_value=self.count[i] #!!!(利用for循环)保证output的是第一个 题目的要求值,而不是任意一个只出现一次的char
ch=chr(i)
return ch
def Insert(self,char):
if self.count[ord(char)]==-1:
self.count[ord(char)]=self.index
elif self.count[ord(char)]>=0:
self.count[ord(char)]=-2
self.index+=1
#2个命令
index() '''返回查找对象的索引位置,如果没有找到对象则抛出异常。'''
ord() '''返回查找对象的[一个字符(长度为1的字符串)作为参数]ascii码,对应的十进制整数'''
chr() '''主要用来表示ascii码对应的字符:输入是数字,可以用十进制,也可以用十六进制,返回字符'''
print(ord('a')) #97
print(chr(97)) #a
print(chr(0x61)) #a ps:[ord()与chr()用法以及区别](https://www.cnblogs.com/sui776265233/p/9103251.html)
51.(多practice) 数组中的逆序对
输入一个数组,求出这个数组中的逆序对的总数。
class Solution:
def inversePairs(self, nums):
self.count=0
self.merge_sort(nums)
return self.count
def merge_sort(self,nums):
if len(nums)<=1: return nums
mid = len(nums)>>1
left=nums[:,mid]
right=nums[mid:]
l,r=0,1
tmp=[]
while l<len(left) and r<len(right): #!!!用while (当unknown/无法sure 具体的计数变量[]e.g:l, r都在动)
if left[l]>right[r]:
tmp.append(right[r])
r+=1
self.count+=len(left[l:]) #是加的left[l:]
else:
tmp.append(left[l])
l+=1
tmp +=left[l:]+right[r:] #收尾:确保剩下未被比较的数
return tmp
52.两个链表的第一个公共节点
找到两个单链表相交的起始节点
牛客网要求 !=LeetCode
一个哈希表存储链表A的所有节点,然后检查链表 B 中的每一个结点 b_i 是否在哈希表中。若在,则 b_i 为相交结点。
哈希表法 Time:O(m+n) Space:O(m)或O(n)
def getIntersectionNode(self, headA, headB):
dic=set() #!!!用的set 哈希结构----p1是指针
p1= pHead1
while p1:
dic.add(p1)
p1=p1.next
while pHead2:
if pHead2 in dic:
return pHead2
pHead2= pHead2.next
#也对
# while q:
# if q in dic:
# return q
# q= q.next
双指针法 Space:O(1) [leetcode160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/submissions/)
def getIntersectionNode(self, headA, headB):
if not headA or not headB: return
p,q =headA,headB
while p!=q:
p=p.next if p else headB
q=q.next if q else headA
return p
∙ \bullet ∙ 知识点:
dict是用 哈希的方式
存储数据,其复杂度为O(1);速度快
set()哈希的方式
, 也 是 一 组 k e y 的 集 合 , 但 不 存 储 v a l u e \color{blue}{也是一组key的集合,但不存储value} 也是一组key的集合,但不存储value
Python哈希表的例子:dict、set