📣 独立日限时特惠: VIP通行证立减$68
查看: 3032| 回复: 17
跳转到指定楼层
上一主题 下一主题
收起左侧

【求答案】最近面过最恶心的两道题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
条件: 6~8小时,超时可以继续做题,直到做出答案。。
第一道题:input是 string digits (example: "124345353"), int target (example: 7),添加“+”或者“*”进去使得答案为target。Return string type, 如果没有答案,return "No solution"


第二道题,google上找到题目,没找到思路,答案做出一半不到,已经没超时了。。真的是**。。

题目: http://www-personal.engin.umd.um ... 488/sample-test.txt


补充内容 (2018-1-19 10:57):
已经有答案了,可以用来参考一下

上一篇:刷题怎么开始?
下一篇:刷题30天倒计时
推荐
magicsets 2018-1-19 23:36:21 | 只看该作者
全局:
最后答案就是题目样例给的那样,可以快速用这个online C++工具执行一下:
http://cpp.sh/3qhuej


  1. 6 tiles
  2. link 1
  3. 11
  4. 23

  5. link 2
  6. 44
  7. 11
  8. 11
  9. 23

  10. link 3
  11. 44  
  12. 11  
  13. 1116
  14. 2336

  15. link 4
  16.   44  
  17. 5111  
  18. 711116
  19.   2336

  20. link 5
  21.   44   
  22. 5111   
  23. 71111662
  24.   233664

  25. link 6
  26.   4448  
  27. 511116  
  28. 71111662
  29.   233664

  30. --------

  31. 7 tiles
  32. link 1
  33. 12
  34. 34

  35. link 2
  36. 5112
  37. 6334

  38. link 3
  39. 755112
  40. 866334

  41. link 4
  42. 97755112
  43. 08866334

  44. link 5
  45. 97755112
  46. 08866334
  47.    66   
  48.    42   

  49. link 6
  50. 97755112
  51. 08866334
  52. 08 66   
  53. 18 42   

  54. link 7
  55. 97755112
  56. 08866334
  57. 08 66 34
  58. 18 42 95

  59. --------

  60. 10 tiles
  61. link 1
  62. 11
  63. 12

  64. link 2
  65. 11
  66. 12
  67. 12
  68. 93

  69. link 3
  70. 11
  71. 12
  72. 12
  73. 93
  74. 93
  75. 99

  76. link 4
  77. 11  
  78. 12  
  79. 12  
  80. 93  
  81. 9339
  82. 9998

  83. link 5
  84. 11   
  85. 12   
  86. 12   
  87. 93   
  88. 933995
  89. 999886

  90. link 6
  91. 11      
  92. 12      
  93. 12      
  94. 93      
  95. 93399557
  96. 99988666

  97. link 7
  98. 11      
  99. 12      
  100. 12    77
  101. 93    57
  102. 93399557
  103. 99988666

  104. link 8
  105. 1113   
  106. 1224   
  107. 12    77
  108. 93    57
  109. 93399557
  110. 99988666

  111. link 9
  112. 1113   
  113. 122445  
  114. 12  4777
  115. 93    57
  116. 93399557
  117. 99988666

  118. link 10
  119. 1113   
  120. 122445  
  121. 12244777
  122. 9339  57
  123. 93399557
  124. 99988666
复制代码

评分

参与人数 1大米 +4 收起 理由
麻倉枼 + 4 太棒了!

查看全部评分

回复

使用道具 举报

推荐
magicsets 2018-1-19 15:05:37 | 只看该作者
全局:
本帖最后由 magicsets 于 2018-1-19 23:33 编辑

第二个题目确实比较复杂,这种工程上的复杂性可以通过抽象、分解模块并封装来处理

我试着写了一下,主要定义了5个类:
Point, Rectangle, Tile, Canvas, TileManager

其中Point和Rectangle用于服务坐标计算以及对拼图边界的处理

Tile表示一个2X2的砖片,支持旋转操作

Canvas用于抽象一个“无边界”,支持负数坐标的拼图

