1496. Path Crossing | switch case | set

class Solution {
public:
    bool isPathCrossing(string path) {
        set<pair<int, int>> s;
        s.insert(make_pair(0,0));
        int x=0, y=0;
        for(auto p: path){
            switch(p){
                case 'N': x-=1; break;
                case 'S': x+=1; break;
                case 'W': y-=1; break;
                case 'E': y+=1; break;
            }
            if(s.find(make_pair(x,y))!=s.end()) return true;
            s.insert(make_pair(x,y));
        }
        return false;
    }
};
  • make_pair(x,y)可直接寫爲{x,y}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章