332:重新岸炮行程

链接:332. 重新安排行程 - 力扣(LeetCode)

机场字典:{起飞机场:[到达机场的列表]}

去重:到达机场列表,i>0时,当前机场和上一个机场相等,continue

 1 class Solution:
2 def findItinerary(self, tickets: List[List[str]]) -> List[str]:
3 if(not tickets): return tickets
4 targets={}
5 for i in tickets:
6 if(i[0] not in targets.keys()): targets[i[0]]=[]
7 targets[i[0]].append(i[1])
8 for i in targets: targets[i].sort()
9 re=['JFK']
10 self.backtracking(len(tickets),targets,re,"JFK")
11 return re
12 def backtracking(self,lens,targets,re,tic):
13 if(len(re)==lens+1): return True
14 if(tic not in targets or not targets[tic]): return False
15 for i,dest in enumerate(targets[tic]):
16 if(i>0 and dest==targets[tic][i-1]): continue
17 targets[tic].pop(i)
18 re.append(dest)
19 if(self.backtracking(lens,targets,re,dest)): return True
20 targets[tic].insert(i, dest)
21 re.pop()
22 return False

findItinerary

51:N皇后

链接:51. N 皇后 - 力扣(LeetCode)

for遍历行

递归列

同一列没有Q,45°    135°对角线没有Q,可以放

 1 import copy
2 class Solution:
3 def solveNQueens(self, n: int) -> List[List[str]]:
4 if(not n): return []
5 #预置棋盘,所有位置均可以放
6 path=[]
7 for i in range(n):
8 path.append(copy.deepcopy(['.']*n))
9 re=[]
10 self.backtracking(n,re,path,0)
11 return re
12 def backtracking(self,n,re,path,startID):
13 #一次遍历完成
14 if(startID==n):
15 re.append(self.get_result(path))
16 return
17 for i in range(n):
18 #是否可以放Q
19 if(self.isValid(startID,i,n,path)):
20 path[startID][i]='Q'
21 self.backtracking(n,re,path,startID+1)
22 #回溯棋盘
23 path[startID][i]='.'
24
25 def isValid(self,x,y,n,path):
26 #一列有Q
27 for i in range(n):
28 if('Q' == path[i][y]): return False
29 i=x-1
30 j=y-1
31 #45°对角线
32 while i>=0 and j>=0:
33 if(path[i][j]=='Q'): return False
34 i-=1
35 j-=1
36 i=x-1
37 j=y+1
38 #135°对角线
39 while i>=0 and j<n:
40 if(path[i][j]=='Q'): return False
41 i-=1
42 j+=1
43 return True
44 def get_result(self,path):
45 re=[]
46 for i in path:
47 s=''
48 for j in i:
49 s+=j
50 re.append(s)
51 return re

solveNQueens

37:解数独

链接:37. 解数独 - 力扣(LeetCode)

(⊙﹏⊙)双层for循环中再加一层for循环,然后居然就过了

怎么说

 1 class Solution:
2 def solveSudoku(self, board: List[List[str]]) -> None:
3 """
4 Do not return anything, modify board in-place instead.
5 """
6 for i in range(len(board)):
7 for j in range(len(board[0])):
8 if(board[i][j]!='.'): continue
9 for k in range(1,10):
10 if(self.isValid(i,j,str(k),board)):
11 board[i][j]=str(k)
12 if self.solveSudoku(board): return True
13 board[i][j]='.'
14 return False
15 return True
16 def isValid(self,x,y,k,board):
17 #行
18 if k in board[x]: return False
19 #列
20 for i in range(9):
21 if board[i][y]==k: return False
22 xi=x-x%3
23 yj=y-y%3
24 #9宫
25 for i in range(xi,xi+3):
26 for j in range(yj,yj+3):
27 if board[i][j]==k: return False
28 return True

solveSudoku

=================================================================================================================

