这就和上学时遇到的问题是一模一样的,现在呢,项目中要求在短时间内快速得到有效信息,需要记录下 如何从以往低效的学习方式快速转变。
还是要多读书才可以。
这道题目大部分摘抄 https://zxi.mytechroad.com/blog/leetcode/leetcode-241-different-ways-to-add-parentheses/ d的题解。
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.
Example 1:
Input: "2-1-1".
1
2
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]
Example 2:
Input: "2*3-4*5"
1
2
3
4
5
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Output: [-34, -14, -10, -10, 10]
其实这道题目就是不停的参数,然后把目前所得的值存入一个存储中。
“2-1-1”为例:
“23-45”为例: 同样例那样就是。
namespace vimer { // 首次使用 namespace
int add(int a, int b) { return a + b; }
int minus(int a, int b) { return a - b; }
int multi(int a, int b) { return a * b; }
} // First time use namespace in c++
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
return ways(input);
}
private:
const vector<int>& ways(const string& input){
if(m_.count(input)) return m_[input]; // m_.find() is ok also but...
// Array for answer of input
vector<int> ans;
// Split every oper
for(int i = 0; i < input.length();i++){
char op = input[i];
if (op == '+' || op == '-' || op == '*'){
// Get left/right sub expression
const string left = input.substr(0,i);
const string right = input.substr(i+1);
// Get value left and right sub-expression
const vector<int>& l = ways(left); // 计划递归
const vector<int>& r = ways(right);
std::function<int(int, int)> f;
switch(op){
case '+': f = vimer::add; break;
case '-': f = vimer::minus; break;
case '*': f = vimer::multi; break; // 如此调用 回调函数
}
for (int a : l){
for(int b : r){
ans.push_back(f(a,b));
}
}
}
}
if(ans.empty())
ans.push_back(std::stoi(input));
m_[input].swap(ans);
return m_[input];
}
unordered_map<string, vector<int>> m_;
};
c++的新知识:
unordered_map中的find和count是等价的,但是呢, find 的用法更强调结果,比如发现一个值然后使用它;而count只是查找,查到后返回true就罢了。
swap就是交换两个map的键值对,就是暂时还没有发现为啥这么干。
std::function是一个通用的 多态函数封装函数,它可以 store, copy 和 invkoe 任何 copy构造 target.
因为在工作中,尤其在目前的工作中,经常是以周为单位进行工作总结和规划,那么, 在一个具体的项目工作中会碰上很多的问题,如果这些问题经常花一定的时间去写blog,感觉时间上不是特别允许。所以,这个category应该是总结,而不是plan。但是已经有了这个title,我也懒得再次进行修改,一直用下去得了。
这次在Raspberry安装CHIP对我而言是一个大考,难在了代理这块的设置上。 最终的解决方案是: 服务端使用的squid软件,然后,在树莓派不是使用终端定义http_proxy的方式,而是使用 raspi-config 配置工具进行总体配置。ok,成功解决问题。
看来有一个自己的服务器还是非常关键的。
善用vscode Task扩展。
过去的知识可能会变得过时,但是基础性的原理永远不会变,这也就是掌握基础的原因之一。
这周的任务是:
发现还是喜欢编代码,哈哈 , 挺好的目前,看看情况怎么发展吧,还真得修炼内功。