leetcode
文章平均质量分 59
喜欢恋着风
这个作者很懒,什么都没留下…
展开
-
找出数组中重复元素
题目描述一个大小为n的数组,里面的数都属于范围[0, n-1],有不确定的重复元素,找到至少一个重复元素; 要求O(1)空间和O(n)时间;方法1因为数组长度为n,且数的范围为[0~n-1],且包含重复元素,故将数组中的每个元素置位到和坐标值相同的位置,0->0,1->1,2->2,以此类推,如果存在重复元素,则必然会使某两个位置上的元素值相同。int find_rongyu(vector<int原创 2017-05-24 11:39:30 · 1337 阅读 · 0 评论 -
基数排序
基数排序描述基数排序(以整形为例),将整形10进制按每位拆分,然后从低位到高位依次比较各个位。主要分为两个过程: 分配:先从个位开始,根据位值(0-9)分别放在0~9号桶中; 收集:将放置在0~9号桶中的数据按顺序放在数组中 重复1,2过程,从个位到最高位(比如32位无符号整形最大数为4147483647,最高位是10位) 以[521, 310, 72, 373, 15,原创 2017-05-24 15:20:10 · 241 阅读 · 0 评论 -
594. Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.Now, given an integer array, you need to find the length of its longest harmon原创 2017-05-25 19:38:14 · 285 阅读 · 0 评论 -
539. Minimum Time Difference
Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimum minutes difference between any two time points in the list.Example 1: Input: [“23:59”,”00:00”] Output: 1 Note: T原创 2017-05-25 20:33:40 · 397 阅读 · 0 评论 -
567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string.Example 1原创 2017-05-25 21:38:42 · 233 阅读 · 0 评论 -
515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.Example: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]不明白这样的题目还是medium,所以直接提交代码了。。。 主要是利用了一个队列,然后每次找出一层原创 2017-05-26 14:39:56 · 218 阅读 · 0 评论 -
501. Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.Assume a BST is defined as follows:The left subtree of a node contains onl原创 2017-05-26 14:44:28 · 256 阅读 · 0 评论 -
504. Base 7
Given an integer, return its base 7 string representation.Example 1: Input: 100 Output: “202” Example 2: Input: -7 Output: “-10” Note: The input will be in range of [-1e7, 1e7].给出一个数字,找出其七进制的字符串表原创 2017-05-26 14:47:10 · 185 阅读 · 0 评论 -
593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a square.The coordinate (x,y) of a point is represented by an integer array with two integers.Example:原创 2017-05-26 20:27:21 · 658 阅读 · 0 评论 -
582. Kill Process
Given n processes, each process has a unique PID (process id) and its PPID (parent process id).Each process only has one parent process, but may have one or more children processes. This is just like a原创 2017-05-26 20:32:24 · 449 阅读 · 0 评论 -
592. Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your fina原创 2017-05-26 21:34:34 · 290 阅读 · 0 评论 -
494. 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 many w原创 2017-05-27 15:17:57 · 200 阅读 · 0 评论 -
566. Reshape the Matrix
In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dimensio原创 2017-05-27 20:24:14 · 195 阅读 · 0 评论 -
463. 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 s原创 2017-05-27 20:36:59 · 161 阅读 · 0 评论 -
71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c”click to show corner cases. Corner Cases:Did you consider the ca原创 2017-05-31 09:08:55 · 186 阅读 · 0 评论 -
560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2Note:The length of原创 2017-05-31 09:13:02 · 336 阅读 · 0 评论 -
606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. And you nee原创 2017-06-04 20:22:05 · 1119 阅读 · 0 评论 -
79. 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 neig原创 2017-06-05 09:14:53 · 182 阅读 · 0 评论 -
10. Regular Expression Matching
Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire i原创 2017-06-05 09:17:03 · 208 阅读 · 0 评论 -
617. Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. T原创 2017-06-15 21:17:45 · 179 阅读 · 0 评论 -
611. Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example 1:Inpu原创 2017-06-15 21:23:57 · 233 阅读 · 0 评论 -
572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’原创 2017-05-31 21:26:47 · 266 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string “23”原创 2017-05-31 21:29:31 · 177 阅读 · 0 评论 -
525. Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray原创 2017-05-31 21:34:14 · 197 阅读 · 0 评论 -
513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 3 Output: 1 Example 2: Input: 1 / \ 2 3 / / \4 5 6原创 2017-06-05 21:17:17 · 173 阅读 · 0 评论 -
535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURLTinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it retur原创 2017-06-01 21:33:16 · 395 阅读 · 0 评论 -
524. Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return原创 2017-06-01 22:22:34 · 130 阅读 · 0 评论 -
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde原创 2017-06-01 22:42:53 · 151 阅读 · 0 评论 -
60. Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): “123” “132” “213” “23原创 2017-06-02 09:13:42 · 165 阅读 · 0 评论 -
337. 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,原创 2017-06-06 21:08:50 · 212 阅读 · 0 评论 -
11. Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2017-06-06 21:09:58 · 161 阅读 · 0 评论 -
84. Largest Rectangle in Histogram
Given n non-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 each ba原创 2017-06-06 21:15:23 · 182 阅读 · 0 评论 -
599. Minimum Index Sum of Two Lists
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.You need to help them find out their common interest with the lea原创 2017-06-02 21:01:57 · 387 阅读 · 0 评论 -
598. Range Addition II
Given an m * n matrix M initialized with all 0’s and several update operations.Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b原创 2017-06-02 22:19:00 · 364 阅读 · 0 评论 -
341. Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.Example 1: Given the list [[1,1],原创 2017-06-02 22:21:27 · 203 阅读 · 0 评论 -
动态规划----各种最长序列
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the原创 2017-06-19 14:56:26 · 194 阅读 · 0 评论 -
131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = “aab”, Return [ [“aa”,”b”],原创 2017-06-03 20:47:54 · 221 阅读 · 0 评论 -
51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle.Each s原创 2017-06-03 20:50:52 · 224 阅读 · 0 评论 -
52. N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. 思路:此题虽然是N皇后问题的变体,但还是可以用N皇后的思路去解决,只是用了一个set来存储合法的皇后bool isNQueens(vector<int>原创 2017-06-03 22:10:06 · 204 阅读 · 0 评论 -
494. 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 many w原创 2017-06-20 11:18:55 · 175 阅读 · 0 评论