-
Cracking-- 4.7 在一颗二叉树中找两个节点的第一个共同祖先
摘要:分别记录从 root 到 node1 的path 到 node2的path,再找其中共同的部分struct Node{ int val; struct Node *left; struct Node *right; Node(int _val) { val...
阅读全文
-
priority_queue 示例
摘要:http://www.cplusplus.com/reference/queue/priority_queue/priority_queue 的top始终保持着为一堆数据中的最大元素。读取最小 O(1)插入和删除 lg(n)(真是又简便又好用,难怪g不要我,当时连这个都不会写,sigh...)#in...
阅读全文
-
heap c++ 操作 大顶堆、小顶堆
摘要:在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数。下面是一些示例应用http://www.cplusplus.com/reference/algorithm/pop_heap/#include #include ...
阅读全文
-
【转】当你在浏览器地址栏输入一个URL后回车,将会发生的事情?
摘要:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/http://www.cnblogs.com/panxueji/archive/2013/05/12/3073924.html作为一个软件开发者,你一定会对...
阅读全文
-
Cracking-- 1.1 判断字符串中是否有重复字符
摘要:第三种方法为位运算的方法。位运算符: #include #include #include #include using namespace std;//时间 O(n) 空间 O(1)bool hasSame(string str){ if(str.size() == 0 ) r...
阅读全文
-
LeetCode OJ-- Word Ladder II ***@
摘要:https://oj.leetcode.com/problems/word-ladder-ii/啊,终于过了class Solution {public: vector > findLadders(string start, string end, unordered_set &dict) {...
阅读全文
-
LeetCode OJ-- Valid Number **@
摘要:https://oj.leetcode.com/problems/valid-number/判断给的串,是不是合理的 数字形式主要问题在需求定义上吧class Solution {public: bool isNumber(const char *s) { if(s == NUL...
阅读全文
-
LeetCode OJ-- Wildcard Matching **@
摘要:https://oj.leetcode.com/problems/wildcard-matching/模拟通配符的匹配做法非常好 class Solution {public: bool isMatch(const char *s, const char *p) { bool h...
阅读全文
-
LeetCode OJ-- String to Integer (atoi) **
摘要:https://oj.leetcode.com/problems/string-to-integer-atoi/细节题,把一个字符串转换成整数class Solution {public: int atoi(const char *str) { if(str == NULL) ...
阅读全文
-
LeetCode OJ--Minimum Window Substring ***
摘要:https://oj.leetcode.com/problems/minimum-window-substring/模拟题这道题细节比较多。从左到右扫一遍模拟着做 class Solution {public: string minWindow(string S, string T) { ...
阅读全文
-
LeetCode OJ-- Clone Graph **@
摘要:https://oj.leetcode.com/problems/clone-graph/图的拷贝,就是给一个图,再弄出一个一模一样的来。/** * Definition for undirected graph. * struct UndirectedGraphNode { * int l...
阅读全文
-
LeetCode OJ-- Remove Element
摘要:https://oj.leetcode.com/problems/remove-element/简单处理class Solution {public: int removeElement(int A[], int n, int elem) { if(n == 0) ...
阅读全文
-
LeetCode OJ--Swap Nodes in Pairs
摘要:https://oj.leetcode.com/problems/swap-nodes-in-pairs/链表的处理/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *...
阅读全文
-
LeetCode OJ-- Scramble String ***@
摘要:https://oj.leetcode.com/problems/scramble-string/一个字符串的混排变换,简直太妙了,好题class Solution {public: bool isScramble(string s1, string s2) { if(s1.si...
阅读全文
-
LeetCode OJ-- Sort List **@
摘要:链表排序,要求使用 O(nlgn) 时间,常量空间。使用归并的思路/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int ...
阅读全文
-
LeetCode OJ-- Merge k Sorted Lists *@
摘要:https://oj.leetcode.com/problems/merge-k-sorted-lists/这道题主要是考虑测试数据的特点吧。刚开始的时候想,每次找出头结点中最小的两个,然后取最小的一个,一直取到它的值 > 倒数第二小的,之后重复这个过程。适合的数据特点为: 1 2 3 5 6 7 ...
阅读全文
-
LeetCode OJ-- Text Justification
摘要:https://oj.leetcode.com/problems/text-justification/细节题class Solution {public: vector fullJustify(vector &words, int L) { vector ans; ...
阅读全文
-
LeetCode OJ-- Interleaving String **@
摘要:https://oj.leetcode.com/problems/interleaving-string/刚开始用递归做,但是超时了class Solution {public: bool flag; bool isInterleave(string s1, string s2,...
阅读全文
-
二叉树遍历 Morris
摘要:二叉树的遍历,先根遍历,不适用递归,存储空间为 O(1)转自:http://chuansongme.com/n/100461MorrisInOrder(): while 没有结束 如果当前节点没有左后代 // 则它就是最左了 所以 读取该点 访问该节点 转向右节点 否则 ...
阅读全文
-
LeetCode OJ--Binary Tree Zigzag Level Order Traversal *
摘要:https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/树的层序遍历使用两个 stack 或者 vector 分别表示 当前层和下一层/** * Definition for binary tree * st...
阅读全文
-
LeetCode OJ-- Letter Combinations of a Phone Number ***
摘要:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/使用递归,深搜,使用 map 保存已经处理过的结果class Solution {public: unordered_map > memory; ...
阅读全文
-
【转】 堆和栈的区别
摘要:转自:http://blog.csdn.net/hairetz/article/details/4141043一、预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其 操作方式类似于数据...
阅读全文
-
LeetCode OJ-- Valid Sudoku
摘要:https://oj.leetcode.com/problems/valid-sudoku/给出数独的一部分来,验证给出的这一部分是否为 valid行上无重复的,且是从 ‘1’ 到‘9’列上无重复的,且是从 ‘1’ 到‘9’小的3*3无重复的,且是从 ‘1’ 到‘9’class Solution {...
阅读全文
-
LeetCode OJ--Word Break II ***@
摘要:https://oj.leetcode.com/problems/word-break-ii/class Solution {public: unordered_set dict; string s; unordered_map > memory; //用来记录一个string可...
阅读全文
-
LeetCode OJ-- Surrounded Regions **@
摘要:https://oj.leetcode.com/problems/surrounded-regions/棋盘类的题目。找出所有没有被 X 包围的 O使用深搜,但是太深了,run time error可以自己写个栈,模拟深搜,取数据存入,弹出的后来参考了九章struct point{ i...
阅读全文
|