LeetCode OJ
文章平均质量分 64
Cuphrody
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
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原创 2013-10-23 09:44:47 · 738 阅读 · 0 评论 -
Path Sum (路径和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2014-03-04 16:25:40 · 517 阅读 · 0 评论 -
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-03-05 17:03:38 · 533 阅读 · 0 评论 -
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.给定先序序列和中序序列建立二叉树, 递归解决, 按顺序扫描先序序列,每一次扫描的第一个节点作为根,然后在中序序原创 2014-03-06 09:31:44 · 659 阅读 · 0 评论 -
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-03-06 09:53:14 · 732 阅读 · 0 评论 -
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.题意是说给定一个k >= 0 把链表循环右移k位, 这原创 2014-03-06 10:36:08 · 1033 阅读 · 0 评论 -
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:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *原创 2014-03-07 09:47:19 · 548 阅读 · 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.题目描述很简单,就是说把一个升序链表转换成平衡二叉排序树,即 最优二叉排序树, 最优二叉排序树的搜索时间为logn 即树的高度, 因为链表很难处理,直接转换成数组比较容易原创 2014-03-05 10:29:54 · 595 阅读 · 0 评论 -
Insertion Sort List(链表插入排序)
Sort a linked list using insertion sort.这里给单链表进行插入排序,个人想到两种方法 一种是交换节点的指针,一种是交换节点的值,前者要判断头节点,感觉比较麻烦,就用了交换值的方法, 通常的插入排序是在插入点的位置依次与前面的元素相比较,如果比前面的小,就把当前的位置的值变成前一个位置的值,最后得到的是最终插入位置, 这里单链表没办法往前搜索,那么就从头原创 2014-03-04 15:38:45 · 696 阅读 · 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,原创 2013-10-23 15:07:51 · 761 阅读 · 0 评论 -
Text Justification(文本对齐)
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i原创 2013-10-23 21:00:10 · 746 阅读 · 0 评论 -
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.对于给定的二叉树,求最小高度(最小高度为根节点到最近的叶子节点的原创 2013-10-30 08:47:54 · 852 阅读 · 0 评论 -
Binary Tree Preorder Traversal(二叉树先序遍历)
阔别了已久的leetcode 又回来了。Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].原创 2014-03-03 20:41:51 · 591 阅读 · 0 评论 -
Binary Tree Postorder Traversal (二叉树后序遍历 非递归)
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solut原创 2014-03-04 10:04:40 · 531 阅读 · 0 评论 -
Plus One (加一)
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.这个题就不说了,加法操作。。原创 2014-03-04 14:24:18 · 532 阅读 · 0 评论 -
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原创 2014-03-05 09:51:28 · 661 阅读 · 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.罗马数字转成整数的题目,题目规定数字在1-3999之间,罗马数字用7个字母表示:I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、M(1000) 规则原创 2013-10-21 22:09:07 · 1283 阅读 · 1 评论 -
Longest Common Prefix (最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings.求一个字符串数组中的所有字符串的最长公共前缀,这个就一个一个字符串从头比较就行了。class Solution {public: string longestCommonPrefix(vector &s原创 2014-03-07 10:25:19 · 2012 阅读 · 1 评论 -
Set Matrix Zeroes(矩阵置0)
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-03-10 08:36:19 · 571 阅读 · 0 评论 -
Surrounded Regions (包围区域)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O XX X原创 2014-03-27 10:48:21 · 479 阅读 · 0 评论 -
Valid Sudoku (九宫格)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2014-03-17 20:53:02 · 627 阅读 · 0 评论 -
Reverse Linked List II (链表逆置)
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the fol原创 2014-03-24 14:05:42 · 526 阅读 · 0 评论 -
Subsets(子集)
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,原创 2014-03-10 10:53:17 · 463 阅读 · 0 评论 -
Length of Last Word(最后一个单词长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2014-03-10 10:00:40 · 619 阅读 · 0 评论 -
LRU Cache (LRU策略缓存)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if原创 2014-03-28 15:13:21 · 644 阅读 · 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-03-18 15:27:53 · 388 阅读 · 0 评论 -
Remove Duplicates from Sorted List II (从排序链表中移出重复元素)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1原创 2014-03-19 16:01:29 · 651 阅读 · 0 评论 -
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-03-20 18:33:54 · 584 阅读 · 0 评论 -
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 tr原创 2014-03-22 19:33:57 · 684 阅读 · 0 评论 -
Recover Binary Search Tree (恢复一棵二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a原创 2014-03-22 10:06:11 · 739 阅读 · 0 评论 -
Jump Game (跳跃游戏)
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.Determine i原创 2013-10-24 12:12:30 · 680 阅读 · 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.两个已排序的单链表合并。 很简单 直接在原有立案表上修改就行了。/** * Definition原创 2013-10-21 14:52:33 · 445 阅读 · 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. Y原创 2013-10-21 20:03:06 · 925 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree(构建最优二叉排序树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题目很常规 根据一个递增的数组建立一个平衡的二叉排序树。 平衡二叉排序树的特征是根节点处于数组中间位置,子树的根节点分别是数组以中间位置分割的两部分的中间位置, 即递归性质。 所以可以利用递归来原创 2013-10-21 15:50:41 · 2320 阅读 · 0 评论 -
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.题意是从数原创 2013-10-21 15:11:43 · 1661 阅读 · 0 评论 -
Same Tree(相同树判断)
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./*原创 2013-10-20 15:51:41 · 519 阅读 · 0 评论 -
Reverse Integer(整数逆转)
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2013-10-20 17:00:23 · 551 阅读 · 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] ha原创 2013-10-21 10:42:57 · 497 阅读 · 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原创 2013-10-20 23:45:43 · 457 阅读 · 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./** * Definition for bina原创 2013-10-20 15:18:15 · 418 阅读 · 0 评论
分享