vendredi 31 mai 2019

How to map char values to int using enum

I am trying to map some chars in a string to some integer values using enum. Please tell where am I going wrong?

enum moves{U,R,D,L};
class Solution {
public:
    bool judgeCircle(string moves) {   

// moves is a string having values like ULLDDRR, ULRD, UULLDDRR 
        int X[] = {0,1,0,-1};
        int Y[] = {1,0,-1,0};

// while iterating the string if a get 'U' , I want it to use as an index 
//with  U representing 0th index, R as index=1 and so on.. as in enum

        int x=0 , y=0;
        enum moves ind;
        for( int i = 0 ; i < moves.length() ; i++ ) {
            ind = moves[i];  // but this line here gives error
            x += X[ind];
            y += Y[ind];
        }

        if(!x && !y)
            return true;
        else
            return false;
    }
};

Aucun commentaire:

Enregistrer un commentaire