在实现了以上基础设施的情况下,再实现拼图算法,结构和代码就比较清晰了
  1. #include <array>
  2. #include <cstddef>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <memory>
  6. #include <stdexcept>
  7. #include <string>
  8. #include <unordered_set>
  9. #include <vector>

  10. #define DISALLOW_COPY_AND_ASSIGN(classname) \
  11.   classname(const classname &other) = delete; \
  12.   classname& operator=(const classname &other) = delete

  13. struct Point {
  14.   int x, y;

  15.   Point shift(const Point &delta) const {
  16.     return { x + delta.x, y + delta.y };
  17.   }
  18.   Point operator-() const {
  19.     return { -x, -y };
  20.   }
  21.   bool operator==(const Point &other) const {
  22.     return x == other.x && y == other.y;
  23.   }
  24. };

  25. struct PointHasher {
  26.   std::size_t operator()(const Point &loc) const {
  27.     return (static_cast<std::size_t>(loc.x) << 32) | static_cast<std::size_t>(loc.y);
  28.   }
  29. };

  30. struct Rectangle {
  31.   int left, right, top, bottom;

  32.   int width() const {
  33.     return right - left + 1;
  34.   }
  35.   int area() const {
  36.     return (right - left + 1) * (bottom - top + 1);
  37.   }
  38.   bool contains(const Point &loc) const {
  39.     return loc.x >= left && loc.x <= right && loc.y >= top && loc.y <= bottom;
  40.   }
  41.   int address(const int x, const int y) const {
  42.     return x - left + (y - top) * width();
  43.   }
  44.   Rectangle intersect(const Rectangle &other) const {
  45.     return { std::max(left, other.left),
  46.              std::min(right, other.right),
  47.              std::max(top, other.top),
  48.              std::min(bottom, other.bottom) };
  49.   }
  50.   Rectangle extend(const Rectangle &other) const {
  51.     return { std::min(left, other.left),
  52.              std::max(right, other.right),
  53.              std::min(top, other.top),
  54.              std::max(bottom, other.bottom) };
  55.   }
  56. };

  57. class Canvas {
  58. public:
  59.   Canvas(const Rectangle &frame, const char blank)
  60.       : frame_(frame), blank_(blank),
  61.         cells_(static_cast<char*>(std::malloc(frame_.area()))) {
  62.     std::memset(cells_, blank_, frame_.area());
  63.   }

  64.   ~Canvas() {
  65.     std::free(cells_);
  66.   }

  67.   char& operator[](const Point &loc) {
  68.     if (!frame_.contains(loc)) {
  69.       const Rectangle nframe = {
  70.           Extend(loc.x, frame_.left), Extend(loc.x, frame_.right),
  71.           Extend(loc.y, frame_.top), Extend(loc.y, frame_.bottom)
  72.       };
  73.       resize(nframe);
  74.     }
  75.     return cells_[frame_.address(loc.x, loc.y)];
  76.   }

  77.   std::string toString(const Rectangle &box) const {
  78.     const Rectangle region = frame_.intersect(box);

  79.     std::string out;
  80.     for (int y = region.top; y <= region.bottom; ++y) {
  81.       out.append(cells_ + frame_.address(region.left, y), region.width());
  82.       out.push_back('\n');
  83.     }
  84.     return out;
  85.   }

  86. private:
  87.   static int Extend(const int value, int bound) {
  88.     if (bound < 0) {
  89.       while (bound > value) {
  90.         bound *= 2;
  91.       }
  92.     } else {
  93.       while (bound < value) {
  94.         bound *= 2;
  95.       }
  96.     }
  97.     return bound;
  98.   }

  99.   void resize(const Rectangle &nframe) {
  100.     char *ncells = static_cast<char*>(std::malloc(nframe.area()));
  101.     std::memset(ncells, blank_, nframe.area());

  102.     for (int y = frame_.top; y <= frame_.bottom; ++y) {
  103.       std::memcpy(ncells + nframe.address(frame_.left, y),
  104.                   cells_ + frame_.address(frame_.left, y),
  105.                   frame_.width());
  106.     }

  107.     std::free(cells_);
  108.     cells_ = ncells;
  109.     frame_ = nframe;
  110.   }

  111.   Rectangle frame_;
  112.   const char blank_;
  113.   char *cells_;

  114.   DISALLOW_COPY_AND_ASSIGN(Canvas);
  115. };

  116. class Tile {
  117. public:
  118.   Tile(const char lu, const char ru, const char rb, const char lb)
  119.       : cells_({{lu, ru, rb, lb}}) {
  120.   }

  121.   Tile(const Tile &other) {
  122.     std::memcpy(cells_.data(), other.cells_.data(), sizeof(cells_));
  123.   }

  124.   Tile rotate90() const {
  125.     return Tile(cells_[3], cells_[0], cells_[1], cells_[2]);
  126.   }

  127.   char operator[](const int corner) const {
  128.     return cells_[corner];
  129.   }

  130. private:
  131.   //  0    1
  132.   //    XX
  133.   //    XX
  134.   //  3    2
  135.   std::array<char, 4> cells_;
  136. };

  137. class TileManager {
  138. public:
  139.   TileManager()
  140.       : canvas_({-8, 8, -8, 8}, ' '),
  141.         shadow_({-8, 8, -8, 8}, kEmptyCell),
  142.         bounds_({0, 0, 0, 0}) {
  143.   }

  144.   void link(const char **tiles) {
  145.     std::vector<Tile> input;
  146.     while (true) {
  147.       input.emplace_back(tiles[0][0], tiles[0][1], tiles[1][1], tiles[1][0]);
  148.       if (tiles[2] == 0) {
  149.         break;
  150.       }
  151.       tiles += 3;
  152.     }

  153.     std::cout << input.size() << " " << "tiles\n";
  154.     for (int i = 0; i < input.size(); ++i) {
  155.       link(input[i]);
  156.       std::cout << "link " << (i + 1) << "\n";
  157.       std::cout << canvas_.toString(bounds_) << "\n";
  158.     }
  159.   }

  160. private:
  161.   void link(Tile tile) {
  162.     const Point loc = locate(tile);

  163.     for (int dir = 0; dir < 4; ++dir) {
  164.       const Point cell = loc.shift(kTileOffsets[dir]);
  165.       slots_.erase(cell);

  166.       const char ch = tile[dir];
  167.       canvas_[cell] = ch;
  168.       shadow_[cell] = kInvalidCell;

  169.       for (int i = 0; i < 2; ++i) {
  170.         const Point slot = cell.shift(kSlotOffsets[dir * 2 + i]);
  171.         char &orig = shadow_[slot];

  172.         if (orig == kEmptyCell) {
  173.           orig = ch;
  174.           slots_.emplace(slot);
  175.         } else if (orig != kInvalidCell && orig != ch) {
  176.           orig = kInvalidCell;
  177.           slots_.erase(slot);
  178.         }
  179.       }
  180.     }

  181.     bounds_ = bounds_.extend({loc.x, loc.x + 1, loc.y, loc.y + 1});
  182.   }

  183.   Point locate(Tile &target) {
  184.     if (slots_.empty()) {
  185.       return {0, 0};
  186.     }

  187.     for (const Point &slot : slots_) {
  188.       for (const Point &offset : kTileOffsets) {
  189.         const Point loc = slot.shift(-offset);
  190.         for (int rotate = 0; rotate < 4; ++rotate) {
  191.           target = target.rotate90();
  192.           if (matches(target, loc)) {
  193.             return loc;
  194.           }
  195.         }
  196.       }
  197.     }
  198.     throw std::runtime_error("Invalid input!");
  199.   }

  200.   bool matches(const Tile &tile, const Point &loc) {
  201.     int count = 0;
  202.     for (int dir = 0; dir < 4; ++dir) {
  203.       const char expected = shadow_[loc.shift(kTileOffsets[dir])];
  204.       if (expected == kEmptyCell) {
  205.         continue;
  206.       }
  207.       if (expected != tile[dir]) {
  208.         return false;
  209.       }
  210.       ++count;
  211.     }
  212.     return count >= 2;
  213.   }

  214.   Canvas canvas_;
  215.   Canvas shadow_;

  216.   Rectangle bounds_;
  217.   std::unordered_set<Point, PointHasher> slots_;

  218.   constexpr static char kEmptyCell = '.';
  219.   constexpr static char kInvalidCell = 'X';

  220.   const static std::array<Point, 4> kTileOffsets;
  221.   const static std::array<Point, 8> kSlotOffsets;

  222.   DISALLOW_COPY_AND_ASSIGN(TileManager);
  223. };

  224. const std::array<Point, 4> TileManager::kTileOffsets = {{
  225.     { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }
  226. }};

  227. const std::array<Point, 8> TileManager::kSlotOffsets = {{
  228.     { -1, 0 }, { 0, -1 },
  229.     { 0, -1 }, { 1, 0 },
  230.     { 1, 0 }, { 0, 1},
  231.     { 0, 1 }, { -1, 0 }
  232. }};

  233. int main(int argc, char *argv[]) {
  234.   std::unique_ptr<TileManager> tm;

  235.   tm = std::make_unique<TileManager>();
  236.   const char *tiles1[] = {
  237.       "11",     // 1
  238.       "23",
  239.       "",
  240.       "44",     // 2
  241.       "11",
  242.       "",
  243.       "16",     // 3
  244.       "36",
  245.       "",
  246.       "51",     // 4
  247.       "71",
  248.       "",
  249.       "46",     // 5
  250.       "26",
  251.       "",
  252.       "14",     // 5
  253.       "68",
  254.       0
  255.   };
  256.   tm->link(tiles1);

  257.   std::cout << "--------\n\n";

  258.   tm = std::make_unique<TileManager>();
  259.   const char *tiles2[] = {
  260.       "12",  // 1
  261.       "34",
  262.       "",
  263.       "36",  // 2
  264.       "15",
  265.       "",
  266.       "56",  // 3
  267.       "78",
  268.       "",
  269.       "78",  // 4
  270.       "90",
  271.       "",
  272.       "46",  // 5
  273.       "26",
  274.       "",
  275.       "88",  // 6
  276.       "01",
  277.       "",
  278.       "93",  // 7
  279.       "54",
  280.       0
  281.   };
  282.   tm->link(tiles2);

  283.   std::cout << "--------\n\n";

  284.   tm = std::make_unique<TileManager>();
  285.   const char *tiles3[] = {
  286.       "11",     // 1
  287.       "12",
  288.       "",
  289.       "12",     // 2
  290.       "93",
  291.       "",
  292.       "93",     // 3
  293.       "99",
  294.       "",
  295.       "39",     // 4
  296.       "98",
  297.       "",
  298.       "95",     // 5
  299.       "86",
  300.       "",
  301.       "57",     // 6
  302.       "66",
  303.       "",
  304.       "77",     // 7
  305.       "57",
  306.       "",
  307.       "13",     // 8
  308.       "24",
  309.       "",
  310.       "45",     // 9
  311.       "47",
  312.       "",
  313.       "24",     // 10
  314.       "39",
  315.       0
  316.   };
  317.   tm->link(tiles3);

  318.   return 0;
  319. }
