题目:
思路:回溯,写一个辅助函数来实现回溯操作。以python代码为例,如果当前i==length,就代表已遍历完字符串s,将子路径path加入最终res中。i记录了每次回溯的开始index。
代码:
python版:
def partition(self, s):
res=[]
length=len(s)
def helper(path,i):
if i==length and path not in res:
res.append(path)
return
for j in range(i+1, len(s)+1): # j,下一个分割位置
temp = s[i:j] # 两个分割位置确定的字符串
if temp == temp[::-1]: # 判断是否为回文
helper(path+[temp],j)
helper([],0)
return res
c++ 版:
class Solution {
public:
vector<vector<string>> res;
void helper(int i,vector<string> path, int length,string s){
if (i==s.size()) {
res.push_back(path);
return ;}
//if (i>s.size()) return;
for (int j=i+1;j<s.size()+1;j++){
string tmp=s.substr(i,j-i);
string tmp1=tmp;
//std::cout<<tmp<<" "<<j<<" "<<std::endl;
reverse(tmp1.begin(),tmp1.end());
if (tmp1==tmp){
path.push_back(tmp);
helper(j,path,length,s);
path.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
int len=s.size();
vector<string> path;
helper(0,path,len,s);
return res;
}
};
----
2020.4.5号,第一题。
博主是研三狗了,去年秋招面试算法岗cv相关,惨败...去年人工智能&机器学习相关岗位竞争还是很激烈的,博主在公司一直实习也没啥时间准备,最后正式秋招才发现竞争很激烈啊,没有一些硬实力(paper or 竞赛 or 几份实习 or 会吹的能力...) 难收获满意的offer。最后拿了一些小offer,所幸还是留在了原公司转正了...
但经过去年的面试感受到,光是会写python是不够的,大家的简历基本上都是只会写python+项目经历or实习经历,如果仅是这样感觉得会吹了hhh,毕竟面试技巧也是很重要的,最后工作找的最好的人不一定是最强的人,但善于包装的人一定很占优势。
最近疫情导致还没开学,想想去年的秋招经历,痛定思痛,楼主决定还是要重新学学c++,用c++写题。因为好久不写c++了,我一般先看python版的解答思路,再用c++写...所以写法里可能会有很多python的影子。
最后计划每天写8题,望能坚持下去,尽快能找到一家更优秀的公司