LeetCode
kassical
研发工程师
展开
-
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.原创 2014-08-13 21:05:02 · 397 阅读 · 0 评论 -
LeetCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?斐波那契数列原创 2014-08-13 21:53:54 · 440 阅读 · 0 评论 -
LeetCode Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2014-08-14 00:34:22 · 369 阅读 · 0 评论 -
LeetCode Unique Binary Search Trees
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 \原创 2014-08-15 00:27:06 · 375 阅读 · 0 评论 -
LeetCode Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2014-08-27 23:36:34 · 453 阅读 · 0 评论 -
LeetCode Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2014-08-14 23:43:28 · 378 阅读 · 0 评论 -
LeetCode Plus One
Plus One原创 2014-08-13 23:57:03 · 402 阅读 · 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 at原创 2014-08-28 01:10:43 · 468 阅读 · 0 评论 -
LeetCode Longest Palindromic Substring
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.参考:原创 2014-08-27 21:33:19 · 558 阅读 · 0 评论 -
LeetCode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio原创 2014-08-14 23:59:01 · 428 阅读 · 0 评论 -
LeetCode Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2014-08-13 22:42:00 · 489 阅读 · 0 评论 -
LeetCode Pow(x, n)
Implement pow(x, n).思路原创 2014-08-13 21:40:40 · 373 阅读 · 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原创 2014-08-26 22:13:16 · 495 阅读 · 0 评论 -
LeetCode 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.Th原创 2014-08-13 00:45:24 · 373 阅读 · 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)).这题时间fuza原创 2014-08-13 00:15:43 · 351 阅读 · 0 评论 -
LeetCode Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2014-08-12 21:35:10 · 417 阅读 · 0 评论 -
LeetCode Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements fro原创 2014-08-13 11:40:36 · 394 阅读 · 0 评论 -
LeetCode Maximum Subarray
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原创 2014-08-25 22:10:51 · 361 阅读 · 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, the原创 2014-08-26 00:41:23 · 498 阅读 · 0 评论 -
LeetCode 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, 5 / \原创 2014-08-12 17:11:10 · 370 阅读 · 0 评论 -
LeetCode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.注意yez原创 2014-08-12 22:06:17 · 517 阅读 · 0 评论 -
LeetCode 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2014-08-13 17:44:40 · 355 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.class Solution {public: const double EPSILON = 1e-6; int sqrt(int x) { if (x == 0 || x == 1) return x; double原创 2014-08-25 22:04:51 · 426 阅读 · 0 评论 -
LeetCode Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solution {public:原创 2014-08-14 23:10:29 · 389 阅读 · 0 评论 -
LeetCode Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin原创 2014-08-27 21:54:22 · 472 阅读 · 0 评论 -
LeetCode Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2014-08-27 23:08:00 · 430 阅读 · 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 binary原创 2014-08-17 21:58:27 · 366 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * struct TreeNode {原创 2014-08-17 22:42:02 · 458 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.原创 2014-08-17 23:02:43 · 506 阅读 · 0 评论 -
LeetCode Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2014-08-30 23:34:01 · 535 阅读 · 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.原创 2014-08-30 22:03:37 · 566 阅读 · 0 评论 -
LeetCode 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.原创 2014-08-30 23:03:57 · 473 阅读 · 0 评论 -
LeetCode N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.class Solution {public: int totalNQueens(int n) { vector ivec;原创 2014-08-30 23:39:02 · 482 阅读 · 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原创 2014-09-24 00:58:52 · 641 阅读 · 0 评论 -
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.class Solution {public: string longestCommonPrefix(vector &strs) { if (strs.size() == 0) return ""; st原创 2014-08-18 23:12:11 · 559 阅读 · 0 评论 -
LeetCode Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2014-09-01 23:00:12 · 564 阅读 · 0 评论 -
LeetCode Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2014-08-17 21:43:23 · 378 阅读 · 0 评论 -
LeetCode Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2014-08-17 21:26:11 · 432 阅读 · 0 评论 -
LeetCode Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2014-08-28 20:06:48 · 476 阅读 · 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", r原创 2014-08-28 22:06:33 · 467 阅读 · 0 评论