复制代码

评分

参与人数 1大米 +4 收起 理由
麻倉枼 + 4 大神!最后答案打印出来了吗?

查看全部评分

回复

使用道具 举报

推荐
真淘蛮 2018-1-20 04:09:34 | 只看该作者
全局:
fb 的题,见到有人写过,对的话加点米吧
public List<String> equation(String e) {
        List<String> res = new LinkedList<>();
        if (e.length() == 0) return res;
        String[] temp = e.split("=");
        String left = temp[0], right = temp[1];
        Map<Integer, List<String>> l = new HashMap<>();
        findResult(l, left, 0, 0, "");
        Map<Integer, List<String>> r = new HashMap<>();
        findResult(r, right, 0, 0, "");
        for (Integer num1 : l.keySet()) {
            if (r.containsKey(num1)) {
                for (String ll : l.get(num1)) {
                    for (String rr : r.get(num1)) {
                        res.add(ll + "=" + rr);
                    }
                }
            }
        }
        return res;
    }

    public void findResult(Map<Integer, List<String>> map, String str, int start, int sum, String cur) {
        if (start == str.length()) {
            if (!map.containsKey(sum)) map.put(sum, new LinkedList<>());
            map.get(sum).add(cur);
            return;
        }
        for (int i = start; i < str.length(); i++) {
            String s = str.substring(start, i + 1);
            if (s.length() > 1 && s.charAt(0) == '0') break;
            int num = Integer.parseInt(s);
            findResult(map, str, i + 1, sum + num, cur + "+" + s);
            findResult(map, str, i + 1, sum - num, cur + "-" + s);
        }
    }

