LeetCode算法分析
文章平均质量分 58
LeetCode题目分析及解答
「已注销」
精神病人思路广,2B青年欢乐多!
展开
-
LeetCode 01 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, whe原创 2014-10-30 23:50:09 · 1828 阅读 · 0 评论 -
LeetCode 02 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2014-11-03 22:09:39 · 1614 阅读 · 0 评论 -
LeetCode 03: Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For原创 2015-06-01 22:58:24 · 1863 阅读 · 0 评论 -
LeetCode 4: Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 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))代码如下:class Solutio原创 2015-06-01 23:00:44 · 3210 阅读 · 0 评论 -
LeetCode 5: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.分析:采用中心扩展法,每原创 2015-05-12 00:38:14 · 3144 阅读 · 0 评论 -
LeetCode 6: ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I G原创 2016-10-07 03:53:58 · 1468 阅读 · 0 评论 -
LeetCode 7: Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321代码如下:class Solution {public: int reverse(int x) { if (x == -x) { return 0; } if (x <原创 2015-06-01 23:07:12 · 2222 阅读 · 0 评论 -
LeetCode 8: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原创 2015-06-01 22:55:54 · 2177 阅读 · 0 评论 -
LeetCode 9: Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.本题是判断一个数是否是回文数。代码如下:bool isPalindrome(int x) { int max = x; int min = 0; while(max >0){ min...原创 2015-06-01 23:11:53 · 3468 阅读 · 4 评论 -
LeetCode 10: 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 inpu原创 2015-06-01 23:15:17 · 2628 阅读 · 0 评论 -
LeetCode 14: Longest Common Prefix
Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.求最长公共前缀。代码如下:class Solution {public: string longestCommonPrefix(vect原创 2015-07-30 19:04:07 · 1802 阅读 · 0 评论 -
LeetCode 15: 3 Sum
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) m原创 2015-06-09 19:45:38 · 3280 阅读 · 0 评论 -
LeetCode 18: 4Sum
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:Elements原创 2015-06-02 22:00:39 · 3401 阅读 · 0 评论 -
LeetCode 19: 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 l原创 2015-06-02 22:03:47 · 1561 阅读 · 0 评论 -
LeetCode 20: 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 valid b原创 2015-06-02 22:05:50 · 1593 阅读 · 0 评论 -
LeetCode 21:Merge Two Sorted Lists
LeetCode 21: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.题目分析:对两个原创 2015-05-27 23:08:48 · 4527 阅读 · 0 评论 -
LeetCode 23: Merge K Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个。此题有如下几种解法:1、最简单的方法莫过于每次原创 2015-05-31 02:04:42 · 4139 阅读 · 0 评论 -
LeetCode 24: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原创 2015-06-13 23:12:02 · 1523 阅读 · 0 评论 -
LeetCode 25: 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.原创 2015-06-08 20:00:42 · 4715 阅读 · 0 评论 -
LeetCode 26: Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-06-15 19:32:10 · 1509 阅读 · 0 评论 -
LeetCode 27: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.题原创 2015-06-03 23:05:55 · 2567 阅读 · 0 评论 -
LeetCode 28:Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.实现函数strStr。代码如下:int strStr(char* haystack, char* needle) { size_原创 2015-06-03 23:08:12 · 692 阅读 · 0 评论 -
LeetCode 35:Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2015-06-15 19:57:29 · 574 阅读 · 0 评论 -
LeetCode 44: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 cov原创 2015-06-03 23:13:44 · 1923 阅读 · 0 评论 -
LeetCode 56:Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].分析:题目要求对区间进行合并,首先必须对区间按照左边元素的大小进行排序,然后对排原创 2015-06-02 21:17:41 · 2845 阅读 · 0 评论 -
LeetCode 80:Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five e原创 2015-06-15 19:49:29 · 605 阅读 · 0 评论 -
LeetCode 82: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->原创 2015-06-15 19:19:06 · 564 阅读 · 0 评论 -
LeetCode 83: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.简单链表操作,代码如下:/** *原创 2015-06-06 01:26:43 · 749 阅读 · 0 评论 -
LeetCode 86 Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2015-06-06 01:27:06 · 2377 阅读 · 0 评论 -
LeetCode 94: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]. 题目要求对二叉树进行非递归的中序遍历,所谓中序遍历即,先访问左子树、再...原创 2015-06-01 19:06:05 · 2132 阅读 · 2 评论 -
LeetCode 100: 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.分析:题目要求判原创 2015-05-14 12:46:24 · 762 阅读 · 0 评论 -
LeetCode 101: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原创 2015-05-31 00:02:15 · 624 阅读 · 0 评论 -
LeetCode 103: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原创 2015-05-31 00:34:47 · 2568 阅读 · 0 评论 -
LeetCode 104: 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.分析;此题是求二叉树的深度,(无所谓最大深度),用递归的方法很容原创 2015-05-14 12:43:53 · 855 阅读 · 0 评论 -
LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105:Given preorder and inorder traversal of a tree, construct the binary tree.给定一个二叉树的前序和中序遍历,重建这棵二叉树。原创 2015-05-20 23:09:41 · 1540 阅读 · 0 评论 -
LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.给定一个二叉树的后序和中序遍历,重建这棵二叉树。此题和LeetCode105 根据前序和中序重建二叉树类似。所谓后序遍历,即先访问根的左、右子树,然后再访问根节点。这样根节点在二叉树后序遍历的最后一个个元素。所谓中序遍原创 2015-06-01 19:09:42 · 877 阅读 · 0 评论 -
LeetCode 107: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},原创 2015-05-31 00:11:23 · 4236 阅读 · 0 评论 -
LeetCode 112: Path Sum
Question: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 b原创 2015-04-30 19:51:29 · 528 阅读 · 0 评论 -
LeetCode 118: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 {publi原创 2015-06-06 01:31:49 · 1495 阅读 · 0 评论 -
LeetCode 119: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].题目要求计算杨辉三角某一行的元素,这个也是二项式系数的计算问题。class Solution {public: vector getRow(int row原创 2015-06-06 01:38:13 · 2609 阅读 · 0 评论