leetcode 1859.将句子排序

⭐️ 题目描述

leetcode 1859.将句子排序_第1张图片


leetcode链接:将句子排序

代码:

class Solution {
public:
    string sortSentence(string s) {
        vector<string> record;
        record.resize(9);
        string curString;
        for (auto val : s) {
            if (isdigit(val)) {
                record[ val - '0' - 1 ] = curString;
                curString.clear();
            } else if (isalpha(val)) {
                curString.push_back(val);
            }
        }

        string ans = record[0];
        for (int i = 1; i < 9; i++) {
            if (!record[i].empty()) {
                ans += " " + record[i];
            }
        }

        return ans;
    }
};

你可能感兴趣的:(刷题,leetcode,刷题,学习)