评分

参与人数 1大米 +1 收起 理由
麻倉枼 + 1 感谢分享啦

查看全部评分

回复

使用道具 举报

🔗
littlegirl 2018-1-18 16:26:20 | 只看该作者
全局:
第一題是leetcode 282
PS可以帮忙加米吗?

评分

参与人数 1大米 +1 收起 理由
麻倉枼 + 1 我就知道肯定是lc题。。但我想知道第二题答.

查看全部评分

回复

使用道具 举报

🔗
ouyangivan 2018-1-18 21:21:56 | 只看该作者
全局:
哟西,校友啊,要不要一起抱团刷题
回复

使用道具 举报

🔗
 楼主| 麻倉枼 2018-1-18 23:33:19 | 只看该作者
全局:
ouyangivan 发表于 2018-1-18 08:21
哟西,校友啊,要不要一起抱团刷题

我好久没刷题了。怎么刷都会忘记答案,现在巩固C++基础和其他东西中。
回复

使用道具 举报

🔗
伊罗伊罗 2018-1-19 13:17:09 | 只看该作者
全局:
ouyangivan 发表于 2018-1-18 21:21
哟西,校友啊,要不要一起抱团刷题

求一起刷题!!!
回复

使用道具 举报

🔗
伊罗伊罗 2018-1-19 13:19:24 | 只看该作者
全局:
第二题是不是深度优先搜索。需要找到一条长度为N的路径。每个tile经过旋转可以产生4个点,这四个需要指向同一个用来表示visited的object。不过这样做复杂度有点高 要n平方。不知道有没有更好的方法。
回复

