[leetcode] 题型整理之图论

图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦。

求最短路径:

127. Word Ladder

Given two words (beginWord and endWord), and a dictionary‘s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.

All words contain only lowercase alphabetic characters.

从起点开始向外更新,因为每条路径的权值都不是负数,所以先更新的总比后更新的小。

已经被更新过的之后就不用考虑了

时间: 2024-11-07 15:19:19

[leetcode] 题型整理之图论的相关文章

leetcode 题型整理之数字加减乘除乘方开根号

需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the tw

[leetcode]题型整理之用bit统计个数

137. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 记录32个bit每个bit出现的

leetcode 题型 数据结构 解法 分类总结

第2章 线性表 2.1 数组 2.1.1 Remove Duplicates from Sorted Array 2.1.2 Remove Duplicates from Sorted Array II 2.1.3 Search in Rotated Sorted Array 2.1.4 Search in Rotated Sorted Array II 2.1.5 Median of Two Sorted Arrays 2.1.6 Longest Consecutive Sequence 2.

Leetcode文章整理

LeetCode的题目种类比较多,感觉应该将自己联系过的题目进行分类,这个就是根据自己做过的题目进行划分,并做一定的总结,会持续更新 Sort: Two Pointer: 单链表: Reorder List将l1->l2...->ln转化为l1->ln->l2->ln-1.. 这里用的很直接的方法就是找到链表的中点,然后将链表分为两部分,后半截翻转后两个链表进行融合.我在想,如果能之间把后面半截放入vector当中,就简单很多,但是就是牺牲了空间,不知道有没有更好的办法. I

leetcode 动态规划整理

动态规划整理 1.最长公共子序列 # 给定两个字符串?text1 和?text2,返回这两个字符串的最长公共子序列. # # 一个字符串的?子序列?是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串. # 例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" # 的子序列.两个字符串的「公共子序列」是这两个字符串所共同拥有的子

Leetcode 题目整理-4

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 注:这题竟然连个示例都没有,说明特殊情况并不多,就是要找出所有字符串的最长公共前缀.他应该不会超过所有字符串中最短的那个,可以试着找出最短长度n,然后每个都读取和比较n个,并根据情况不断减小那个n. 19. Remove Nth Node From End of List

Leetcode 题目整理-2 Reverse Integer && String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

Leetcode 题目整理-2

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

Leetcode 题目整理-3 Palindrome Number & Roman to Integer

9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the res