forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphabet Board Path.cpp
37 lines (36 loc) · 1.09 KB
/
Alphabet Board Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Runtime: 2 ms (Top 60.19%) | Memory: 6.1 MB (Top 62.14%)
class Solution {
public:
string alphabetBoardPath(string target) {
string ans = "";
int prevRow = 0;
int prevCol = 0;
int curRow = 0;
int curCol = 0;
for(int i = 0; i < target.length(); i++){
prevCol = curCol;
prevRow = curRow;
curRow = (target[i] - 'a')/5;
curCol = (target[i] - 'a')%5;
if(curRow == 5 and abs(curCol - prevCol) > 0){
curRow--;
}
if(curRow - prevRow > 0){
ans += string((curRow - prevRow), 'D');
}else{
ans += string((prevRow - curRow), 'U');
}
if(curCol - prevCol > 0){
ans += string((curCol - prevCol), 'R');
}else{
ans += string((prevCol - curCol), 'L');
}
if(((target[i] - 'a')/5) == 5 and abs(curCol - prevCol) > 0){
ans += 'D';
curRow++;
}
ans += '!';
}
return ans;
}
};