![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法题
文章平均质量分 63
Neo_dot
这个作者很懒,什么都没留下…
展开
-
【算法刷题】一个字符串中连续出现次数最多的子串
//字符串中连续出现次数最多的子串//利用后缀数组后缀数组是一种数据结构,对一个字符串生成相应的后缀数组后,然后再排序,排完序依次检测相邻的两个字符串的开头公共部分。//这样的时间复杂度为:总的时间复杂度是 O(N^2*logN),//后缀数组,每一行比上一行少一个,跳行就可出现少2个、3个……//第一趟:(一个长度的子串)第一行a与第二行第一个b比较是否相等,不等 (若相等则继续在第二行后取长度...原创 2018-06-03 20:18:13 · 3788 阅读 · 0 评论 -
【算法刷题】leetcode gas-station
There are N gas stations along a circular route, where the amount of gas at station iisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next s...原创 2018-07-11 22:06:55 · 187 阅读 · 0 评论 -
【算法刷题】 leetcode Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each...原创 2018-07-24 09:32:41 · 214 阅读 · 0 评论 -
【算法刷题】leetcode 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 ...原创 2018-07-07 15:31:06 · 231 阅读 · 0 评论 -
【算法刷题】leetcode word ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word must ...原创 2018-07-07 19:58:54 · 212 阅读 · 0 评论 -
【算法刷题】 单链表的快排和归并排序
快排: struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; void swap(ListNode *l, ListNode *r) { int temp = l->val; l->val = r-&...原创 2018-07-14 18:18:50 · 334 阅读 · 0 评论 -
【算法刷题】leetcode 分割回文串
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s ="aab",Return [ ["aa","b"], ["a"...原创 2018-07-15 16:09:04 · 484 阅读 · 0 评论 -
【算法刷题】leetcode surrounded region
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 O XX O X...原创 2018-07-22 17:07:25 · 177 阅读 · 0 评论 -
【算法刷题】leetcode longest-consecutive- sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. ...原创 2018-07-22 17:47:28 · 196 阅读 · 0 评论 -
【算法刷题】leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none)...原创 2018-08-14 15:39:58 · 189 阅读 · 0 评论 -
【算法刷题】leetcode Maximal-Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 思路参考 blog int maximalRectangle(vector<vector<char> > &am...原创 2018-08-13 12:16:41 · 191 阅读 · 0 评论 -
【算法刷题】leetcode restore-ip-addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does no...原创 2018-08-30 15:07:59 · 261 阅读 · 0 评论 -
【算法刷题】leetcode subsets ii
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate...原创 2018-08-30 15:10:22 · 208 阅读 · 0 评论 -
【算法刷题】leetcode Reverse digits of an integer.
Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have alrea...原创 2018-07-04 20:55:11 · 227 阅读 · 0 评论 -
【算法刷题】leetcode reorder-list
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,...原创 2018-07-04 19:26:43 · 160 阅读 · 0 评论 -
【算法刷题】 最长单调子序列
子序列在原序列中可以是不连续的动态规划求解://时间复杂度 n*n//令c[i]表示:在a[0->i]中,当以a[i]为单调递增子序列最后一个元素时,所得最长单调递增子序列的长度。状态转移方程:c[0]=1;c[i]=max{1,c[j]+1} a[j]<a[i]&&j<i;int LongestIncr(int X[], int n, int c[],...原创 2018-06-03 20:28:07 · 662 阅读 · 0 评论 -
【算法刷题】美团CodeM 2018资格赛
小美和小团最近沉迷可乐。可供TA们选择的可乐共有k种,比如可口可乐、零度可乐等等,每种可乐会带给小美和小团不同的快乐程度。TA们一共要买n瓶可乐,每种可乐可以买无限多瓶,小美会随机挑选其中的m瓶喝,剩下的n-m瓶小团喝。请问应该如何购买可乐,使得小美和小团得到的快乐程度的和的期望值最大?现在请求出购买可乐的方案。输入描述:第一行三个整数n,m,k分别表示要买的可乐数、小美喝的可乐数以及可供选择的可...原创 2018-06-03 20:39:34 · 1026 阅读 · 0 评论 -
【算法刷题】字符串中出现的相同且长度最长的字符串
以左和右查找子串是否出现位置相同,不同说明存在重复子串#include<iostream>#include<string>using namespace std;pair<int, string> longestsubstr(const string & str);int main(){ string s = "aaaaa"; pair&...转载 2018-06-03 22:29:47 · 462 阅读 · 0 评论 -
【算法刷题】8皇后问题
利用一个8个元素数组:queen[i]表示第i行的皇后所在列数;8个元素只能从0-7中选择,每个只能一次,这就保证了皇后不处在同一列或同一行,对于不处对角:任意两行的皇后i、j,不在对角满足|i-j|!=|queen[i]-queen[j]|由此,可解题:queen数组的全排列,对于全排列中每一个可能性,判断不在对角的条件//8皇后问题//全排列+逐个判断int g_number = 0;v...原创 2018-06-20 20:03:49 · 261 阅读 · 0 评论 -
【算法刷题】CodeM2018 初赛A轮 下棋
CodeM2018 初赛A轮 下棋 第二题[编程|1000分] 下棋 时间限制:C/C++ 1秒,其他语言 2秒 空间限制:C/C++ 262144K,其他语言 524288K 64bit IO Format: %lld 题目描述 有一个1*n的棋盘,上面有若干个棋子,一个格子上可能有多个棋子。 你每次操作是先选择一个棋子,然后选择以下两个操作中的一个: (1) 若该棋...原创 2018-06-15 14:59:11 · 447 阅读 · 0 评论 -
【算法刷题】字符串编辑距离
源串到目的串:只能删除、增加、替换动态规划:dp[i][j] s[0....i] ->t[0...j]的最小编辑距离字符串对其:状态方程:dp[i][j] = min{dp[i-1][j]+1,dp[i][j-1]+1,dp[i][j]=dp[i-1][j-1]+(s[i]==t[j]?0:1)}#include <iostream>#include <string...原创 2018-06-17 22:27:21 · 229 阅读 · 0 评论 -
【算法刷题】找出数组中出现次数大于N/K的所有元素
数组中出现次数超过一半(可实现时间复杂度o(n),空间复杂度o(1))的进阶版鸽巢原理:出现次数大于N/K的元素的个数至多为(M-1)个思路:每次从数组中删除k个不同的元素,直到不能再删了为止。那么最后数组中剩余的元素就是候选元素。因此 可建立一个k长度的map记录从数组中待消的元素,例如数组a={4,3,3,9,4,2,1,4,3,4,9,2} N=12,k=3 ,则每次应该删3个数,依次遍...原创 2018-06-10 16:57:18 · 2446 阅读 · 0 评论 -
【算法刷题】子序列个数
子序列的定义:对于一个序列a=a[1],a[2],......a[n],则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<.....<pm<=n。例如:4,14,2,3和14,1,2,3都为4,13,14,1,2,3的子序列。 对于给出序列a,有些子序列可能是相同的,这里只算做1个,要求输出a的不同子序列的数量。解题:...原创 2018-06-18 11:59:17 · 1073 阅读 · 0 评论 -
【算法刷题】字符串的组合(非排列)
递归的思想:n个字符里取出m个从头扫描字符串的第一个字符。针对第一个字符,有两种选择:要么取第一个,剩下n-1个取m-1个,要么第一不取,剩下n-1个中取m个,递归实现//n个字符里取出m个,递归实现//要么取第一个,剩下n-1个取m-1个,要么第一不取,剩下n-1个中取m个void Combination(char* str, int length, vector<char>&a...原创 2018-06-19 11:39:13 · 217 阅读 · 0 评论 -
【算法刷题】leetcode Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy....原创 2018-07-01 20:33:52 · 369 阅读 · 0 评论 -
【算法刷题】N皇后问题
主要思想就是递归回溯,一行一行的加,每次确保前边的都满足要求,因此,新加入的一行只需与遍历前边已加入的行,是否满足要求即可//N-Queens//一行一行放置使其不冲突 递归回溯 //检查最后一行的皇后,和前边行的是否冲突 bool check(int row, vector<int> Queen) { for (int i = 0; i < row; ++i)...原创 2018-07-11 10:21:45 · 351 阅读 · 0 评论 -
【算法刷题】leetcode word-break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens ="leetcode",dict =["leet", "code"]....原创 2018-07-04 16:06:56 · 177 阅读 · 0 评论