![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
文章平均质量分 87
liuwenyirachel
这个作者很懒,什么都没留下…
展开
-
[Leetcode] Max Tree Sum
不多说了~/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */c原创 2013-07-16 16:27:39 · 451 阅读 · 0 评论 -
[Leetcode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.class Solution{public: ListNo原创 2013-03-10 10:19:50 · 386 阅读 · 0 评论 -
[Leetcode] Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, t原创 2013-03-10 08:26:40 · 370 阅读 · 0 评论 -
[Leetcode] Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2013-03-10 13:28:39 · 392 阅读 · 0 评论 -
[Leetcode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binar原创 2013-03-10 09:16:32 · 389 阅读 · 0 评论 -
[Leetcode] 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原创 2013-03-10 05:13:01 · 389 阅读 · 0 评论 -
[Leetcode] Merge K sorted lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.二分法可以降低复杂度。一遍通过,耶~/** * Definition for singly-linked list. * struct ListNode { * int va原创 2013-03-11 03:32:38 · 415 阅读 · 0 评论 -
[Leetcode] Jump Game ii
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to原创 2013-02-16 03:36:28 · 235 阅读 · 0 评论 -
[Leetcode] Remove Duplicates in Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2013-02-27 11:16:35 · 157 阅读 · 0 评论 -
[Leetcode] Divide Two Integers
Divide two integers without using multiplication, division and mod operator.用 unsigned int 可以解决溢出问题class Solution {public: int divide(int dividend, int divisor) { // Start typing your原创 2013-02-27 10:38:15 · 231 阅读 · 0 评论 -
[Leetcode] 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原创 2013-03-10 03:36:40 · 543 阅读 · 0 评论 -
[Leetcode] Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?剥洋葱大法~注意有时候算层数时候要记得处理余下的一层,这题不需要。例如size=5,一共有两个外层原创 2013-03-11 00:56:00 · 645 阅读 · 0 评论 -
[Leetcode] Pow(x,n)
Implement pow(x, n).class Solution {public: double pow(double x, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n==0) return 1原创 2013-03-10 23:35:49 · 446 阅读 · 0 评论 -
[Leetcode] Maximum Subarrary
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] ha原创 2013-03-10 14:32:55 · 477 阅读 · 0 评论 -
[Leetcode] reverse nodes in K-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2013-03-10 01:32:30 · 433 阅读 · 0 评论 -
[Leetcode] Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.牛顿迭代class Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT w原创 2013-03-08 13:02:44 · 417 阅读 · 0 评论 -
[Leetcode] Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路是二分法。这样似乎可以降低一些复杂度(存疑)。分析见:http://blog.unieagle.net/2012/10/05/leetcode%E9%A2%98%E7%9B%AE%EF%B原创 2013-02-28 03:19:54 · 238 阅读 · 0 评论 -
[Leetcode] Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).这道题挺难的~二分法,代码与解释参考:http://f原创 2013-02-28 12:02:52 · 250 阅读 · 0 评论 -
[Leetcode] Convert Sorted List to BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.起初觉得这道题和 Convert Sorted Array to BST 是一样的,但是如果要用那个二分法的算法,则需要将 list 先处理成 array。这样一来,原创 2013-03-03 04:11:52 · 333 阅读 · 0 评论 -
[Leetcode] Convert Sorted Array to BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.简单,二分法。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left原创 2013-03-03 03:59:38 · 226 阅读 · 0 评论 -
[Leetcode] Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right原创 2013-03-04 11:05:09 · 212 阅读 · 0 评论 -
[Leetcode] Populating Next Right Pointers in Each Node ii
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constan原创 2013-03-02 21:32:11 · 138 阅读 · 0 评论 -
[Leetcode] Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without原创 2013-03-03 03:32:16 · 176 阅读 · 0 评论 -
[Leetcode] Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in t原创 2013-03-02 12:31:06 · 150 阅读 · 0 评论 -
[Leetcode] Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the c原创 2013-03-02 03:48:48 · 185 阅读 · 0 评论 -
[Leetcode] 4 Sum [Unsolved]
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2013-03-02 04:46:38 · 172 阅读 · 0 评论 -
[Leetcode] Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2013-03-02 02:57:26 · 354 阅读 · 0 评论 -
[Leetcode] Remove Duplicates from Sorted Arrary II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?善用返回值!class Solution {public: int removeDuplicates(int A[], int n) { // Start typing your C/C++原创 2013-03-02 10:32:37 · 171 阅读 · 0 评论 -
[Leetcode] 2 Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2013-03-02 05:20:34 · 217 阅读 · 0 评论 -
[Leetcode] 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 input st原创 2013-03-02 00:24:17 · 176 阅读 · 0 评论 -
[Leetcode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover t原创 2013-03-01 05:08:58 · 176 阅读 · 0 评论 -
[Leetcode] Longest Palindromic Substring [Unsolved]
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.This problem is an原创 2013-03-01 02:23:51 · 198 阅读 · 0 评论 -
[Leetcode] 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 w原创 2013-02-28 04:30:00 · 190 阅读 · 0 评论 -
[Leetcode] Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.终于鼓起勇气把KMP做了。推荐不熟悉KMP的童鞋看这篇帖子:http://www.matrix67.com/blog/archive原创 2013-03-09 15:11:50 · 515 阅读 · 0 评论 -
[Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain d原创 2013-03-08 04:11:02 · 583 阅读 · 0 评论 -
[Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2013-03-04 12:54:42 · 274 阅读 · 0 评论 -
[Leetcode] Unique BST II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2013-03-04 08:57:54 · 332 阅读 · 0 评论 -
[Leetcode] Unique BST I
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1原创 2013-03-04 06:25:21 · 191 阅读 · 0 评论 -
[Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc",原创 2013-03-04 05:26:22 · 157 阅读 · 0 评论 -
[Leetcode] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be n原创 2013-03-04 03:17:32 · 223 阅读 · 0 评论