題目:
思路:回溯,寫一個輔助函數來實現回溯操作。以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題,望能堅持下去,儘快能找到一家更優秀的公司