【Lintcode】Restore IP Addresses, Number of Airplanes in the Sky

到了学校开始刷刷题,比如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;
    }
};

转载于:https://www.cnblogs.com/soya/p/5195277.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值