2022-5-9 每日一题-leetcode

题目链接:https://leetcode.cn/problems/di-string-match/

个人题解:贪心算法,每次把最小和最大的排在一起,用双指针来进行移动即可

代码:

class Solution {
public:
    vector<int> diStringMatch(string s) {
        vector<int> res;
        int l=0,r=s.size();
        for(int i=0;i<s.length();i++){
            if(s[i]=='I') res.push_back(l++);
            else res.push_back(r--); 
        }
        res.push_back(l);
        return res;
    }
};

运行截图:

image

posted @ 2022-05-09 14:02  黑VS白-清墨  阅读(22)  评论(0)    收藏  举报