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. 【题解】Qin Shi Huang's National Road System HDU - 4081 ⭐⭐⭐⭐ 【次小生成树】

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  2. ngix反向代理服务器

    Nginx ("engine x") 是一个高性能的HTTP 和反向代理 服务器,在大负载的情况下表现十分优秀. 1.正向代理 正向代理也是大家最常接触的到的代理模式.正向代理最大 ...

  3. vue子页面给App.vue传值

    前端工程现在变成了单页面富文本的模式,整体布局定下来后,跳转只在<router-view>中展示,外层的布局不容易改变.最近发现有如下这个方法可以直接传值给App.vue,经过实践确实可以 ...

  4. poi 4.11版本 word模板操作

    写代码之前先说说遇到的问题,之前word模板是使用poi 3.9的包实现的,之后又写了exlce上传下载的功能使用的是poi 4.11的版本,他们之间融合的时候发现包冲突,总有一个功能不能使用,之后发 ...

  5. 以太网扫盲(一)各种网络总线 mii总线,mdio总线介绍

    本文主要介绍以太网的MAC(Media Access Control,即媒体访问控制子层协议)和PHY(物理层)之间的MII(Media Independent Interface ,媒体独立接口), ...

  6. 区分开发环境和生产环境webpack

  7. java基础-java面向对象01-day08

    1. 一个简单的类 认识类 成员变量 类方法 public class Person { //类的成员变量 int age; String name; double height; double we ...

  8. The project description file (.project) for XXX is missing

    在STS中切换项目分支的时候,出现一个项目打不开了,提示:The project description file (.project) for XXX is missing 试了下网上的方法都没有解 ...

  9. [转帖]一份快速实用的 tcpdump 命令参考手册

    http://team.jiunile.com/blog/2019/06/tcpdump.html tcpdump 简介 对于 tcpdump 的使用,大部分管理员会分成两类.有一类管理员,他们熟知  ...

  10. 【转帖】GPT4All开源的聊天机器人

    GPT4All是一个开源的聊天机器人,它基于LLaMA的大型语言模型训练而成,使用了大量的干净的助手数据,包括代码.故事和对话.它可以在本地运行,不需要云服务或登录,也可以通过Python或Types ...