算法题
被打的小怪兽
这个作者很懒,什么都没留下…
展开
-
Leetcode 14
这道题让我们求一系列字符串的共同前缀,可以无脑查找,我们定义两个变量i和j,采用纵向的逐列遍历,在遍历时,如果某一行没有了或者取出的字符与当前比较的字符不同,就终止循环。C++ string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return "";...原创 2019-07-19 09:00:12 · 75 阅读 · 0 评论 -
Leetcode7:Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an ...原创 2019-07-19 09:10:16 · 148 阅读 · 0 评论 -
Leetcode21: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.Example:Input: 1->2->4, 1->3->4Output: 1->...原创 2019-07-24 09:12:26 · 76 阅读 · 0 评论 -
Leetcode 38:Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1112112111112211 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21...原创 2019-07-29 09:02:27 · 98 阅读 · 0 评论 -
Leetcode 39:Combination Sum
Given 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.The same repeate...原创 2019-07-29 09:15:00 · 85 阅读 · 0 评论 -
Leetcode 22. 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:[“((()))”,“(()())”,“(())()”,“()(())”,“()()()”...原创 2019-07-25 09:12:40 · 63 阅读 · 0 评论 -
Leetcode 40:Combination Sum II
Given 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 candidates may ...原创 2019-07-30 09:07:07 · 99 阅读 · 0 评论 -
Leetcde 43:Multiply Strings
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.Example 1:Input: num1 = “2”, num2 = “3”Output: “6”Example 2:...原创 2019-07-30 09:08:34 · 77 阅读 · 0 评论 -
Leetcode 15: 3 sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.For example, given array S = {-1 0 1 2...原创 2019-07-21 09:27:53 · 72 阅读 · 0 评论 -
Leercode 16: 3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...转载 2019-07-21 09:55:30 · 78 阅读 · 0 评论 -
Leetcode18: 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of tar...原创 2019-07-22 09:07:52 · 45 阅读 · 0 评论 -
Leetcode 46: Permutations
Given a collection of distinct integers, return all possiblepermutations.Example:Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1],[3,1,2], [3,2,1] ]C++vector<vector&l...原创 2019-07-31 10:08:28 · 88 阅读 · 0 评论 -
Leetcode 47: Permutations II
Given a collection of numbers that might contain duplicates, returnall possible unique permutations.Example:Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ]C++vector<vector<int&...原创 2019-07-31 10:09:53 · 100 阅读 · 0 评论 -
Leetcode23 : Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, you sho...原创 2019-07-26 09:17:34 · 68 阅读 · 0 评论 -
Leetcode 19: Remove Nth Node From End of List
Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.C++ListNode* removeNthFromEnd(ListNode* hea...原创 2019-07-23 09:10:18 · 84 阅读 · 0 评论 -
Leetcod20 : Valid Parentheses
Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:1: Open brackets must be closed by the same type of ...原创 2019-07-23 09:22:26 · 76 阅读 · 0 评论 -
Leetcode 48:Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix dire...原创 2019-08-01 09:11:09 · 79 阅读 · 0 评论 -
Leetcode 26:Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifyin...转载 2019-07-27 09:51:58 · 113 阅读 · 0 评论 -
Leetcode27:Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array...转载 2019-07-27 10:10:38 · 63 阅读 · 0 评论 -
Leetcode 28: Implement strStr()
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = “hello”, needle = “ll”Output: 2Example 2:In...原创 2019-07-27 10:25:44 · 89 阅读 · 0 评论 -
Leetcode 29:Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.Return the quotient after dividing dividend by divisor.The integer division shoul...转载 2019-07-27 10:58:27 · 84 阅读 · 0 评论 -
Leetcode 31: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 possible or...转载 2019-07-28 09:31:52 · 89 阅读 · 0 评论 -
Leetcode 53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation:...原创 2019-08-08 09:18:17 · 85 阅读 · 0 评论 -
Leetcode 54:Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example...原创 2019-08-08 09:19:33 · 105 阅读 · 0 评论 -
Leetcode33: 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 found in...转载 2019-07-28 10:18:27 · 74 阅读 · 0 评论 -
Leetcode 34:Find First and Last Position of Element in Sorted Array
Given 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(log n).If the t...原创 2019-07-28 10:29:01 · 94 阅读 · 0 评论 -
各种排序算法总结
1:插入排序C++//insert sortvoid insertion_sort(int arr[],int len){ for(int i = 1;i < len;++i) { int key = arr[i]; int j = i - 1; while(arr[j] > key&&j >...原创 2019-08-01 20:21:24 · 86 阅读 · 0 评论 -
Leetcode35: 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.Ex...转载 2019-07-28 17:59:08 · 90 阅读 · 0 评论 -
Leetcode 36:Valid Sudoku
Determine 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 column must contain...转载 2019-07-28 18:32:03 · 70 阅读 · 0 评论 -
Leetcode 50:Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: ...原创 2019-08-06 09:18:25 · 91 阅读 · 0 评论 -
Leetcode 49:Group Anagrams
Given an array of strings, group anagrams together.Example:Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],Output:[[“ate”,“eat”,“tea”],[“nat”,“tan”],[“bat”]]C++vector<vector<string&g...原创 2019-08-02 09:25:57 · 103 阅读 · 0 评论 -
Leetcode 74: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 integer of each row i...原创 2019-08-14 09:11:26 · 86 阅读 · 0 评论 -
Leetcode 91: Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1‘B’ -> 2…‘Z’ -> 26Given a non-empty string containing only digits, determine the tot...原创 2019-08-19 09:13:02 · 98 阅读 · 0 评论 -
Leetcode 92: Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5->...原创 2019-08-19 09:41:45 · 66 阅读 · 0 评论 -
Leetcode 75:Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the int...原创 2019-08-14 09:48:16 · 60 阅读 · 0 评论 -
Leetcode 55: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 if you ...原创 2019-08-10 09:36:27 · 74 阅读 · 0 评论 -
Leetcode 56:Merge Intervals
Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, m...原创 2019-08-10 09:37:30 · 92 阅读 · 0 评论 -
Leetcode 57: 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 defined...原创 2019-08-10 09:59:37 · 88 阅读 · 0 评论 -
Leetcode 58:Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.Example:Input: 3Output:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]C++vector<vector<int&...原创 2019-08-10 10:17:29 · 76 阅读 · 0 评论 -
Leetcode 60: Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence for n = 3:“123”“132”“213”“231”“312”“321”G...原创 2019-08-10 11:01:42 · 97 阅读 · 0 评论