
leetcode
且听疯吟
做为一名奋斗的80后,至今仍在平凡之路上。
展开
-
523. Continuous Subarray Sum
class Solution {public: bool checkSubarraySum(vector<int>& nums, int k) { int n = nums.size(), sum = 0, pre = 0; unordered_set<int> modk; for (int i = 0; i < n; ++i) {原创 2017-09-18 17:19:40 · 337 阅读 · 0 评论 -
376. Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either posi原创 2017-09-19 16:53:25 · 320 阅读 · 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-09-02 23:18:46 · 136 阅读 · 0 评论 -
647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consist of原创 2017-09-19 23:43:20 · 280 阅读 · 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.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return原创 2017-09-20 00:09:01 · 230 阅读 · 0 评论 -
312. Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[lef原创 2017-09-28 09:28:21 · 187 阅读 · 0 评论 -
646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pai原创 2017-09-28 10:59:40 · 232 阅读 · 0 评论 -
176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table.+—-+——–+ | Id | Salary | +—-+——–+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +—-+——–+ For example, given the a原创 2017-09-11 22:49:39 · 156 阅读 · 0 评论 -
120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangleclass Solution {public: int m原创 2017-09-11 23:47:13 · 170 阅读 · 0 评论 -
264. Ugly Number II
class Solution {public: int nthUglyNumber(int n) { int cnt = 1; vector<int> dp(n,0); if(n == 1){ return 1; } /*index of the merge sort*/ i原创 2017-09-19 16:50:28 · 282 阅读 · 0 评论 -
115. Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of S which equals T.A subsequence of a string is a new string which is formed from the original string by deleting some (can b原创 2017-09-27 17:48:14 · 256 阅读 · 0 评论 -
138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list./** * Definition for singly-原创 2017-09-02 00:02:40 · 148 阅读 · 0 评论 -
69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x.int mySqrt(int x) { double pre = 0; double cur = x; // 这里从x开始 从x/2开始会导致 1 不能满足 x(n+1)= xn - f'(xn)/f(xn)原创 2017-09-10 23:03:19 · 148 阅读 · 0 评论 -
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not a palin原创 2017-09-10 22:56:47 · 174 阅读 · 0 评论 -
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A 2 -> B 3 -> C … 26 -> Z 27 -> AA 28 -> ABclass Solution {public原创 2017-09-11 00:02:59 · 175 阅读 · 0 评论 -
132. Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return 1 since t原创 2017-09-27 00:36:15 · 199 阅读 · 0 评论 -
413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1,原创 2017-09-19 09:33:53 · 438 阅读 · 0 评论 -
32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()", which has原创 2017-09-27 10:32:49 · 217 阅读 · 0 评论 -
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, retu原创 2017-09-19 12:57:26 · 298 阅读 · 0 评论 -
8. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible原创 2017-09-01 17:38:22 · 162 阅读 · 0 评论 -
368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.If there are multiple solutions, return原创 2017-09-28 12:51:15 · 275 阅读 · 0 评论 -
474. Ones and Zeroes
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.For now, suppose you are a dominator of m 0s and n 1s respectively. On the other han原创 2017-09-28 13:13:02 · 195 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 s原创 2017-09-12 13:26:00 · 167 阅读 · 0 评论 -
354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the原创 2017-09-29 16:54:54 · 234 阅读 · 0 评论 -
343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, return 1原创 2017-09-21 17:07:37 · 165 阅读 · 0 评论 -
304. Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).Range Sum Query 2DThe above rectangle (wit原创 2017-09-21 23:56:38 · 166 阅读 · 0 评论 -
576. Out of Boundary Paths
There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However,原创 2017-09-22 08:18:44 · 642 阅读 · 0 评论 -
139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assum原创 2017-09-22 12:43:54 · 208 阅读 · 0 评论 -
72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) In原创 2017-10-12 12:24:11 · 343 阅读 · 0 评论 -
文章标题
Given a non-empty list of words, return the k most frequent elements.Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower原创 2017-11-01 23:12:11 · 220 阅读 · 0 评论 -
742. Closest Leaf in a Binary Tree
Given a binary tree where every node has a unique value, and a target key k, find the value of the closest leaf node to target k in the tree.Here, closest to a leaf means the least number of edges trav原创 2017-12-18 23:08:58 · 272 阅读 · 0 评论 -
300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore t原创 2017-09-21 10:11:59 · 195 阅读 · 0 评论 -
213. House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all原创 2017-09-21 00:47:46 · 196 阅读 · 0 评论 -
516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.Example 1:Input:"bbbab"Output:4One possible longest palindromic subsequ原创 2017-09-21 00:31:02 · 160 阅读 · 0 评论 -
662. Maximum Width of Binary Tree
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binar原创 2017-09-03 15:37:33 · 346 阅读 · 0 评论 -
437. 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 must go原创 2017-09-03 18:05:43 · 139 阅读 · 0 评论 -
113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22,/** * Definition for a binary tree原创 2017-09-03 18:07:09 · 154 阅读 · 0 评论 -
338. 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:For num = 5 you sho原创 2017-09-12 14:59:12 · 166 阅读 · 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.这道题目非常简单的题目,用递归或者队列非常容易做原创 2017-09-28 21:46:53 · 260 阅读 · 0 评论 -
309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and原创 2017-09-12 17:34:45 · 216 阅读 · 0 评论