leetcode

LeetCode 246/247/248 中心对称数

落爺英雄遲暮 提交于 2020-03-06 02:00:32
目录 LeetCode 246 (Easy):判断一个数是否为中心对称数 LeetCode 247 (Medium):给出长度为 n 的所有中心对称数 LeetCode 248 (Hard):求给定区间内有多少中心对称数 LeetCode 246 (Easy):判断一个数是否为中心对称数 A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. Examples: Input: "69" Output: true Input: "88" Output: true Input: "962" Output: false 解答 : 两个数字只有这5种情况满足中心对称:11,88, 00, 69,96。可以看做求回文数的一种特殊情况,用双指针。 参考代码: https://www.cnblogs.com/grandyang/p/5196960.html LeetCode 247 (Medium):给出长度为 n 的所有中心对称数 A

【数据结构和算法】_09_广度 / 深度 优先搜索

a 夏天 提交于 2020-03-05 23:11:50
文章目录 【一】 Breadth - First Search (广度优先搜索) 【二】 Depth - First Search (深度优先搜索) 【三】 Interview (面试题) 【3.1】 LeetCode 102:Binary Tree Level Order (二叉树的层次遍历) 【3.2】 LeetCode 104:Max depth (二叉树的最大深度) 【3.3】 LeetCode 111:Min depth (二叉树的最小深度) 【3.4】 LeetCode 22:Generate Parentheses (括号生成) 【一】 Breadth - First Search (广度优先搜索) 比较符合人类的思维的,常用的,搜索算法,必须掌握 示意图( 树 ) 代码 (不仅适用于树,也适用于图) # python 广度优先搜索 def BFS ( graph , start , end ) : # 队列,先进先出 queue = [ ] queue . append ( [ start ] ) # visited 里的数据表示被访问过了 (对二叉树来说没必要) visited . add ( start ) # 当队列不为空时 while queue : # 将队列头元素取出 node = queue . pop ( ) # 放进被访问过的列表里 visited

【LeetCode题解】144_二叉树的前序遍历

妖精的绣舞 提交于 2020-03-05 23:09:48
目录 【LeetCode题解】144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 【LeetCode题解】144_二叉树的前序遍历 描述 给定一个二叉树,返回它的 前序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 方法一:递归 Java 代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); preorderTraversal(root, res); return res; } private void preorderTraversal(TreeNode root, List<Integer> res) { if

[LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

♀尐吖头ヾ 提交于 2020-03-05 13:33:47
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example, given a 3-ary tree: We should return its level order traversal: [ [1], [3,2,4], [5,6] ] Note: The depth of the tree is at most 1000 . The total number of nodes is at most 5000 . 这道题给了我们一棵N叉树,让我们对其进行层序遍历。我们做过之前二叉树的层序遍历的那道题的话 Binary Tree Level Order Traversal ,那么这道题也就不难了。虽说现在每一个结点可能有很多个子结点,但其实处理的思路的都是一样的。子结点放到了一个children数组中,我们访问的时候只要遍历数组就行了。先来看迭代的写法,用到了队列queue来辅助,首先判断root是否为空,为空直接返回空数组,否则加入queue中。然后遍历queue,这里用的trick就是,要加个for循环,要将当前queue中的结点的个数统计下来,因为再加入下一层的结点时

LeetCode All in One 题目讲解汇总(持续更新中...)

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-05 11:19:47
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks. +----+-------+ | Id | Score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ For example, given the above Scores table, your query should generate the following report (order by highest score): +-------+------+ | Score | Rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

扶醉桌前 提交于 2020-03-05 05:17:35
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1 ) is given as graph . graph.length = N , and j != i is in the list graph[i] exactly once, if and only if nodes i and j are connected. Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges. Example 1: Input: [[1,2,3],[0],[0],[0]] Output: 4 Explanation: One possible path is [1,0,2,0,3] Example 2: Input: [[1],[0,2,4],[1,3,4],[2],[1,2]] Output: 4 Explanation: One possible path is [0,1,4,2,3] Note: 1 <= graph.length <= 12 0 <

LeetCode All in One 题目讲解汇总(持续更新中...)

别等时光非礼了梦想. 提交于 2020-03-05 04:55:05
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out the secret number. For example: Secret number: 1807 Friend's guess: 7810 Hint: 1 bull and 3 cows. (The bull is 8 , the cows are 0 , 1 and 7 .) According to Wikipedia : "Bulls and Cows (also known as Cows and

【LeetCode 943】 Find the Shortest Superstring

风流意气都作罢 提交于 2020-03-05 01:15:03
题目描述 Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A. Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also be accepted. Example 2: Input: ["catg","ctaagt","gcta","ttca","atgcatc"] Output: "gctaagttcatgcatc" Note: 1 <= A.length <= 12 1 <= A[i].length <= 20 思路 思路一: DFS。时间复杂度:O(n!) 。注意剪枝:当前路径的长度>已有的最短路径长度时,不用继续搜索。然后,如果在搜索,保存最优字符串,不断的对字符串操作很费时间,可以保存路径,最后根据路径获得最后的字符串。 思路二: 动态规划。dp[s][i]

LeetCode 31. Next Permutation

社会主义新天地 提交于 2020-03-04 07:30:49
问题链接 LeetCode 31. Next Permutation 题目解析 求序列的下一个字典序列。 解题思路 什么是下一个字典序列?了解一下: next_permutation(全排列算法) 。算法的核心思想: 从序列尾部开始往前寻找两个相邻元素,设为( i, ii),即有 \(\*i < \*ii\) 。 再次从序列尾部往前寻找第一个大于 i 的元素,设为 j,即有 \(\*j > \*i\) 。交换(*i,*j)。 将从 *ii 开始在尾部的所有元素逆序排列。 //注:第二第三步可交换,即先全部逆序,再找两个交换也OK,不过在找 *j 时需要正序从 *i++ 开始找。 本题的目的应该是要实现该算法,看过描述后可以直接写出代码。 一行代码 如果只是这样的话,那就没什么意思了:)放在这里只是告诉大家C++中有这么一个函数,对了还有一个与之配套的函数,叫做prev_permutation,没错它是用来求上一个排列的。 class Solution { public: void nextPermutation(vector<int>& nums) { next_permutation(nums.begin(), nums.end()); } }; 具体实现 class Solution { public: void nextPermutation(vector<int>&

一道递归/动规易错题的总结

廉价感情. 提交于 2020-03-04 04:19:39
这两天做leetcode发现自己对一个递归/动规的问题容易想错,特此来总结一下 总共3道题 一个是前几天碰见的面试题,给定一个二叉树还有一个给定的数值,让找到这课二叉树是否有一条路径上的值的和刚好等于给定的值(这里的路径只能从父节点到子节点) Leetcode 1367 这道题是相当于上面的变体,给定一个二叉树还有一个链表,判断二叉树上是否有一条路径刚好等于链表上的那条路径。 第三题也是leetcode上的一题,但是忘了哪道了。记得是一个字符串里,需要计算最长满足另外一个字符串的连续子串的问题。 上面的三个问题容易犯一个共同的错误就是递归去做这道题的时候,递归函数容易写成如下(比如以第二题为例): bool isSubPath ( ListNode * head , TreeNode * root ) { if ( head == NULL ) return true ; if ( root == NULL ) return false ; bool res = false ; if ( head - > val == root - > val ) res = isSubPath ( head - > next , root - > left ) || isSubPath ( head - > next , root - > right ) ; if ( res == true )