随笔分类 - C++
C++终于熟了
摘要:实现二叉树中序非递归遍历。 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left...
阅读全文
摘要:两个字符串的字符个数完全相同,这两个字符串是Anagrams。因此Anagrams至少指俩字符串。找出字符集合中的Anagrams组。 vector<string> anagrams(vector<string> &strs) { vector<string> ret; map<string, vector<const string *...
阅读全文
摘要:两个十进制数,反向存储在单向链表里,每位数占一个节点。求和的链表。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), nex...
阅读全文
摘要:求数组中和为target的4个数,结果的4个数是非降序,结果集合不能有重复。跟3Sum一样的解法。 vector<vector<int> > fourSum(vector<int> &num, int target) { vector<vector<int> > ret; if (num.size() == 0) ret...
阅读全文
摘要:找到与target最接近的三个数的和。 int three_sum_closest(vector<int> &num, int target) { int result = 0; int dist = INT_MAX; sort(num.begin(), num.end()); ...
阅读全文
摘要:求和为指定值的三个数,麻烦的一点是,结果的集合不能有重复的。主要思路有两个,一个是在求值过程中过滤去重,还有一个是用hash。当然不能偷懒直接用set<vector<int> >,这样会直接超时。 // Dedup directly, // LeetCode Judge Large, 272 milli secs. vector<vector<int> > ...
阅读全文
摘要:逆序数的题最经典的就是求逆序对数,可以直接冒泡然后记录交换的次数,时间复杂度O(n^2)。也可以用修改版的归并排序来做,时间复杂度会降到O(nlogn)。然后,有一种题是有一队人,每个人都知道自己的身高和前面比自己高的人数,队伍解散后怎么才能恢复队伍?这个题给的信息实际上就是逆序对数,根据逆序对数恢复元素位置,我想到的办法是用类似于插入排序的东西来做。下面有实现代码,现根据身高求出每个人前...
阅读全文
摘要:题意是计算两个二进制的字符串的和,返回值依然是个二进制字符串。直接写自然不好搞,于是把整个步骤拆解为两个二进制字符与进位相加。实际上任意类似的加法都可以这样拆解。 // add a and b and carry, return a + b + carry. // carry will be updated when add char. char add_ch...
阅读全文
摘要:反转链表是一个常见的面试题,现在出现了N多种的变形,各种增加难度。今天看到了一个从m反转到n的,于是手写代码试了试。发现要想把所有情况覆盖全达到bug free还是挺难的。于是,根据各种反转链表题总结了一些比较简练的代码。比较:接口限定的反转链表。 // From curr reverse to end, return reversed linked list head....
阅读全文
摘要:天天准备面试题,有复杂的算法有简单的数据结构。但是总是有些常考的经典排序算法:插入排序/堆排序/快排/归并排序。日常工作中谁也不会2B到自己写这些排序,这些纯应付面试用,你懂的! // insert sort template<typename T> void insert_sort(T *begin, T *end) { if (be...
阅读全文
摘要:题目:Bridging signals (POJ 1631) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1631 #include <iostream> #include <vector> #include <limits.h> using namespace std; // O(...
阅读全文
摘要:题目:Longest Ordered Subsequence (POJ 2533) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=2533 #include <iostream> #include <vector> using namespace std; int LIS(const vec...
阅读全文
摘要:题目:Testing the CATCHER (POJ 1887) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1887 #include <iostream> #include <vector> using namespace std; int main(int argc, char *...
阅读全文
摘要:题目:AGTC (POJ 3356) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=3356 #include <iostream> #include <string> #include <algorithm> using namespace std; int matrix[1001]...
阅读全文
摘要:题目:Zipper (POJ 2192) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=2192 #include <iostream> #include <string> #include <memory.h> using namespace std; bool dp[201][20...
阅读全文
摘要:题目:Human Gene Functions (POJ 1080) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1080 #include <iostream> #include <memory.h> #include <string> using namespace std; i...
阅读全文
摘要:链接:https://www.interviewstreet.com/challenges/dashboard/#problem/4fe12e4cbb829 分析:可以把rating看成一个有升有降的数组,然后数组中存在三种区域(连续升、连续降、水平),区域的最小长度为1。举个例子,rating[] = [1, 2, 3, 3, 4, 2],则direction[] = [↑, ↑, 〓, ↑,...
阅读全文
摘要:题目:Palindrome (POJ 1159) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1159 #include <iostream> #include <string> #include <memory.h> #include <algorithm> using namespace ...
阅读全文
摘要:题目:Compromise (POJ 2250) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=2250 #include <iostream> #include <string> #include <vector> #include <memory.h> #include <algorithm> ...
阅读全文
摘要:题目:Common Subsequence (POJ 1458) 链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1458 #include <iostream> #include <string> #include <algorithm> using namespace std; in...
阅读全文

浙公网安备 33010602011771号