![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode算法练习(Medium)
我们要爱学习
敲敲代码,各种玩耍
展开
-
Longest Substring Without Repeating Characters题目及解法
问题描述:Given a string, find the length of the longest substring without repeating characters.示例:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b",原创 2017-02-21 13:05:08 · 354 阅读 · 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.示例:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.原创 2017-02-23 09:43:39 · 341 阅读 · 0 评论 -
Add Two Numbers问题及解法
问题描述:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers a原创 2017-02-16 09:17:32 · 1115 阅读 · 0 评论 -
Container With Most Water问题及解法
问题描述:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i,原创 2017-03-03 18:20:43 · 247 阅读 · 0 评论 -
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: The原创 2017-03-08 14:14:37 · 445 阅读 · 0 评论 -
Remove Nth Node From End of List问题及解法
问题描述:Given a linked list, remove the nth node from the end of list and return its head.示例: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l原创 2017-03-08 14:46:16 · 361 阅读 · 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.问题分析:本题目目的是将一个整数转换为罗马数字的表现形式。我们可以将能用的到罗马数字存到数组里,然后再调用。详见代码:class Soluti原创 2017-03-05 08:57:06 · 235 阅读 · 0 评论 -
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 NA原创 2017-02-27 10:27:36 · 272 阅读 · 0 评论 -
Generate Parentheses问题及解法
问题描述:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.示例:For example, given n = 3, a solution set is:[ "((()))", "(()())", "((原创 2017-03-09 13:01:00 · 270 阅读 · 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 i原创 2017-02-27 16:49:18 · 270 阅读 · 0 评论 -
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: The solution set must not原创 2017-03-06 22:59:05 · 512 阅读 · 0 评论 -
3Sum Closest问题及解法
问题描述:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would hav原创 2017-03-07 14:18:36 · 289 阅读 · 0 评论 -
Letter Combinations of a Phone Number问题及解法
问题描述:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.示例:原创 2017-03-07 16:43:17 · 251 阅读 · 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 spa原创 2017-05-16 09:20:34 · 375 阅读 · 0 评论 -
Search for a Range问题及解法
问题描述:Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If原创 2017-06-06 06:54:14 · 696 阅读 · 0 评论 -
Divide Two Integers问题及解法
问题描述:Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.问题分析:首先把容易溢出的案列举出来:除数为零或者被除数等于INT_MIN,除数等于-1。接下来,我们可以根据十进制数转换为二进制数的方法,逐层求转载 2017-06-02 15:45:14 · 269 阅读 · 0 评论 -
Next Permutation问题及解法
问题描述:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest p原创 2017-06-03 17:01:22 · 406 阅读 · 0 评论 -
Search in Rotated Sorted Array问题及解法
问题描述:Suppose an array sorted in ascending order 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原创 2017-06-04 10:35:40 · 279 阅读 · 0 评论 -
Permutations II问题及解法
问题描述:Given a collection of numbers that might contain duplicates, return all possible unique permutations.示例:[1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1原创 2017-06-30 17:14:45 · 264 阅读 · 0 评论 -
Rotate Image问题及解法
问题描述:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).问题分析:矩阵顺时针旋转90度可以分为两步:1.对矩阵转置2.对矩阵按列reverse(翻转)过程详见代码:class Solution {publ原创 2017-07-01 09:37:32 · 817 阅读 · 0 评论 -
Group Anagrams问题及解法
问题描述:Given an array of strings, group anagrams together.示例:Given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]问题分析:采用map数据结原创 2017-07-02 13:10:40 · 304 阅读 · 0 评论 -
Permutations问题及解法
问题描述:Given a collection of distinct numbers, return all possible permutations.示例:[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]原创 2017-06-23 07:40:46 · 404 阅读 · 0 评论 -
Combination Sum问题及解法
问题描述:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be原创 2017-06-12 14:54:23 · 460 阅读 · 0 评论 -
Combination Sum II问题及解法
问题描述:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the原创 2017-06-15 07:18:27 · 226 阅读 · 0 评论 -
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 respectivel原创 2017-07-29 19:57:48 · 390 阅读 · 0 评论 -
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原创 2017-07-29 20:35:49 · 301 阅读 · 0 评论 -
Simplify Path问题及解法
问题描述:Given an absolute path for a file (Unix-style), simplify it.示例:path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"问题分析:将/ 和 / 之间的字符串分割出来,"." 可以直接忽略,每出现一个".."字串,根据原创 2017-07-30 15:32:43 · 347 阅读 · 0 评论 -
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.Follow up:Did you use extra space?A straight forward solution using O(mn) space is proba原创 2017-07-31 08:18:39 · 239 阅读 · 0 评论 -
Search a 2D Matrix问题及解法
问题描述:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first inte原创 2017-07-31 09:10:31 · 168 阅读 · 0 评论 -
Pow(x, n)问题及解法
问题描述:Implement pow(x, n).问题分析:对一个数求幂运算,我们可以把他的幂分解开来,利用二分法,降低时间复杂度。过程详见代码:class Solution {public: double myPow(double x, int n) { return myPow2(x, n); } double myPow2(double原创 2017-07-22 08:14:33 · 1741 阅读 · 0 评论 -
Multiply Strings问题及解法
问题描述:Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 contains only原创 2017-07-22 19:27:49 · 354 阅读 · 0 评论 -
Restore IP Addresses问题及解法
问题描述:Given a string containing only digits, restore it by returning all possible valid IP address combinations.示例:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Orde原创 2017-08-07 16:14:59 · 245 阅读 · 0 评论 -
Binary Tree Inorder Traversal问题及解法
问题描述:Given a binary tree, return the inorder traversal of its nodes' values.示例:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].问题分析:利用二叉树的先序遍历,先遍原创 2017-08-07 16:25:11 · 225 阅读 · 0 评论 -
Unique Binary Search Trees问题及解法
问题描述:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?示例:Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1原创 2017-08-07 18:43:57 · 178 阅读 · 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:The left subtree of a node contains only nodes with keys less than the no原创 2017-08-07 20:13:54 · 278 阅读 · 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 th原创 2017-08-01 18:28:53 · 320 阅读 · 0 评论 -
Unique Binary Search Trees II问题及解法
问题描述:Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.示例:Given n = 3, your program should return all 5 unique BST's shown below.原创 2017-08-08 14:51:33 · 215 阅读 · 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).示例:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 2原创 2017-08-08 15:21:46 · 196 阅读 · 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).示例:Given binar原创 2017-08-08 15:33:51 · 196 阅读 · 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.问题分析:已知树的先序和中序遍历的结果,我们可以依此来分别构建左子树、右子树和树根,递归原创 2017-08-08 16:49:37 · 289 阅读 · 0 评论