![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode OJ
文章平均质量分 66
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 · 672 阅读 · 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 · 451 阅读 · 0 评论 -
Reverse Words in a String (字符串单词逆序)
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes原创 2014-03-11 09:25:33 · 516 阅读 · 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 · 468 阅读 · 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 · 607 阅读 · 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 · 653 阅读 · 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 · 931 阅读 · 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 · 490 阅读 · 0 评论 -
Evaluate Reverse Polish Notation (求逆波兰表达式值)
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+",原创 2014-03-12 09:57:01 · 477 阅读 · 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 · 538 阅读 · 0 评论 -
Insertion Sort List(链表插入排序)
Sort a linked list using insertion sort.这里给单链表进行插入排序,个人想到两种方法 一种是交换节点的指针,一种是交换节点的值,前者要判断头节点,感觉比较麻烦,就用了交换值的方法, 通常的插入排序是在插入点的位置依次与前面的元素相比较,如果比前面的小,就把当前的位置的值变成前一个位置的值,最后得到的是最终插入位置, 这里单链表没办法往前搜索,那么就从头原创 2014-03-04 15:38:45 · 620 阅读 · 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 · 687 阅读 · 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 · 661 阅读 · 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 · 784 阅读 · 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 · 531 阅读 · 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 · 476 阅读 · 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 · 458 阅读 · 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 · 550 阅读 · 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 · 1202 阅读 · 1 评论 -
Reorder List (链表重排)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4原创 2014-03-13 15:13:02 · 472 阅读 · 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 &s原创 2014-03-07 10:25:19 · 1917 阅读 · 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 · 513 阅读 · 0 评论 -
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 triangle[ [2], [3,4], [6,原创 2014-03-26 14:02:48 · 501 阅读 · 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 · 396 阅读 · 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 · 544 阅读 · 0 评论 -
Two 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, where原创 2014-07-28 15:39:49 · 368 阅读 · 0 评论 -
Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return it原创 2014-07-29 16:32:40 · 330 阅读 · 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-07-30 14:45:28 · 399 阅读 · 0 评论 -
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 input case原创 2014-08-06 16:43:53 · 339 阅读 · 0 评论 -
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.Have原创 2014-08-08 16:03:09 · 360 阅读 · 0 评论 -
Copy List with Random Pointer(拷贝带有random指针的链表)
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. 这道题自己一直没想到比较好的方法原创 2014-03-25 16:16:05 · 444 阅读 · 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 · 464 阅读 · 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 · 415 阅读 · 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 · 557 阅读 · 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 · 551 阅读 · 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 · 331 阅读 · 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 · 594 阅读 · 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 · 518 阅读 · 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 · 623 阅读 · 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 · 652 阅读 · 0 评论