上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 34 下一页
摘要: O(N)划分法,注意这个方法会改变原数据(函数参数是引用的情况下)!当然也可以再定义一个新容器对其划分 要求前k小的数,只要执行快排划分,每次划分都会把数据分成大小两拨。直到某一次划分的中心点正好在k处,则左侧0~k-1的数字正好就是所求。 class Solution { public: vect 阅读全文
posted @ 2020-02-08 14:51 NeoZy 阅读(127) 评论(0) 推荐(0)
摘要: #include<vector> #include<iostream> #include<time.h> using namespace std; int partition_1(vector<int> &nums,int le,int ri){ //来回填坑法 if(le>=ri){ return 阅读全文
posted @ 2020-02-08 02:51 NeoZy 阅读(504) 评论(0) 推荐(0)
摘要: 我惯用的dfs模板直接拿来套 class Solution { public: vector<string> Permutation(string str) { if(str.empty()){return{};} int n=str.size(); vector<string> res; vect 阅读全文
posted @ 2020-02-07 22:50 NeoZy 阅读(232) 评论(0) 推荐(0)
摘要: 中序递归,一个pre节点记录前一个节点 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } 阅读全文
posted @ 2020-02-07 15:36 NeoZy 阅读(168) 评论(0) 推荐(0)
摘要: 时间O(N),空间O(N) /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { 阅读全文
posted @ 2020-02-07 01:42 NeoZy 阅读(163) 评论(0) 推荐(0)
摘要: 一开始写的垃圾代码,push和pop都是O(N) class Solution { public: vector<int> vec; int min_val=INT_MAX,min_cnt=0; void push(int value) { vec.push_back(value); if(min_ 阅读全文
posted @ 2020-02-06 16:32 NeoZy 阅读(196) 评论(0) 推荐(0)
摘要: dfs: class Solution { public: string decodeString(string s) { int i=0; return dfs(s,i); } string dfs(string& s,int& i){//计算一对括号内的解码后字符串 string tmp=""; 阅读全文
posted @ 2020-02-02 22:28 NeoZy 阅读(188) 评论(0) 推荐(0)
摘要: 78子集 dfs dfs1: 和全排列的区别就是对于当前考察的索引i,全排列如果不取i,之后还要取i,所以需要一个visited数组用来记录。对于子集问题如果不取i,之后也不必再取i。 单纯递归回溯 class Solution { public: vector<vector<int>> subse 阅读全文
posted @ 2020-02-02 16:13 NeoZy 阅读(190) 评论(0) 推荐(0)
摘要: Solution1 动态规划 方法1:dp[i][j]表示i到j是否是回文 class Solution { public: string longestPalindrome(string s) { int n=s.size(); int max_len=1,le=0,ri=0; if(n==0){ 阅读全文
posted @ 2020-02-02 14:17 NeoZy 阅读(90) 评论(0) 推荐(0)
摘要: 第一题 Q: 小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为m|S,例如字符串ABCABCABC将会被压缩为[3|ABC],现在小Q的同学收到了小Q发送过来的字符串,你能帮助他进 阅读全文
posted @ 2020-01-28 22:47 NeoZy 阅读(661) 评论(0) 推荐(0)
上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 34 下一页