leetcode
帆的孤独啊
这个作者很懒,什么都没留下…
展开
-
矩阵上折半查找
class Solution {2public:3 bool searchMatrix(vector<vector<int>>& matrix, int target) {4 // 先折半定位第几行, 在折半定位第几行5 if(matrix.size() == 0) return false;6 ...原创 2018-11-16 13:59:13 · 126 阅读 · 0 评论 -
Longest Moutain
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold:B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] <...原创 2018-11-24 19:57:41 · 115 阅读 · 0 评论 -
前缀树
class Trie {public: // 定义字典树 : 根节点为空 其余节点时一个字符 struct TrieNode{ // 字典树一个节点 bool isEnd; TrieNode *child[26];//记录经过该节点的子树 没有则即为NULL TrieNode():isEnd(false){ ...原创 2019-02-10 15:40:37 · 190 阅读 · 0 评论 -
Find the Duplicate Number
// 1-n 所以可以把值当做下标索引 或者链表中的next 这道题就变成判断是否有环存在class Solution {public: int findDuplicate(vector<int>& nums) { int slow = nums[0], fast=nums[nums[0]]; while(slow != fast...原创 2019-03-02 20:18:56 · 209 阅读 · 0 评论 -
34. Find First and Last Position of Element in Sorted Array
Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).If the...原创 2019-03-02 21:11:58 · 191 阅读 · 0 评论 -
438. Find All Anagrams in a String
Given a stringsand anon-emptystringp, find all the start indices ofp's anagrams ins.Strings consists of lowercase English letters only and the length of both stringssandpwill not be larger...原创 2019-03-02 22:15:55 · 69 阅读 · 0 评论 -
207. Course Schedule
There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair...原创 2019-03-06 21:39:37 · 70 阅读 · 0 评论 -
295. Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.For example,[2,3,4], the median ...原创 2019-03-06 23:02:16 · 66 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Note:Division between two integers ...原创 2019-03-07 19:57:39 · 99 阅读 · 0 评论 -
Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it mus...原创 2019-03-15 13:46:09 · 70 阅读 · 0 评论 -
Product of Array Except Self("山"形遍历 两个方向各遍历一次)
Given an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Example:Input: [1,2,3,4]Output: ...原创 2019-03-15 14:29:58 · 116 阅读 · 0 评论 -
counting bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example 1:Input: 2Output...原创 2019-03-15 20:09:26 · 84 阅读 · 0 评论 -
Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix dire...原创 2019-03-15 20:46:17 · 113 阅读 · 0 评论 -
Search a 2D Matrix II(观察矩阵规律)
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each...原创 2019-03-15 20:58:04 · 145 阅读 · 0 评论 -
Largest Rectangle in Histogram(对单调栈的理解!!!!重要)
Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of ea...原创 2019-03-11 18:58:20 · 177 阅读 · 0 评论 -
Largest Number (巧妙的排序)
Given a list of non negative integers, arrange them such that they form the largest number.Example 1:Input: [10,2]Output: "210"Example 2:Input: [3,30,34,5,9]Output: "9534330"/* * [3,30...原创 2019-03-11 19:23:15 · 242 阅读 · 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 keysless thanthe node's key. The ...原创 2019-03-11 19:56:38 · 82 阅读 · 0 评论 -
Word Ladder
Given two words (beginWordandendWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWordtoendWord, such that:Only one letter can be changed at a ti...原创 2019-03-11 21:40:53 · 134 阅读 · 0 评论 -
Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could ...原创 2019-03-20 16:19:04 · 99 阅读 · 0 评论 -
Generate Parentheses(递归深搜当成二叉树处理)
Given an array of integersnumsand a positive integerk, find whether it's possible to divide this array intoknon-empty subsets whose sums are all equal.Example 1:Input: nums = [4, 3, 2, 3,...原创 2019-03-20 19:10:37 · 122 阅读 · 0 评论 -
Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols+and-. For each integer, you should choose one from+and-as its new symbol.Find out how m...原创 2019-03-17 17:53:48 · 89 阅读 · 0 评论 -
Partition Equal Subset Sum
Given anon-emptyarray containingonly positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array eleme...原创 2019-03-17 16:49:38 · 86 阅读 · 0 评论 -
Sqrt(x)
Implementint sqrt(int x).Compute and return the square root ofx, wherexis guaranteed to be a non-negative integer.Since the return typeis an integer, the decimal digits are truncated and only...原创 2019-03-12 19:50:43 · 87 阅读 · 0 评论 -
Top K Frequent Elements (set 和 map 使用)
Given a non-empty array of integers, return thekmost frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]class Sol...原创 2019-03-12 21:34:23 · 103 阅读 · 0 评论 -
八皇后问题
问题介绍及参考我的解法:// 八皇后问题 递归回溯 同时记得返回现场// 皇后必然放在不同的行 check时考虑列即可 而且在check时只需要check 刚刚下过的上面的部分bool check(vector<vector<int>> &visit, int row, int col){// 对第i个位置沿竖直水平斜线方向检测 int len...原创 2019-03-24 19:41:13 · 94 阅读 · 0 评论 -
Sort List 在链表上进行归并排序
Sort a linked list inO(nlogn) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1-...原创 2019-03-13 10:38:20 · 128 阅读 · 0 评论 -
层次遍历(记录层数) 引入哨兵的简化计算 Binary Tree Zigzag Level Order Traversal
以 求二叉树 奇数层和偶数层 的层次遍历和为例class Solution {public: /* 层次遍历找奇数层和偶树层 哪个最大 ? * oddsum evensum * 虽然这道题的思路错了 但是提供了一个很好的记录层数的层次遍历方法 : 引入哨兵 * */ int rob(TreeNode* root) { // ...原创 2019-03-22 12:41:25 · 880 阅读 · 0 评论 -
House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour...原创 2019-03-22 13:16:07 · 59 阅读 · 0 评论 -
Find All Numbers Disappeared in an Array(设立哨兵有一定代价 但是如果输入部分的数据是自己实现的就还好)
Given an array of integers where 1 ≤ a[i] ≤n(n= size of array), some elements appear twice and others appear once.Find all the elements of [1,n] inclusive that do not appear in this array.Coul...原创 2019-03-13 19:34:06 · 104 阅读 · 0 评论 -
Symmetric Tree(搞清判断对称的条件)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree[1,2,2,3,4,4,3]is symmetric: 1 / \ 2 2 / \ / \3 4 4 3...原创 2019-03-13 20:08:34 · 144 阅读 · 0 评论 -
permutations (dfs递归)
Given a collection ofdistinctintegers, return all possible permutations.Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]class Solution {pu...原创 2019-03-18 21:15:10 · 160 阅读 · 0 评论 -
Subsets
Given a set ofdistinctintegers,nums, return all possible subsets (the power set).Note:The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3], [1...原创 2019-03-18 21:34:49 · 419 阅读 · 2 评论 -
Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), wherehis the height of the person andkis the number of people in front of this ...原创 2019-03-25 19:23:42 · 165 阅读 · 0 评论 -
221. Maximal Square (递归 对以及 避免特判的写法)
Given a 2D binary matrix filled with 0's and 1's, find the largest square 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: 4class Solut...原创 2019-03-22 14:12:28 · 67 阅读 · 0 评论 -
Kth Smallest Element in a Sorted Matrix
Given anxnmatrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not t...原创 2019-03-25 20:53:27 · 69 阅读 · 0 评论 -
Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there existsi, j, ksuch thatarr[i]<arr[...原创 2019-03-25 21:23:44 · 85 阅读 · 0 评论 -
Word Search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n...原创 2019-03-22 19:22:03 · 91 阅读 · 0 评论 -
Add Two Numbers(链表实现大数相加 不占用额外辅助空间)
核心思想: 固定一个较长的链表You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add the two num...原创 2019-03-22 19:23:33 · 115 阅读 · 0 评论 -
Longest Substring with At Least K Repeating Characters(bit mask)
Find the length of the longest substringTof a given string (consists of lowercase letters only) such that every character inTappears no less thanktimes.Example 1:Input:s = "aaabb", k = 3...原创 2019-03-29 21:04:41 · 78 阅读 · 0 评论 -
Longest Valid Parentheses
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid...原创 2019-04-03 20:43:47 · 133 阅读 · 0 评论