
LeetCode
文章平均质量分 50
TodorovChen
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2014-01-02 16:41:03 · 524 阅读 · 0 评论 -
Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra原创 2014-01-04 20:03:18 · 496 阅读 · 0 评论 -
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?这道题用动态规划就好了。第到达第i个台阶有2种方法。1.从i原创 2014-01-04 21:29:27 · 459 阅读 · 0 评论 -
Remove Duplicates from 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 c原创 2014-01-05 14:06:38 · 435 阅读 · 0 评论 -
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] has原创 2014-01-05 14:31:06 · 444 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.比较简单,用dfs就行。 int maxDepth(TreeNod原创 2014-01-07 21:49:36 · 507 阅读 · 0 评论 -
Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321取模就ok。int reverse(int x){ int res=0; int pre=1; if(x<0) pre=-1; int absx=abs(x);原创 2014-01-07 22:02:27 · 368 阅读 · 0 评论 -
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-01-12 16:12:45 · 442 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],原创 2014-01-22 13:32:09 · 501 阅读 · 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 is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the followi原创 2014-01-22 16:28:25 · 606 阅读 · 0 评论 -
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You m原创 2014-01-22 19:43:38 · 506 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.ListNode *deleteDuplicates(原创 2014-01-22 16:06:06 · 481 阅读 · 0 评论 -
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using转载 2014-01-24 15:19:45 · 485 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题目很简单,用递归比较简洁。ListNode *mergeTwoLists(ListNode *l1, L原创 2014-01-16 19:02:18 · 423 阅读 · 0 评论 -
Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?经典的快慢指针方法。bool hasCycle(ListNode *head){ ListNode *slow = head; ListNode *f原创 2014-01-16 19:40:53 · 404 阅读 · 0 评论 -
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]]杨辉三角,比较简单,关键是pascal[i][j] = pa原创 2014-01-24 14:52:34 · 408 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.用二分法求就可以。。也可以用牛顿迭代公式。int sqrt(int x){ long long left = 0; long long right = x; while(left <= right)原创 2014-01-18 16:07:59 · 584 阅读 · 0 评论 -
Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.首先需要将string转化为vector。然后为了避免越界,num1和num2原创 2014-01-18 16:47:44 · 385 阅读 · 0 评论 -
Anagrams
Q:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.For example:Input: ["tea","and","ate","eat","den"]Output: ["te转载 2014-01-18 15:42:58 · 592 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.Note:You may onl原创 2014-01-19 13:32:39 · 499 阅读 · 0 评论 -
Binary Tree Inorder Traversal & Preorder 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].非递归遍历: vector inorderTrave转载 2014-01-19 15:24:34 · 632 阅读 · 0 评论 -
Pow(x, n)
简单的循环或者递归是不行的,会超时。采用二分法+递归可以解决。double newPow(double x, int n){ if(n==0) return 1; double d = newPow(x, n/2); if(n%2==0) return d*d; else return x*d*d;}do原创 2014-01-18 19:16:31 · 407 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3,原创 2014-01-19 14:58:10 · 580 阅读 · 0 评论 -
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-02-06 15:39:26 · 531 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2014-02-06 15:51:52 · 448 阅读 · 0 评论 -
Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr原创 2014-02-06 16:14:01 · 428 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.比较简单。vector plusOne(vector &digits){ vector answer; int c = 1; for (int i = digits.size() - 1; i >= 0; i--)原创 2014-02-08 16:09:06 · 487 阅读 · 0 评论 -
Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?求帕斯卡三角的第n行。这里有个规原创 2014-02-08 15:48:36 · 468 阅读 · 0 评论 -
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?采用递归的思想,从外到里一层层递归旋转。void rotateRecur(vector >& matrix,原创 2014-02-22 19:22:12 · 748 阅读 · 0 评论 -
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 0,原创 2014-02-22 18:57:23 · 641 阅读 · 0 评论 -
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 bo原创 2014-02-17 20:21:39 · 487 阅读 · 0 评论 -
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 vali转载 2014-04-30 14:53:17 · 375 阅读 · 0 评论 -
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", retur原创 2014-03-29 21:07:06 · 367 阅读 · 0 评论 -
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.Eac转载 2014-05-02 18:29:18 · 372 阅读 · 0 评论 -
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.int total = 0;int col[100];void dfs(int cur, int n){ if(cu原创 2014-05-02 18:44:32 · 364 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".原创 2014-05-02 19:57:19 · 431 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. TreeNode* sortedListToBSTUtil(ListNode*& cur, int start, int end){ if(start > e原创 2014-05-05 15:24:59 · 468 阅读 · 0 评论 -
Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.罗马数字转到十进制。原创 2014-04-20 19:44:10 · 401 阅读 · 0 评论 -
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 ]]You原创 2014-04-20 17:58:15 · 418 阅读 · 0 评论 -
Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.string intToRoman(int num){ int values[13] = {1000, 900, 500, 400, 100, 90, 50,原创 2014-04-20 20:12:28 · 446 阅读 · 0 评论