kamacoder-solutions icon indicating copy to clipboard operation
kamacoder-solutions copied to clipboard

题36 网络路径和

Open Mantal-pelple opened this issue 1 year ago • 3 comments

第36题这种输入方式是不是有点考察的本末倒置了,[[2,3,1],[2,5,3],[4,2,1]]这种输入方式对于java来说有直接的函数可以用,但是对于c++或者其它语言来说,输入就要花费好长时间来想想怎么处理,可不可以卡尔考虑一下解决一下类似这种题目的输入问题,把问题的考察点重点放在算法而不是输入上?谢谢啦!

Mantal-pelple avatar Aug 25 '23 07:08 Mantal-pelple

确实,最近也做过很多笔试,一般都会让你自己处理输入,但是都是比较常规的,给你n行、1行m个数,然后读取n次、1次读取m个数这样子的,感觉牛客那样子的就够了

YakultGo avatar Aug 29 '23 04:08 YakultGo

同意!!!将数据转换为vector<vector>耗费最多的时间 贴一个比较简单、可能复杂度较高的方法:

std::vector<std::vector<int>> str2vec(const std::string& str) {
  std::vector<std::string> rows;

  auto start = str.find("[", 1);
  auto end = str.find("]");

  while (start != std::string::npos) {
    rows.emplace_back(str.substr(start + 1, end - start - 1));
    start = str.find("[", end + 1);
    end = str.find("]", end + 1);
  }

  int r = rows.size();
  int c = std::count(rows[0].begin(), rows[0].end(), ',') + 1;
  std::vector<std::vector<int>> nums(r, std::vector<int>(c, 0));
  for (int i = 0; i < r; ++i) {
    std::stringstream ss(rows[i]);
    std::string elem;
    int j = 0;
    while (getline(ss, elem, ',')) {
      nums[i][j++] = std::stoi(elem);
    }
  }

  return nums;
}

ZoYooJy avatar Aug 30 '23 03:08 ZoYooJy

第36题这种输入方式是不是有点考察的本末倒置了,[[2,3,1],[2,5,3],[4,2,1]]这种输入方式对于java来说有直接的函数可以用,但是对于c++或者其它语言来说,输入就要花费好长时间来想想怎么处理,可不可以卡尔考虑一下解决一下类似这种题目的输入问题,把问题的考察点重点放在算法而不是输入上?谢谢啦!

java什么函数能直接处理?我java写这题的时候也哼哧哼哧的写了半天,36. 网格路径和 就是力扣原题 剑指 Offer 47. 礼物的最大价值。入门dp,但把这一行字符串的输入 处理成二维数组是真麻烦。

JohnChenLiang avatar Sep 05 '23 14:09 JohnChenLiang