不行,脑壳疼,换哈希啃啃

242:有效的字母异位词

链接:242. 有效的字母异位词 - 力扣(LeetCode)

1 class Solution:
2 def isAnagram(self, s: str, t: str) -> bool:
3 if(len(s)!=len(t)): return False
4 for i in s:
5 if s.count(i)!=t.count(i): return False
6 return True

isAnagram

349:两个数组的交集

链接:349. 两个数组的交集 - 力扣(LeetCode)

1 class Solution:
2 def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
3 nums1=list(set(nums1))
4 nums2=list(set(nums2))
5 re=[]
6 for i in nums1:
7 if(i in nums2): re.append(i)
8 return re

intersection

202:快乐数

链接:202. 快乐数 - 力扣(LeetCode)

 1 class Solution:
2 def isHappy(self, n: int) -> bool:
3 hashcode=[n]
4 sums=0
5 while True:
6 if(not n):
7 if(sums==1): return True
8 if(sums in hashcode): return False
9 hashcode.append(sums)
10 n,sums=sums,n
11 else:
12 m=n%10
13 n=n//10
14 sums+=m*m

isHappy

Leetcode刷题第七天-回溯-哈希的更多相关文章

  1. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  2. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

  3. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  4. LeetCode刷题的一点个人建议和心得

    目录 1.    为什么我们要刷LeetCode? 2.    LeetCode的现状和问题 3.    本文的初衷 4.    LeetCode刷题建议 4.1入门数据结构,打基础阶段 4.2 建立 ...

  5. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  6. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  7. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  8. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  9. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  10. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

随机推荐

  1. Codeforce 1327A - Sum of Odd Integers

    Example input 6 3 1 4 2 10 3 10 2 16 4 16 5 output YES YES NO YES YES NO 解题思路:首先我们应该知道:偶数个奇数相加一定是偶数, ...

  2. Codeforces Round #710 (Div. 3)个人简单题解

    补题链接:Here Proble-A. Strange Table 根据 x 确定坐标确定的行数和列数. int main() { ios_base::sync_with_stdio(false), ...

  3. ios-class-guard - iOS代码混淆与加固实践

    ​ 目录 ios-class-guard - iOS代码混淆与加固实践 摘要 引言 一.class-dump 二.ios-class-guard 混淆原理 三.ios-class-guard 混淆结果 ...

  4. 使用cdn剥离js文件,让他们独立加载

  5. P2234

    乐死我了,一道需要用平衡树的算法的题,在我忘了看标签的情况下下意识用了一个普及-难度的超简单思路解决了.当然其中加入了一些半骗分半贪心性质的剪枝. 总之这破算法竟然AC了就离谱,乐死我了 Code # ...

  6. 二分图的匹配 hdu 1083

    ***题意:n个学生,p门课,求最大匹配,即p门课是否都有人上*** 匈牙利算法 #include<iostream> #include<cstdio> #include< ...

  7. nginx 工作原理及特点

    本文为博主原创,未经允许不得转载: nginx 简介:是一个高性能 HTTP 和 反向代理 服务器. Nginx 特点是占有内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中 ...

  8. Laravel - 创建项目

    1,创建目录 ( 路径不要带有中文 ) 2,进入目录,执行下列命令 composer create-project --prefer-dist laravel/laravel project

  9. K8S_IPV6 POD与数据库联通方法以及快速网络调试的一个思路

    K8S_IPV6 POD与数据库联通方法以及快速网络调试的一个思路 背景 前端时间搭建了一套K8S only IPV6 Single Stack的测试环境 因为自己长时间不搞K8S了, 并且IPV6的 ...

  10. [转帖]网站开启 IPv6 的三种方式

    https://zhuanlan.zhihu.com/p/443835798 从传统二进制部署的 Nginx ,到云原生部署的 K8S.Istio,分别介绍网站开启 IPv6 的三种方式. 1.Ngi ...