01 2014 档案
摘要:LCA 分为几种情况1. Binary Search Tree 的 LCA2. Binary Tree with parent pointer3. Binary Tree without parent pointer4. ordinary Tree without parent pointerBinary Search Tree思路1. 对一个节点 node, 假如 n1,n2 均大/小于 node->val, 那么LCA一定在 node->right 分支上2. 若一个大于一个小于, 则 LCA 就是 node 本身3. updown 解法, log(n) 的时间复杂度Binar
阅读全文
摘要:在做 compiler 语义分析时, 需要用到 map在别人的代码上做扩展, 所以有些代码是不能动的这时, 需要一个 map 的数据结构, 但是我并不清楚 symbol 是否重载了 , 成功了#include #include using namespace std;class unknow {private: int index;public: int get_index() { return index; } void increment() { index++; } /* bool operatorindex mapping; unknow *u1 = new unknow(); ...
阅读全文
摘要:QuestionGiven two words (startandend), and a dictionary, find all shortest transformation sequence(s) fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot",&
阅读全文
摘要:题目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 order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.
阅读全文
摘要:题目Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longes
阅读全文
摘要:题目Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.思路1. 这题明说, label 独一无二, 那么就可以使用 hash map 存储元素2. BFS 搜索, 边搜边向 hash map 添加元素3. 在设置标记位上 TLE 了 N 次, 一个元素一旦被假如到 hash map, 就说明该元素已经被访问到了并已被假如到 queue 中, 同时环的问题也被克服了. 我在做的时候, 把环的问题拉出来单独处理, 但标记忘记了4. unordered_map 这种设
阅读全文
摘要:题目There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting g
阅读全文
摘要:题目Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is no
阅读全文
摘要:题目Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思路1. 使用数组模拟哈希表, 数组下标0-25分别代表字符'a'-'z', a[0] 代表 'a' 在单词中出现的次数2. 排序, 只有相邻的单词才有可能是相同的3. 这么慢的方法没想到 176ms 就能通过总结1. word 起初没有对 char 数组初始化, 结果 VS 成功运行, 但 Leetcode 上却是 W
阅读全文
摘要:题目Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思路1. 排序是 nlogn, 没有其他想法了2. 参考别人思路. 将 A[i] 月 A[A[i]] 替换, 第二遍 PASS 检查 A[i] == i代码class Solution {public: int f...
阅读全文
摘要:题目Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted acco
阅读全文
摘要:题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].思路:1.
阅读全文
摘要:题目大整数乘法思路:1. 转化成分组背包问题, 代码比较 tricky2. 将 string 相乘转化为 string 按位乘再加的过程3. 细节. 不同长度 string 相加可以先补 0, 对齐. 按位相乘的时候, 可以从左向右计算, 便于移位代码:class Solution {public: string multiply(string num1, string num2) { if(num1.size() len2) { res.append(num1.size()-num2.size(), '0').append(num2); num2 = res; } re...
阅读全文
摘要:题目: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.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0思路:1. 二分法变形, 最讨
阅读全文
摘要:题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of i
阅读全文
摘要:题目:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?update先转置, 后按列翻转class Solution {public: void rotate(vector > &matrix) { transpose(matrix); for(int i = 0; i > &matrix) { int n = matrix.size(...
阅读全文
摘要:题目Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units o
阅读全文
摘要:In C++, a derived class object can be assigned to base class, but the other way is not possible.class Base { int x,y;};class Derived : public Base { int z, w;};int main() { Derived d; Base b = d;}Object slicing happens when a derived class object assigned to base class, additional attributes of deri
阅读全文