[关闭]
@skysbjdy 2016-12-24T06:41:53.000000Z 字数 7548 阅读 3338

亚马逊 9

Interview


  1. Copy List with Random Pointer
    LeetCode

    1. RandomListNode *copyRandomList(RandomListNode *head) {
    2. map<RandomListNode*, RandomListNode*> hash;
    3. RandomListNode* cur = head;
    4. RandomListNode dummy(0);
    5. RandomListNode* newCur = &dummy;
    6. while(cur) {
    7. //只能new一个指针, 而不能声明一个局部对象
    8. newCur->next = new RandomListNode(cur->label);
    9. hash[cur] = newCur->next;
    10. newCur = newCur->next;
    11. cur = cur->next;
    12. }
    13. cur = head;
    14. newCur = dummy.next;
    15. while(cur) {
    16. if (cur->random != NULL) //不一定每个都有哦!!!
    17. newCur->random = hash[cur->random];
    18. cur = cur->next;
    19. newCur = newCur->next;
    20. }
    21. return dummy.next;
    22. }
  2. Order Dependency

    1. class Order {
    2. public:
    3. string orderName;
    4. Order(){orderName = "NoName";}
    5. Order(string name) {
    6. orderName = name;
    7. }
    8. };
    9. class OrderDependency {
    10. public:
    11. Order cur;
    12. Order pre;
    13. OrderDependency(Order pre, Order cur) {
    14. this->pre = pre;
    15. this->cur = cur;
    16. }
    17. };
    18. map<string, vector<string>> makeGraph(vector<OrderDependency> depends);
    19. map<string, int> makeIndegree(vector<OrderDependency> depends);
    20. map<string, Order> makeOrders(vector<OrderDependency> depends);
    21. vector<Order> findOrder(vector<OrderDependency> depends) {
    22. map<string, vector<string>> graph = makeGraph(depends); //是vector而不是set, 就是需要他重复
    23. map<string, int> indegree = makeIndegree(depends);
    24. map<string, Order> nameToOrder = makeOrders(depends);
    25. int num = nameToOrder.size();
    26. vector<Order> res;
    27. queue<string> q;
    28. for (std::map<string, int>::iterator it = indegree.begin(); it != indegree.end(); it++) {
    29. if (it->second == 0) {
    30. q.push(it->first);
    31. }
    32. }
    33. while (!q.empty()) {
    34. string name = q.front();
    35. q.pop();
    36. res.push_back(nameToOrder[name]);
    37. for(string neighbor : graph[name]) {
    38. indegree[neighbor]--;
    39. //cout << neighbor << " == " << indegree[neighbor] << endl;
    40. if (indegree[neighbor] == 0) {
    41. q.push(neighbor);
    42. }
    43. }
    44. }
    45. if (res.size() != num) {
    46. res.clear();
    47. cout << "invalid: there is a cycle" << endl;
    48. }
    49. return res;
    50. }
    51. map<string, Order> makeOrders(vector<OrderDependency> depends) {
    52. map<string, Order> orders;
    53. for (int i = 0; i < depends.size(); i++) {
    54. orders[depends[i].pre.orderName] = depends[i].pre;
    55. orders[depends[i].cur.orderName] = depends[i].cur;
    56. }
    57. return orders;
    58. }
    59. map<string, vector<string>> makeGraph(vector<OrderDependency> depends) {
    60. map<string, vector<string>> graph;
    61. for (int i = 0; i < depends.size(); i++) {
    62. graph[depends[i].pre.orderName].push_back(depends[i].cur.orderName);
    63. }
    64. return graph;
    65. }
    66. map<string, int> makeIndegree(vector<OrderDependency> depends) {
    67. map<string, int> indegree;
    68. for (int i = 0; i < depends.size(); i++) {
    69. if (indegree.find(depends[i].pre.orderName) == indegree.end()) {
    70. indegree[depends[i].pre.orderName] = 0;
    71. }
    72. indegree[depends[i].cur.orderName]++;
    73. }
    74. return indegree;
    75. }
  3. Minimum Spanning Tree

    1. class Connection{
    2. public:
    3. string node1;
    4. string node2;
    5. int cost;
    6. Connection(string a, string b, int c){
    7. node1 = a;
    8. node2 = b;
    9. cost = c;
    10. }
    11. };
    12. int comp(const void* a, const void* b);
    13. int comp2(const void* a, const void* b);
    14. bool unionFind(Connection con, map<string, string>& parent);
    15. string find(string node, map<string, string>& parent);
    16. vector<Connection> getLowCost(vector<Connection> connections) {
    17. vector<Connection> res;
    18. map<string, string> parent;
    19. set<string> nodes;
    20. qsort(&connections[0], connections.size(), sizeof(Connection), comp);
    21. for(int i = 0; i < connections.size(); i++) {
    22. parent[connections[i].node1] = connections[i].node1;
    23. parent[connections[i].node2] = connections[i].node2;
    24. nodes.insert(connections[i].node1);
    25. nodes.insert(connections[i].node2);
    26. }
    27. for (int i = 0; i < connections.size(); i++) {
    28. if (unionFind(connections[i], parent)) {
    29. res.push_back(connections[i]);
    30. }
    31. }
    32. if (res.size() != nodes.size() - 1) {
    33. res.clear();
    34. return res;
    35. }
    36. qsort(&res[0], res.size(), sizeof(Connection), comp2);
    37. return res;
    38. }
    39. int comp(const void* a, const void* b) {
    40. return ((Connection*)a)->cost - ((Connection*)b)->cost;
    41. }
    42. int comp2(const void* a, const void* b) {
    43. Connection con1 = *((Connection*)a);
    44. Connection con2 = *((Connection*)b);
    45. if (con1.node1 < con2.node1) {
    46. return -1;
    47. } else if (con1.node1 > con2.node1) {
    48. return 1;
    49. } else {
    50. if (con1.node2 <= con2.node2) {
    51. return -1;
    52. } else {
    53. return 1;
    54. }
    55. }
    56. }
    57. bool unionFind(Connection con, map<string, string>& parent) {
    58. string p1 = find(con.node1, parent);
    59. string p2 = find(con.node2, parent);
    60. if (p1 == p2) {
    61. return false;
    62. }
    63. parent[p1] = p2;
    64. return true;
    65. }
    66. string find(string node, map<string, string>& parent) {
    67. if (node == parent[node]) return node;
    68. return find(parent[node], parent);
    69. }
  4. High Five

    1. class Result{
    2. public:
    3. int id;
    4. int value;
    5. Result(int id, int value){
    6. this->id = id;
    7. this->value = value;
    8. }
    9. };
    10. map<int, double> highFive(vector<Result> results) {
    11. map<int, double> res;
    12. map<int, priority_queue<int>> scoreTable;
    13. for (Result rs: results) {
    14. scoreTable[rs.id].push(rs.value);
    15. }
    16. for (map<int, priority_queue<int>>::iterator it = scoreTable.begin(); it != scoreTable.end(); it++) {
    17. int sum = 0;
    18. for (int i = 0; i < 5; i++) {
    19. sum += it->second.top();
    20. it->second.pop();
    21. }
    22. //注意是float!!!
    23. res[it->first] = (float)sum / 5;
    24. }
    25. return res;
    26. }
  5. Maximum Subtree of Average

    1. class Node { //这个是题目给好的
    2. public:
    3. int val;
    4. vector<Node> children;
    5. Node(int val){
    6. this->val = val;
    7. children = vector<Node>();
    8. }
    9. };
    10. //自己创建的class 方便保存结果
    11. class sumCount {
    12. public:
    13. int sum;
    14. int count;
    15. sumCount(int a = 0, int b = 0) {
    16. sum = a;
    17. count = b;
    18. }
    19. };
    20. sumCount dfs(Node root, Node& res, float& aver);
    21. Node subtree(Node root) {
    22. Node res(0);
    23. float aver = 0;
    24. dfs(root, res, aver);
    25. return res;
    26. }
    27. sumCount dfs(Node root, Node& res, float& aver) {
    28. sumCount sc(root.val, 1);
    29. //如果是叶子节点 不能去替换res 和 aver!!!!!!!
    30. if (root.children.size() == 0) {
    31. return sc;
    32. }
    33. //做了两个事情! 1. dfs遍历孩子, 2. 计算当前root的sum, count
    34. for (Node child: root.children) {
    35. sumCount childSC = dfs(child, res, aver);
    36. sc.sum += childSC.sum;
    37. sc.count += childSC.count;
    38. }
    39. float curAver = (float)sc.sum / sc.count;
    40. if (curAver > aver) {
    41. aver = curAver;
    42. res = root;
    43. }
    44. return sc;
    45. }
  6. Find K Nearest Point

    k <= 0 return new Point[0]是一个test case
    k > size算是返回原数组,但是里面的points要sort一边,距离最近的点是第一个,第二近的是 第二个and so on

    1. class point {
    2. public:
    3. int x;
    4. int y;
    5. point(int x_value, int y_value):x(x_value), y(y_value) {};
    6. };
    7. float distance(const point& a, const point& b = {0, 0}) {
    8. int dx = a.x - b.x;
    9. int dy = a.y - b.y;
    10. return sqrt(dx * dx + dy * dy);
    11. }
    12. bool comp(const point& a, const point& b) {
    13. point origin(0, 0);
    14. return distance(a, {0, 0}) < distance(b, {0, 0});
    15. }
    16. struct mycomp {
    17. bool operator()(const point& a, const point& b) {
    18. point origin(0, 0);
    19. return distance(a, origin) < distance(b, origin);
    20. }
    21. };
    22. vector<point> KNearestPoint(vector<point> points, point origin, int k) {
    23. priority_queue<point, std::vector<point>, decltype(&comp)> pq(&comp);
    24. //priority_queue<point, std::vector<point>, mycomp> pq;
    25. for (point p : points) {
    26. pq.push(p);
    27. if (pq.size() > k) {
    28. pq.pop();
    29. }
    30. }
    31. vector<point> res;
    32. while (!pq.empty()) {
    33. res.push_back(pq.top());
    34. pq.pop();
    35. }
    36. return res;
    37. }
  7. Longest Palindromic substring
    LeetCode

    1. string longestPalindrome(string s) {
    2. int max = 0;
    3. string res = "";
    4. for (int i = 0; i < s.size(); i++) {
    5. int left = i;
    6. int right = i;
    7. while(left - 1 >= 0 && right + 1 < s.size() && s[left - 1] == s[right + 1]) {
    8. left--;
    9. right++;
    10. }
    11. if (right - left + 1 > max) {
    12. max = right - left + 1;
    13. res = s.substr(left, right - left + 1);
    14. }
    15. if (i + 1 < s.size() && s[i] == s[i + 1]) {
    16. left = i;
    17. right = i + 1;
    18. while(left - 1 >= 0 && right + 1 < s.size() && s[left - 1] == s[right + 1]) {
    19. left--;
    20. right++;
    21. }
    22. if (right - left + 1 > max) {
    23. max = right - left + 1;
    24. res = s.substr(left, right - left + 1);
    25. }
    26. }
    27. }
    28. return res;
    29. }
  8. Overlap Rectangle

    1. //这个题目是左上角和右下角!!
    2. bool doOverlap(Point l1, Point r1, Point l2, Point r2)
    3. {
    4. // If one rectangle is on left side of other
    5. if (l1.x > r2.x || l2.x > r1.x)
    6. return false;
    7. // If one rectangle is above other
    8. if (l1.y < r2.y || l2.y < r1.y)
    9. return false;
    10. return true;
    11. }
    12. //预处理, 不管是左上角+右下角 还是 左下角+右上角
    13. //都统一为左上角+右上角
    14. bool overlap(Point l1, Point r1, Point l2, Point r2) {
    15. int a = min(l1.x, r1.x);
    16. int b = max(l1.y, r1.y);
    17. int c = max(l1.x, r1.x);
    18. int d = min(l1.y, r1.y);
    19. int e = min(l2.x, r2.x);
    20. int f = max(l2.y, r2.y);
    21. int g = max(l2.x, r2.x);
    22. int h = min(l2.y, r2.y);
    23. if (d > f || h > b) return false;
    24. if (c < e || g < a) return false;
    25. return true;
    26. }

    (LeetCode计算面积)[https://leetcode.com/problems/rectangle-area/]

    1. //!!注意这个题目给出的点是 左下角和右上角!!!! 而不是左上角和右下角
    2. int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    3. int area1 = (C - A) * (D - B);
    4. int area2 = (G - E) * (H - F);
    5. int left = max(A, E);
    6. int right = min (C, G);
    7. int bottom = max(B, F);
    8. int top = min(D, H);
    9. int overlap = 0;
    10. if (right > left && top > bottom)
    11. overlap = (right - left) * (top - bottom);
    12. return area1 + area2 - overlap;
    13. }
  9. Window Sum

    1. vector<int> windowSum(vector<int> vect, int k) {
    2. vector<int> res;
    3. if (vect.size() == 0 || k <= 0) return res;
    4. int sum = 0;
    5. for (int i = 0; i < k; i++) {
    6. sum += vect[i];
    7. }
    8. res.push_back(sum);
    9. for (int i = 0; i < vect.size() - k; i++) { //注意这里的个数, 前面以及把第一项加进去了!! 所以不是size - k + 1
    10. sum = sum - vect[i] + vect[i + k];
    11. res.push_back(sum);
    12. }
    13. return res;
    14. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注