到了学校开始刷刷题,比如lintcode,leetcode,这两个真蛋疼,起这种名字。
感觉面试的题目还都挺简单的,就是要又快又好的写好我还做不到。
不是为了找工作真是懒得写这些破题的。
主要是写完基本CE,还有一些细节上的小问题。
举个例子:
Lintcode:
Restore IP Addresses
我错误的程序长这样,可以先眼睛看看。
class Solution { public: /** * @param s the IP string * @return All possible valid IP addresses */ string a[4]; int sd; vector<string> ret; string tostr(int num) { stringstream ss; ss << num; string ret; ss >> ret; return ret; } void dfs(string&s,int dep,int used) { if (dep == 4) { if (used == sd) ret.push_back(a[0] + "." + a[1] + "." + a[2] + "." + a[3]); return; } int num = 0; for (int i = 0;i<3;i++) { num = num * 10 + static_cast<int>(s[used + i] - '0'); if (num > 255) break; a[dep] = tostr(num); dfs(s,dep+1,used + i + 1); if (!num) break; } } vector<string> restoreIpAddresses(string& s) { // Write your code here sd = s.length(); dfs(s,0,0); } };
思路是很简单的。
再贴上对的比较一下。
class Solution { public: /** * @param s the IP string * @return All possible valid IP addresses */ string a[4]; int sd; vector<string> ret; string tostr(int num) { stringstream ss; ss << num; string ret; ss >> ret; return ret; } void dfs(string&s,int dep,int used) { if (dep == 4) { if (used == sd) ret.push_back(a[0] + "." + a[1] + "." + a[2] + "." + a[3]); return; } int num = 0; for (int i = 0;i<3;i++) { if (used + i >= sd) break; num = num * 10 + static_cast<int>(s[used + i] - '0'); if (num > 255) break; a[dep] = tostr(num); dfs(s,dep+1,used + i + 1); if (!num) break; } } vector<string> restoreIpAddresses(string& s) { // Write your code here sd = s.length(); dfs(s,0,0); return ret; } };
两个RE问题,一个是没有返回答案,这个错我经常犯。还有一个也是,第一遍眼睛查没有查出来,就是数组越界的判断问题。明天应该会正确率有所提高。
还有一些问题,好比变量没有定义什么的,就更加经常了。
另外再贴一个我觉得写的挺有意思的代码,
Number of Airplanes in the Sky
/** * Definition of Interval: * classs Interval { * int start, end; * Interval(int start, int end) { * this->start = start; * this->end = end; * } */ class Solution { public: /** * @param intervals: An interval array * @return: Count of airplanes are in the sky. */ map<int,int> m; void insert(int loc,int x) { if (m.find(loc) != m.end()) m[loc] += x; else m.insert(make_pair(loc,x)); } int countOfAirplanes(vector<Interval> &a) { // write your code here for (auto x : a) { insert(x.start,1); insert(x.end,-1); } int c = 0; int ret = 0; for (auto x : m) { c+=x.second; ret = max(ret,c); } return ret; } };