
算法设计
Yjl_Richard
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
力扣-060 第k个排列
题目描述给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:“123”“132”“213”“231”“312”“321”给定 n 和 k,返回第 k 个排列。说明:给定 n 的范围是 [1, 9]。给定 k 的范围是[1, n!]。样例示例 1:输入: n = 3, k = 3输出: “213”示例 2:输入: n = 4, k = 9输出: “2314”分析题目理解起来很简单原创 2020-09-09 16:08:34 · 185 阅读 · 0 评论 -
力扣-059 螺旋矩阵 II
题目描述给定一个正整数 n,生成一个包含 1 到 n2n^2n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。样例示例:输入: 3输出:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]分析这是经典的数组题,题目看起来简单,但做起来的时候,很容易因为边界的问题搞到自己十分混乱。这里我推荐一下我的做法,首先是按圈来排列数字,然后把一圈拆成四个步骤,现实从左到右,然后再从上到下,再从右到左,最后从下到上这样按顺序的四步,完成一圈。按这样的顺序走,出错的概率就原创 2020-09-08 11:11:13 · 210 阅读 · 0 评论 -
力扣-058 最后一个单词的长度
题目描述给定一个仅包含大小写字母和空格 ’ ’ 的字符串 s,返回其最后一个单词的长度。如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。如果不存在最后一个单词,请返回 0 。说明:一个单词是指仅由字母组成、不包含任何空格字符的 最大子字符串。样例示例:输入: “Hello World”输出: 5分析这是一道简单的字符串分析题,翻译题意就是找到最后一个不含空格的字符串子串长度,建议从后往前遍历,先将后置空格给跳过以后,直到下一个空格的位置就是最后一个不含空格的字符串子串长度。原创 2020-09-08 10:05:40 · 193 阅读 · 0 评论 -
力扣-057 插入区间
题目描述给出一个无重叠的 ,按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。样例示例 1:输入:intervals = [[1,3],[6,9]], newInterval = [2,5]输出:[[1,5],[6,9]]示例 2:输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]输出:[[1,2],[3,10],[12,原创 2020-09-08 09:43:08 · 180 阅读 · 0 评论 -
力扣-056 合并区间
题目描述给出一个区间的集合,请合并所有重叠的区间。样例示例 1:输入: intervals = [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].示例 2:输入: intervals = [[1,4],[4,5]]输出: [[1,5]]解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。提示intervals[i][0] <= inter原创 2020-09-07 16:55:23 · 185 阅读 · 0 评论 -
LeetCode-046 Permutations
DescriptionGiven a collection of distinct integers, return all possible permutations.ExampleInput: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]...原创 2018-06-08 18:15:54 · 2276 阅读 · 0 评论 -
LeetCode-039 Combination Sum
DescriptionGiven a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Th...原创 2018-06-06 16:25:28 · 213 阅读 · 0 评论 -
LeetCode-040 Combination Sum II
DescriptionGiven a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in c...原创 2018-06-06 16:37:36 · 205 阅读 · 0 评论 -
LeetCode-041 First Missing Positive
DescriptionGiven an unsorted integer array, find the smallest missing positive integer.ExampleExample 1:Input: [1,2,0] Output: 3 Example 2:Input: [3,4,-1,1] Output: 2 Example 3:Input...原创 2018-06-06 17:14:23 · 303 阅读 · 0 评论 -
LeetCode-038 Count and Say
DescriptionThe count-and-say sequence is the sequence of integers with the first five terms as following:111211211111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 2...原创 2018-06-06 16:15:32 · 228 阅读 · 0 评论 -
LeetCode-037 Sudoku Solver
DescriptionWrite a program to solve a Sudoku puzzle by filling the empty cells.A sudoku solution must satisfy all of the following rules:Each of the digits 1-9 must occur exactly once in each ro...原创 2018-06-05 20:57:17 · 329 阅读 · 0 评论 -
LeetCode-036 Valid Sudoku
DescriptionDetermine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition. Each colu...原创 2018-06-05 20:48:13 · 230 阅读 · 0 评论 -
LeetCode-035 Search Insert Position
DescriptionGiven 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...原创 2018-06-05 20:31:12 · 177 阅读 · 0 评论 -
LeetCode-034 Find First and Last Position of Element in Sorted Array
DescriptionGiven an array of integers nums 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(lo...原创 2018-06-05 20:28:35 · 205 阅读 · 0 评论 -
LeetCode-033 Search in Rotated Sorted Array
DescriptionSuppose 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 sear...原创 2018-06-05 20:20:46 · 227 阅读 · 0 评论 -
LeetCode-032 Longest Valid Parentheses
DescriptionGiven a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.ExampleExample 1:Input: “(()” Output: 2 Explana...原创 2018-06-05 20:12:37 · 152 阅读 · 0 评论 -
LeetCode-042 Trapping Rain Water
DescriptionGiven 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. The above elevation map is repres...原创 2018-06-07 01:05:42 · 215 阅读 · 0 评论 -
LeetCode-043 Multiply Strings
DescriptionGiven two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.ExampleExample 1:Input: num1 = “2”, num2 = “3...原创 2018-06-07 01:10:01 · 233 阅读 · 0 评论 -
LeetCode-044 Wildcard Matching
DescriptionGiven an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?’ and ‘*’.‘?’ Matches any single character. ‘*’ Matches any sequence of characters (...原创 2018-06-08 17:52:00 · 230 阅读 · 0 评论 -
LeetCode-055 Jump Game
DescriptionGiven 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.Deter...原创 2019-06-03 16:43:19 · 202 阅读 · 0 评论 -
LeetCode-054 Spiral Matrix
DescriptionGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.ExampleExample 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,...原创 2019-06-03 16:31:13 · 226 阅读 · 0 评论 -
LeetCode-053 Maximum Subarray
DescriptionGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.ExampleInput: [-2,1,-3,4,-1,2,1,-5,4],Output: 6E...原创 2019-06-03 16:18:25 · 195 阅读 · 0 评论 -
LeetCode-052 N-Queens II
DescriptionThe 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 the number of distinct solutions to the n...原创 2019-06-03 16:07:34 · 202 阅读 · 0 评论 -
LeetCode-051 N-Queens
DescriptionThe 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 pu...原创 2019-04-22 21:03:43 · 198 阅读 · 0 评论 -
LeetCode-050 Pow(x, n)
DescriptionImplement pow(x, n), which calculates x raised to the power n (xn).ExampleExample 1:Input: 2.00000, 10 Output: 1024.00000 Example 2:Input: 2.10000, 3 Output: 9.26100 Example 3...原创 2018-06-08 20:41:54 · 283 阅读 · 0 评论 -
LeetCode-049 Group Anagrams
DescriptionGiven an array of strings, group anagrams together.ExampleInput: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Output: [ [“ate”,”eat”,”tea”], [“nat”,”tan”], [“bat”] ]N...原创 2018-06-08 20:10:17 · 283 阅读 · 0 评论 -
LeetCode-048 Rotate Image
DescriptionYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).NoteYou have to rotate the image in-place, which means you have to modify the input...原创 2018-06-08 20:04:51 · 251 阅读 · 0 评论 -
LeetCode-047 Permutations II
DescriptionGiven a collection of numbers that might contain duplicates, return all possible unique permutations.ExampleInput: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ]Analyse...原创 2018-06-08 18:21:00 · 287 阅读 · 0 评论 -
LeetCode-045 Jump Game II
DescriptionGiven 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.Yo...原创 2018-06-08 18:10:11 · 222 阅读 · 0 评论 -
LeetCode-031 Next Permutation
DescriptionImplement 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 lowe...原创 2018-05-21 11:10:14 · 198 阅读 · 0 评论 -
LeetCode-030 Substring with Concatenation of All Words
DescriptionYou are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactl...原创 2018-05-19 14:51:56 · 202 阅读 · 0 评论 -
算法概论(注释版)课后习题8.15
8.15 Show that the following problem is NP-complete.原创 2018-01-02 21:32:52 · 1012 阅读 · 0 评论 -
LeetCode-023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity原创 2017-12-10 15:16:20 · 184 阅读 · 0 评论 -
LeetCode-022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.原创 2017-12-10 15:04:25 · 239 阅读 · 0 评论 -
LeetCode-021 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.原创 2017-12-10 14:54:02 · 194 阅读 · 0 评论 -
LeetCode-020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.原创 2017-12-08 21:35:53 · 320 阅读 · 0 评论 -
LeetCode-019 Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.原创 2017-12-08 21:24:36 · 212 阅读 · 0 评论 -
LeetCode-018 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.原创 2017-12-08 21:14:14 · 217 阅读 · 0 评论 -
LeetCode-017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.原创 2017-12-08 21:07:19 · 210 阅读 · 0 评论 -
LeetCode-016 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.原创 2017-12-08 21:04:13 · 210 阅读 · 0 评论