Warm Up Contest

386.字典序排数

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res;
        map<string, int> hm;
        for(int i = 1; i <= n; i++) {
            hm[to_string(i)] = i;
        }
        for(auto iter = hm.begin(); iter != hm.end(); iter++) {
            res.push_back(iter->second);
        }
        return res;
    }
};

387.字符串中的第一个唯一字符

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char,int> hm;
        for(auto c : s) hm[c]++;
        for(int i = 0; i < s.size(); i++) {
            if(hm[s[i]] == 1) return i;
        }
        return -1;
    }
};

388.文件的最长绝对路径

class Solution {
    public:
    int lengthLongestPath(string input) {
        stack<int> stk;
        int res = 0;
        for(int i = 0, sum = 0; i < input.size(); ) {
            int k = 0;
            while(i < input.size() && input[i] == '\t') i++, k++;
            while(stk.size() > k) sum -= stk.top(), stk.pop();
            int j = i;
            while(j < input.size() && input[j] != '\n') j++;
            int len = j - i;
            stk.push(len), sum += len;
            if(input.substr(i, len).find('.') != -1)
                res = max(res, sum+(int)stk.size()-1);
            i = j;
        }
        return res;
    }
}
Last modification:February 20, 2022
如果觉得我的文章对你有用,请随意赞赏