题目: Implement a magic directory with buildDict, and search methods. For the method buildDict, youll be given a list of non-repetitive words to build a dictionary. For the method search, youll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built. Example 1: Input: buildDict(["hello", "lee...
leetcode225. 用队列实现栈 使用队列实现栈的下列操作:push(x) -- 元素 x 入栈pop() -- 移除栈顶元素top() -- 获取栈顶元素empty() -- 返回栈是否为空注意:你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。你可以假设所有操作都是有效的(例如, 对...
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. Example 1: Input: s = "egg", t = "add" Output: true Example 2: In...
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/目录题目描述题目大意解题方法并查集日期 题目地址:https://leetcode-cn.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目描述 Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected g...
LeetCode 141. Linked List Cycle–百度面试编程题–C++,Python解法LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目C++和Python的解法都有。题目地址:Linked List Cycle - LeetCodeGiven a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where...
题目描述:思路 见代码注释 代码 class Solution { public:string longestPalindrome(string s) {//Case1:空的或者只有一个的,就是回文,直接返回if(s.size() == 0 || s.size() == 1) return s;//Case2:如果只有两个相等的元素,也是回文字符串,直接返回;if(s.size() == 2 && s[0] == s[1])return s;//Case3:一般情况//--Step1:先初始化变量,int start(0), end(0);//start和end记录回文子串的起始位置int maxLen(0);//maxLen用...
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/目录题目描述题目大意解题方法遍历日期 题目地址:https://leetcode-cn.com/problems/index-pairs-of-a-string/ 题目描述 Given a text string and words (a list of strings), return all index pairs [i, j] so that the substring text[i]...text[j] is in the list of words. Example 1: Input: text = "thestoryofleetcodeandme", words = ["sto...
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/目录题目描述题目大意解题方法大根堆日期 题目地址:https://leetcode-cn.com/problems/high-five/ 题目描述 Given a list of scores of different students, return the average score of each student’s top five scores in the order of each student’s id. Each entry items[i] has items[i][0] the student’s id, and items[i][1] the studen...
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/目录题目描述题目大意解题方法字典日期 题目地址:https://leetcode-cn.com/problems/confusing-number/ 题目描述 Given a number N, return true if and only if it is a confusing number, which satisfies the following condition: We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they b...
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/目录题目描述题目大意解题方法暴力求解日期 题目地址:https://leetcode-cn.com/problems/fixed-point/ 题目描述 Given an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. Example 1: Input: [-10,-5,0,3,7] Output: 3 Explanation: For the giv...
题目 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数。 示例 1: 输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1、1 阶 + 1 阶 2、2 阶 示例 2: 输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1、1 阶 + 1 阶 + 1 阶 2、1 阶 + 2 阶 3、2 阶 + 1 阶 想法 要想爬到第3层,可以先爬到第二层,然后爬1个台阶,或者先爬到第...
给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。 示例 1:输入:text = "nlaebolko" 输出:1示例 2:输入:text = "loonbalxballpoon" 输出:2示例 3:输入:text = "leetcode" 输出:0 提示: 1 <= text.length <= 10^4 text 全部由小写英文字母组成 C++class Solution { public:int maxN...
给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序。我们要执行零次或多次煎饼翻转(按顺序一次接一次地进行)以完成对数组 A 的排序。 返回能使 A 排序的煎饼翻转操作所对应的 k 值序列。任何将数组排序且翻转次数在 10 * A.length 范围内的有效答案都将被判断为正确。 示例 1: 输入:[3,2,4,1] 输出:[4,2,4,3] 解释: 我们执行 4 次煎饼翻转,k 值分别为 4,2,4,和...
1.题目给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] 输出: true 示例 2: 输入: 1 1 / \ 2 2 [1,2], [1,null,2] 输出: false示例 3: 输入: 1 1 ...
题目如下:解题思路: 递归 + 回溯。 代码如下:class Solution { public:vector<vector<int>> permute(vector<int>& nums) {vector<vector<int>> res;int l = nums.size();backtrackpermute(0, l, nums, res);return res;}void backtrackpermute(int k, int n, vector<int>& nums, vector<vector<int>> res){if(k == n){res.push_back(nums);return ;}for(int i = k; i < n; i++){swap(nums[k], nums[i]);backtrackpermute(k+1, n...