@skysbjdy
2016-12-24T06:41:53.000000Z
字数 7548
阅读 3417
Interview
Copy List with Random Pointer
LeetCode
RandomListNode *copyRandomList(RandomListNode *head) {
map<RandomListNode*, RandomListNode*> hash;
RandomListNode* cur = head;
RandomListNode dummy(0);
RandomListNode* newCur = &dummy;
while(cur) {
//只能new一个指针, 而不能声明一个局部对象
newCur->next = new RandomListNode(cur->label);
hash[cur] = newCur->next;
newCur = newCur->next;
cur = cur->next;
}
cur = head;
newCur = dummy.next;
while(cur) {
if (cur->random != NULL) //不一定每个都有哦!!!
newCur->random = hash[cur->random];
cur = cur->next;
newCur = newCur->next;
}
return dummy.next;
}
Order Dependency
class Order {
public:
string orderName;
Order(){orderName = "NoName";}
Order(string name) {
orderName = name;
}
};
class OrderDependency {
public:
Order cur;
Order pre;
OrderDependency(Order pre, Order cur) {
this->pre = pre;
this->cur = cur;
}
};
map<string, vector<string>> makeGraph(vector<OrderDependency> depends);
map<string, int> makeIndegree(vector<OrderDependency> depends);
map<string, Order> makeOrders(vector<OrderDependency> depends);
vector<Order> findOrder(vector<OrderDependency> depends) {
map<string, vector<string>> graph = makeGraph(depends); //是vector而不是set, 就是需要他重复
map<string, int> indegree = makeIndegree(depends);
map<string, Order> nameToOrder = makeOrders(depends);
int num = nameToOrder.size();
vector<Order> res;
queue<string> q;
for (std::map<string, int>::iterator it = indegree.begin(); it != indegree.end(); it++) {
if (it->second == 0) {
q.push(it->first);
}
}
while (!q.empty()) {
string name = q.front();
q.pop();
res.push_back(nameToOrder[name]);
for(string neighbor : graph[name]) {
indegree[neighbor]--;
//cout << neighbor << " == " << indegree[neighbor] << endl;
if (indegree[neighbor] == 0) {
q.push(neighbor);
}
}
}
if (res.size() != num) {
res.clear();
cout << "invalid: there is a cycle" << endl;
}
return res;
}
map<string, Order> makeOrders(vector<OrderDependency> depends) {
map<string, Order> orders;
for (int i = 0; i < depends.size(); i++) {
orders[depends[i].pre.orderName] = depends[i].pre;
orders[depends[i].cur.orderName] = depends[i].cur;
}
return orders;
}
map<string, vector<string>> makeGraph(vector<OrderDependency> depends) {
map<string, vector<string>> graph;
for (int i = 0; i < depends.size(); i++) {
graph[depends[i].pre.orderName].push_back(depends[i].cur.orderName);
}
return graph;
}
map<string, int> makeIndegree(vector<OrderDependency> depends) {
map<string, int> indegree;
for (int i = 0; i < depends.size(); i++) {
if (indegree.find(depends[i].pre.orderName) == indegree.end()) {
indegree[depends[i].pre.orderName] = 0;
}
indegree[depends[i].cur.orderName]++;
}
return indegree;
}
Minimum Spanning Tree
class Connection{
public:
string node1;
string node2;
int cost;
Connection(string a, string b, int c){
node1 = a;
node2 = b;
cost = c;
}
};
int comp(const void* a, const void* b);
int comp2(const void* a, const void* b);
bool unionFind(Connection con, map<string, string>& parent);
string find(string node, map<string, string>& parent);
vector<Connection> getLowCost(vector<Connection> connections) {
vector<Connection> res;
map<string, string> parent;
set<string> nodes;
qsort(&connections[0], connections.size(), sizeof(Connection), comp);
for(int i = 0; i < connections.size(); i++) {
parent[connections[i].node1] = connections[i].node1;
parent[connections[i].node2] = connections[i].node2;
nodes.insert(connections[i].node1);
nodes.insert(connections[i].node2);
}
for (int i = 0; i < connections.size(); i++) {
if (unionFind(connections[i], parent)) {
res.push_back(connections[i]);
}
}
if (res.size() != nodes.size() - 1) {
res.clear();
return res;
}
qsort(&res[0], res.size(), sizeof(Connection), comp2);
return res;
}
int comp(const void* a, const void* b) {
return ((Connection*)a)->cost - ((Connection*)b)->cost;
}
int comp2(const void* a, const void* b) {
Connection con1 = *((Connection*)a);
Connection con2 = *((Connection*)b);
if (con1.node1 < con2.node1) {
return -1;
} else if (con1.node1 > con2.node1) {
return 1;
} else {
if (con1.node2 <= con2.node2) {
return -1;
} else {
return 1;
}
}
}
bool unionFind(Connection con, map<string, string>& parent) {
string p1 = find(con.node1, parent);
string p2 = find(con.node2, parent);
if (p1 == p2) {
return false;
}
parent[p1] = p2;
return true;
}
string find(string node, map<string, string>& parent) {
if (node == parent[node]) return node;
return find(parent[node], parent);
}
High Five
class Result{
public:
int id;
int value;
Result(int id, int value){
this->id = id;
this->value = value;
}
};
map<int, double> highFive(vector<Result> results) {
map<int, double> res;
map<int, priority_queue<int>> scoreTable;
for (Result rs: results) {
scoreTable[rs.id].push(rs.value);
}
for (map<int, priority_queue<int>>::iterator it = scoreTable.begin(); it != scoreTable.end(); it++) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += it->second.top();
it->second.pop();
}
//注意是float!!!
res[it->first] = (float)sum / 5;
}
return res;
}
Maximum Subtree of Average
class Node { //这个是题目给好的
public:
int val;
vector<Node> children;
Node(int val){
this->val = val;
children = vector<Node>();
}
};
//自己创建的class 方便保存结果
class sumCount {
public:
int sum;
int count;
sumCount(int a = 0, int b = 0) {
sum = a;
count = b;
}
};
sumCount dfs(Node root, Node& res, float& aver);
Node subtree(Node root) {
Node res(0);
float aver = 0;
dfs(root, res, aver);
return res;
}
sumCount dfs(Node root, Node& res, float& aver) {
sumCount sc(root.val, 1);
//如果是叶子节点 不能去替换res 和 aver!!!!!!!
if (root.children.size() == 0) {
return sc;
}
//做了两个事情! 1. dfs遍历孩子, 2. 计算当前root的sum, count
for (Node child: root.children) {
sumCount childSC = dfs(child, res, aver);
sc.sum += childSC.sum;
sc.count += childSC.count;
}
float curAver = (float)sc.sum / sc.count;
if (curAver > aver) {
aver = curAver;
res = root;
}
return sc;
}
Find K Nearest Point
k <= 0 return new Point[0]是一个test case
k > size算是返回原数组,但是里面的points要sort一边,距离最近的点是第一个,第二近的是 第二个and so on
class point {
public:
int x;
int y;
point(int x_value, int y_value):x(x_value), y(y_value) {};
};
float distance(const point& a, const point& b = {0, 0}) {
int dx = a.x - b.x;
int dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
bool comp(const point& a, const point& b) {
point origin(0, 0);
return distance(a, {0, 0}) < distance(b, {0, 0});
}
struct mycomp {
bool operator()(const point& a, const point& b) {
point origin(0, 0);
return distance(a, origin) < distance(b, origin);
}
};
vector<point> KNearestPoint(vector<point> points, point origin, int k) {
priority_queue<point, std::vector<point>, decltype(&comp)> pq(&comp);
//priority_queue<point, std::vector<point>, mycomp> pq;
for (point p : points) {
pq.push(p);
if (pq.size() > k) {
pq.pop();
}
}
vector<point> res;
while (!pq.empty()) {
res.push_back(pq.top());
pq.pop();
}
return res;
}
Longest Palindromic substring
LeetCode
string longestPalindrome(string s) {
int max = 0;
string res = "";
for (int i = 0; i < s.size(); i++) {
int left = i;
int right = i;
while(left - 1 >= 0 && right + 1 < s.size() && s[left - 1] == s[right + 1]) {
left--;
right++;
}
if (right - left + 1 > max) {
max = right - left + 1;
res = s.substr(left, right - left + 1);
}
if (i + 1 < s.size() && s[i] == s[i + 1]) {
left = i;
right = i + 1;
while(left - 1 >= 0 && right + 1 < s.size() && s[left - 1] == s[right + 1]) {
left--;
right++;
}
if (right - left + 1 > max) {
max = right - left + 1;
res = s.substr(left, right - left + 1);
}
}
}
return res;
}
Overlap Rectangle
//这个题目是左上角和右下角!!
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
// If one rectangle is on left side of other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above other
if (l1.y < r2.y || l2.y < r1.y)
return false;
return true;
}
//预处理, 不管是左上角+右下角 还是 左下角+右上角
//都统一为左上角+右上角
bool overlap(Point l1, Point r1, Point l2, Point r2) {
int a = min(l1.x, r1.x);
int b = max(l1.y, r1.y);
int c = max(l1.x, r1.x);
int d = min(l1.y, r1.y);
int e = min(l2.x, r2.x);
int f = max(l2.y, r2.y);
int g = max(l2.x, r2.x);
int h = min(l2.y, r2.y);
if (d > f || h > b) return false;
if (c < e || g < a) return false;
return true;
}
(LeetCode计算面积)[https://leetcode.com/problems/rectangle-area/]
//!!注意这个题目给出的点是 左下角和右上角!!!! 而不是左上角和右下角
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (C - A) * (D - B);
int area2 = (G - E) * (H - F);
int left = max(A, E);
int right = min (C, G);
int bottom = max(B, F);
int top = min(D, H);
int overlap = 0;
if (right > left && top > bottom)
overlap = (right - left) * (top - bottom);
return area1 + area2 - overlap;
}
Window Sum
vector<int> windowSum(vector<int> vect, int k) {
vector<int> res;
if (vect.size() == 0 || k <= 0) return res;
int sum = 0;
for (int i = 0; i < k; i++) {
sum += vect[i];
}
res.push_back(sum);
for (int i = 0; i < vect.size() - k; i++) { //注意这里的个数, 前面以及把第一项加进去了!! 所以不是size - k + 1
sum = sum - vect[i] + vect[i + k];
res.push_back(sum);
}
return res;
}