Leetcode
没事多喝白开水
这个作者很懒,什么都没留下…
展开
-
Leetcode101. Symmetric Tree
给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/ 2 2/ \ / 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/ 2 2\ 3 3说明:如果你可以运用递归和迭代两种方法解决这个问题,会很加分。来源:力扣(LeetCode)链接:https://...原创 2019-08-17 03:18:01 · 151 阅读 · 0 评论 -
Leetcode127. Word Ladder
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord的最短转换序列的长度。转换需遵循如下规则:每次转换只能改变一个字母。 转换过程中的中间单词必须是字典中的单词。 说明:如果不存在这样的转换序列,返回 0。 所有单词具有相同的长度。 所有单词只由小写字母组成。 字典中不存在重复的单词。 你可以假设beginWord 和 endW...原创 2019-08-15 04:13:24 · 119 阅读 · 0 评论 -
Leetcode494. Target Sum
You are given a list of non-negative integers, a1, a2, …, an, and atarget, S. Now you have 2 symbols + and -. For each integer, youshould choose one from + and - as its new symbol.Find out how man...原创 2019-08-05 23:46:29 · 88 阅读 · 0 评论 -
Leetcode 5. 最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad” 输出: “bab” 注意: “aba” 也是一个有效答案。 示例 2:输入: “cbbd” 输出: “bb”来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-palindromic-substrin...原创 2019-07-28 14:39:55 · 68 阅读 · 0 评论 -
Leetcode647. Palindromic Substrings
Given a string, your task is to count how many palindromic substringsin this string.The substrings with different start indexes or end indexes are countedas different substrings even they consist ...原创 2019-08-05 21:22:38 · 61 阅读 · 0 评论 -
Leetcode338. Counting Bits
Given a non negative integer number num. For every numbers i in therange 0 ≤ i ≤ num calculate the number of 1’s in their binaryrepresentation and return them as an array.Example 1:Input: 2 Outpu...原创 2019-08-05 20:28:37 · 78 阅读 · 0 评论 -
Leetcode322. Coin Change
You are given coins of different denominations and a total amount ofmoney amount. Write a function to compute the fewest number of coinsthat you need to make up that amount. If that amount of money...原创 2019-08-05 19:47:38 · 80 阅读 · 0 评论 -
Leetcode309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of agiven stock on day i.Design an algorithm to find the maximum profit. You may complete asmany transactions as you like (ie, buy one ...原创 2019-08-05 17:47:19 · 75 阅读 · 0 评论 -
Leetcode300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longestincreasing subsequence.Example:Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longestincreasing subsequence is [2,3,7,101]...原创 2019-08-05 16:40:54 · 78 阅读 · 0 评论 -
Leetcode279. Perfect Squares
Given a positive integer n, find the least number of perfect squarenumbers (for example, 1, 4, 9, 16, …) which sum to n.Example 1:Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example 2:I...原创 2019-08-05 15:31:11 · 61 阅读 · 0 评论 -
[转]Leetcode刷题指南和top100题目
https://blog.csdn.net/lingpy/article/details/88085446转载 2019-07-26 20:52:06 · 273 阅读 · 0 评论 -
Leetcode167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order,find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers ...原创 2019-08-11 00:45:52 · 72 阅读 · 0 评论 -
Leetcode633. Sum of Square Numbers
Given a non-negative integer c, your task is to decide whetherthere’re two integers a and b such that a2 + b2 = c.Example 1:Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5Example 2:Input: 3...原创 2019-08-11 01:11:43 · 87 阅读 · 0 评论 -
Leetcode345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only thevowels of a string.Example 1:Input: “hello” Output: “holle” Example 2:Input: “leetcode” Output: “leotcede” Note: The vowels does ...原创 2019-08-11 01:28:51 · 112 阅读 · 0 评论 -
Leetcode509. Fibonacci Number
斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算F(N)。示例 1:输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1. 示例...原创 2019-08-17 00:51:26 · 149 阅读 · 0 评论 -
Leetcode451. Sort Characters By Frequency
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。示例 1:输入: “tree”输出: “eert”解释: 'e’出现两次,'r’和’t’都只出现一次。 因此’e’必须出现在’r’和’t’之前。此外,"eetr"也是一个有效的答案。 示例2:输入: “cccaaa”输出: “cccaaa”解释: 'c’和’a’都出现三次。此外,"aaaccc"也是有效的答案。 注意"ca...原创 2019-08-13 02:05:45 · 125 阅读 · 0 评论 -
Leetcode75. Sort Colors
给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。注意: 不能使用代码库中的排序函数来解决这道题。示例:输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶:一个直观的解决方案是使用计数排序的两趟扫描算法。 首先,迭代...原创 2019-08-13 02:01:46 · 90 阅读 · 0 评论 -
快手笔试题Leetcode165
比较两个版本号 version1 和 version2。如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。你可以假设版本字符串非空,并且只包含数字和 . 字符。. 字符不代表小数点,而是用于分隔数字序列。例如,2.5 不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。你可以假...原创 2019-08-25 19:13:56 · 489 阅读 · 0 评论 -
Leetcode215. Kth Largest Element in an Array
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。示例 1:输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2:输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明:你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。来源:力扣(LeetCode)链接:...原创 2019-08-12 03:13:33 · 91 阅读 · 0 评论 -
Leetcode524. Longest Word in Dictionary through Deleting
给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到。如果答案不止一个,返回长度最长且字典顺序最小的字符串。如果答案不存在,则返回空字符串。示例 1:输入: s = “abpcplea”, d = [“ale”,“apple”,“monkey”,“plea”]输出: “apple” 示例 2:输入: s = “abpcplea”, d...原创 2019-08-12 02:01:44 · 119 阅读 · 0 评论 -
Leetcode141. Linked List Cycle
给定一个链表,判断链表中是否有环。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是-1,则在该链表中没有环。示例 1:输入:head = [3,2,0,-4], pos = 1 输出:true 解释:链表中有一个环,其尾部连接到第二个节点。 示例 2:输入:head = [1,2], pos = 0 输出:true ...原创 2019-08-12 00:52:16 · 76 阅读 · 0 评论 -
Leetcode88. Merge Sorted Array
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。 你可以假设 nums1 有足够的空间(空间大小大于或等于 m +n)来保存 nums2 中的元素。 示例:输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2...原创 2019-08-12 00:28:56 · 76 阅读 · 0 评论 -
Leetcode680. Valid Palindrome II
Given a non-empty string s, you may delete at most one character.Judge whether you can make it a palindrome.Example 1: Input: “aba” Output: True Example 2: Input: “abca” Output:True Explanation: Y...原创 2019-08-11 02:31:26 · 82 阅读 · 0 评论 -
Leetcode221. Maximal Square
Given a 2D binary matrix filled with 0’s and 1’s, find the largestsquare containing only 1’s and return its area.Example:Input:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Output: 4动态规划20msclass ...原创 2019-08-04 19:17:56 · 86 阅读 · 0 评论 -
Leetcode198. House Robber
You are a professional robber planning to rob houses along a street.Each house has a certain amount of money stashed, the only constraintstopping you from robbing each of them is that adjacent hous...原创 2019-08-04 18:10:16 · 68 阅读 · 0 评论 -
Leetcode 677. 键值映射
实现一个 MapSum 类里的两个方法,insert 和 sum。对于方法insert,你将得到一对(字符串,整数)的键值对。字符串表示键,整数表示值。如果键已经存在,那么原来的键值对将被替代成新的键值对。对于方法 sum,你将得到一个表示前缀的字符串,你需要返回所有以该前缀开头的键的值的总和。示例 1:输入: insert(“apple”, 3), 输出: Null 输入: sum(...原创 2019-07-13 19:45:59 · 288 阅读 · 0 评论 -
Leetcode 211. 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构:void addWord(word) bool search(word) search(word)可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。示例:addWord(“bad”) addWord(“dad”) addWord(“mad”) search(“pad”) -> falsesearch...原创 2019-07-13 19:21:20 · 242 阅读 · 0 评论 -
Leetcode208 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。示例:Trie trie = new Trie();trie.insert(“apple”); trie.search(“apple”); // 返回 truetrie.search(“app”); // 返回 false trie.startsWith(“app”);...原创 2019-07-13 17:44:53 · 206 阅读 · 0 评论 -
Leetcode 307. 区域和检索 - 数组可修改
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。update(i, val) 函数可以通过将下标为 i 的数值更新为 val,从而对数列进行修改。示例:Given nums = [1, 3, 5]sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8 说明:...原创 2019-07-13 03:12:36 · 323 阅读 · 0 评论 -
Leetcode 303. 区域和检索 - 数组不可变 C++
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。示例:给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 说明:你可以假设数组不可变。...原创 2019-06-21 16:50:00 · 389 阅读 · 0 评论 -
Leetcode 347. 前K个高频元素 C++
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。示例 1:输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2:输入: nums = [1], k = 1 输出: [1] 说明:你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。 你的算法的时间复杂度必须优于 O(n log n) , n是数组的大小。来源...原创 2019-06-21 16:47:51 · 784 阅读 · 0 评论 -
Leetcode 350. 两个数组的交集 II C++
给定两个数组,编写一个函数来计算它们的交集。示例 1:输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2:输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [4,9] 说明:输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。 我们可以不考虑输出结果的顺序。 进阶:如果给定的数组已...原创 2019-06-21 16:43:50 · 398 阅读 · 0 评论 -
Leetcode 349. 两个数组的交集 C++
给定两个数组,编写一个函数来计算它们的交集。示例 1:输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2:输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [9,4] 说明:输出结果中的每个元素一定是唯一的。 我们可以不考虑输出结果的顺序。来源:力扣(LeetCode)链接:https://lee...原创 2019-06-21 16:42:22 · 334 阅读 · 0 评论 -
Leetcode 804 唯一摩尔斯密码 C++
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: “a” 对应 “.-”, “b” 对应“-…”, “c” 对应 “-.-.”, 等等。为了方便,所有26个英文字母对应摩尔斯密码表如下:[".-","-…","-.-.","-…",".","…-.","–.","…","…",".—","-.-",".-…","–","-.","—",".–....原创 2019-06-21 16:37:50 · 304 阅读 · 0 评论 -
leetcode 203 移除链表元素 C++
删除链表中等于给定值 val 的所有节点。示例:输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5递归,36ms/** * Definition for singly-linked list. * struct ListNode { * int val; * ...原创 2019-06-21 16:32:40 · 397 阅读 · 0 评论 -
387. 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。案例:s = “leetcode” 返回 0.s = “loveleetcode”, 返回 2.注意事项:您可以假定该字符串只包含小写字母。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/first-unique-character-in-a-s...原创 2019-07-14 19:48:35 · 173 阅读 · 0 评论 -
Leetcode53 Maximum Subarray
Given an integer array nums, find the contiguous subarray (containingat least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation...原创 2019-07-29 20:50:44 · 93 阅读 · 0 评论 -
Leetcode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked‘Start’ in the diagram below).The robot can only move either down or right at any point in time. Therobot is trying to reach the bo...原创 2019-07-29 22:18:16 · 60 阅读 · 0 评论 -
Leetcod91. Decode Ways
A message containing letters from A-Z is being encoded to numbersusing the following mapping:‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given a non-empty string containingonly digits, determine the to...原创 2019-07-31 17:38:54 · 73 阅读 · 0 评论 -
Leetcode152. Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within anarray (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has...原创 2019-08-04 17:34:27 · 75 阅读 · 0 评论