![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
Shane Zhao
CV
展开
-
leetcode139 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。说明:拆分时可以重复使用字典中的单词。你可以假设字典中没有重复的单词。示例 1:输入: s = “leetcode”, wordDict = [“leet”, “code”]输出: true解释: 返回 true 因为 “leetcode” 可以被拆分成...原创 2019-09-14 17:29:21 · 209 阅读 · 0 评论 -
编码字符串,使得总长度最短
字符串“liulishuo”,我们需要对该字符串每个字符编码,使得编码后的字符串总长度最小。我们可以通过哈夫曼树来对字符编码,累积树中非叶子节点的和即为所求总长度。总长度也等于词频1字符长度1+词频2字符长度2+…/*liulishuo23对字符串编码,使得编码后的总长度最短;哈夫曼编码,总长度=字符长度1*频数1+字符长度2*频数2利用最小堆找最小元素*/int MinLe...原创 2019-08-01 11:49:24 · 2542 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100该题简单的循环累乘会超时,时间复杂度O(n).不够快!可以用分治法将复杂度...原创 2019-08-01 11:01:27 · 107 阅读 · 0 评论 -
二叉树非递归遍历方式
前序遍历:root,left,right用栈实现非递归遍历,访问root,将right先存栈,然后left再存栈vector<int> preorderTraversal(TreeNode* root) { stack<TreeNode*> s; vector<int> res; if (!root) retu...原创 2019-07-26 17:54:21 · 131 阅读 · 0 评论 -
3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contai...原创 2019-07-25 19:39:50 · 130 阅读 · 0 评论 -
Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window m...原创 2019-07-20 19:34:23 · 89 阅读 · 0 评论 -
Decode String
Given an encoded string, return its decoded string.The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guara...原创 2019-07-17 21:17:00 · 135 阅读 · 0 评论 -
703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.Your KthLargest class will have a constructor whi...原创 2019-07-20 12:27:06 · 112 阅读 · 0 评论 -
Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]输出数组内最重复的k个数。emmm,...原创 2019-07-24 19:39:19 · 107 阅读 · 0 评论 -
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-in...原创 2019-07-20 10:58:02 · 122 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity.Example:Input: [100, 4, 200, 1, 3, 2]Output: 4Explan...原创 2019-08-01 15:35:01 · 120 阅读 · 0 评论 -
Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The rig...原创 2019-07-28 21:11:58 · 82 阅读 · 0 评论 -
最长回文子串
dabadcmnm最长回文子串为dabad此题有多种解法,暴力法就是逐个子串来比较,记录最长子串,复杂度太高。动态规划当子串是回文子串时候,我们只需要看两遍新添的两个字符是否相等,若相等则最新串也是最长子串;若不等,则新串不是回文子串。这也是此题的状态转移方程。例如,aba是回文子串,当aba子串两端新添的两个字符相等时候,新串dabad也是回文子串。我们可以用一个二维数组dp记录所有的...原创 2019-08-30 17:14:53 · 179 阅读 · 0 评论 -
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 constraint stopping you from robbing each of them is that adjacent house...原创 2019-08-28 14:34:02 · 145 阅读 · 0 评论 -
322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...原创 2019-08-26 17:27:54 · 102 阅读 · 0 评论 -
数组中超过一半的数
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。这个题目看到了首先会想到排序,然后统计个数。O(nlogn)了,不够快超过一半的数字肯定会是中位数,尝试快排的思想,找到中位数即可。这儿遇到一个坑,就是写partition函数的时候nu...原创 2019-08-21 10:50:25 · 578 阅读 · 0 评论 -
Dungeon Game
The demons had captured the princess § and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially posit...原创 2019-08-23 20:34:54 · 181 阅读 · 0 评论 -
链表反转+交叉链接
给个链表1->2->3->4->5->6把它变成1->6->2->5->3->4先把后半段链表反转,然后交叉链接前后半段链表即可#include <iostream>#include <iomanip>#include <vector>#include <algorithm>#...原创 2019-08-14 11:01:18 · 419 阅读 · 0 评论 -
链表快排
快排的核心在于根据哨兵将节点二分,使用双指针分别从头和尾开始遍历即可完成。可是链表的访问时单向的,无法使用一个指针从后往前访问节点。换个思路来解,我们的目的还是要将数据二分嘛。还是用双指针,慢指针 i 指向头结点,快指针 j 从第二个节点开始遍历,一旦遇到节点值小于哨兵的key,那说明 j 指向的节点应该在哨兵前,这时候我们将慢指针后移一步,然后替换 i 和 j 的值。替换后保证了 j 的值大于...原创 2019-08-13 23:11:35 · 203 阅读 · 0 评论 -
翻转单词序列
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?这道题用栈来解非常简单,坑在于牛客...原创 2019-08-08 17:36:11 · 86 阅读 · 0 评论 -
八皇后问题
八皇后问题,太经典了。这儿记录一个回溯写法。回溯法适用于求解含有多个步骤的问题,且每个步骤都有多个选择,每次我们只选一个选择去求解,若达到解的条件则为一个解,若无解那么回到上一步换一个选择继续求解。八个皇后在水平竖直对角方向都不能共线,那这肯定作为求解过程中的限制。我们用一个数组记录每个皇后在对应行上的位置。假设棋盘有n格,每个皇后就有n个选择,每次选一个,然后判断该皇后在这儿会不会与已有皇...原创 2019-08-02 10:59:59 · 110 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: ...原创 2019-07-17 09:50:25 · 78 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between t...原创 2019-07-29 00:06:07 · 101 阅读 · 0 评论 -
Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.Grid cells are connected horizontally/vertically (not diagonally). The grid is completely ...原创 2019-07-16 21:12:10 · 119 阅读 · 0 评论 -
奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in pl...原创 2019-06-08 12:57:25 · 129 阅读 · 0 评论 -
回文链表
Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueFollow up:Could you do it in O(n) time and O(1)...原创 2019-06-08 12:53:16 · 105 阅读 · 0 评论 -
合并两个有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->...原创 2019-06-08 12:52:03 · 90 阅读 · 0 评论 -
Reverse Linked List
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursively. ...原创 2019-06-06 16:24:41 · 156 阅读 · 0 评论 -
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-in...原创 2019-06-06 15:41:01 · 71 阅读 · 0 评论 -
Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,...原创 2019-05-27 21:53:01 · 109 阅读 · 0 评论 -
Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.Example:Input: s = 7, nums...原创 2019-05-27 16:43:56 · 103 阅读 · 0 评论 -
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: [“flower”,“flow”,“flight”]Output: “fl...原创 2019-05-27 14:57:57 · 102 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = “11”, b = “1”Output: “100”Example 2...原创 2019-05-27 14:48:01 · 121 阅读 · 0 评论 -
两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> ...原创 2019-06-10 09:33:32 · 121 阅读 · 0 评论 -
旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。示例 1:输入: 1->2->3->4->5->NULL, k = 2输出: 4->5->1->2->3->NULL解释:向右旋转 1 步: 5->1->2->3->4->NULL向右旋转 2 步: 4->5-&...原创 2019-06-10 11:56:37 · 100 阅读 · 0 评论 -
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, you sho...原创 2019-07-19 09:50:47 · 108 阅读 · 0 评论 -
Reverse Pairs
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].You need to return the number of important reverse pairs in the given array.Example1:Input: [1,3...原创 2019-07-12 11:11:37 · 137 阅读 · 0 评论 -
归并排序
归并排序C++实现归并排序用的是分治法,分治法往往用递归来实现。分治法的核心步骤分解问题当子问题能直接求解时,求解子问题合并子问题的解#include <iostream>#include <vector> #include <string>#include <algorithm>#include <cmath>...原创 2019-07-11 23:48:37 · 108 阅读 · 0 评论 -
Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-07-18 10:07:58 · 222 阅读 · 0 评论 -
旋转数组的最小数字
题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。这道题是剑指offer上的题目,坑多,且想到O(logn)时间复杂度的不容易。旋转数组基本有序,那可以考虑用...原创 2019-07-15 21:41:06 · 100 阅读 · 0 评论