使用道具 举报

🔗
 楼主| 麻倉枼 2018-1-19 14:08:33 | 只看该作者
全局:
伊罗伊罗 发表于 2018-1-19 00:19
第二题是不是深度优先搜索。需要找到一条长度为N的路径。每个tile经过旋转可以产生4个点,这四个需要指向同 ...

我的答案比较bruteforce,可能你做出来后看看可不可以?
另外这道题是C的,被我强制改成C++(用了vector)
回复

使用道具 举报

🔗
 楼主| 麻倉枼 2018-1-20 00:03:57 | 只看该作者
全局:
本帖最后由 麻倉枼 于 2018-1-19 11:13 编辑
magicsets 发表于 2018-1-19 10:36
最后答案就是题目样例给的那样,可以快速用这个online C++工具执行一下:
http://cpp.sh/3qhuej

大神我感觉你C++功底很强,请教一下平时除了看书多练习外怎样提高C++功力呢?

一些很基础的debug,我现在都不知道怎样做好:
1.如何debug template?
2.如何debug smart pointer?
3.如何debug lvalue和rvalue的程序?(Ex:如何debug 一个argument是copy了还是move了)
4.如何debug initializer list constructor?

其次我想请教下,哪里可以学习,怎样练习这些内容:
1. template
2. variadic template
3. Serialization
4. Reflection
5. Proxy class
6. move (rvalue)
暂时这么多,顺便发一贴当做自己学习checklist
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表