In Leetcode my code is not getting accepted as it is giving wrong output but in vs code my code is working perfectly and giving the right answer.I can’t seem to find what is wrong.
For Example-INPUT——commands=[“RIGHT”,”DOWN”],n=2
My output in vs code is 3 (right answer).
In Leetcode it is showing 2(Wrong).
Problem-https://leetcode.com/problems/snake-in-matrix/description/
My Java code—
class Solution {
public int finalPositionOfSnake(int n, List<String> commands) {
int row=0;
int col=0;
for(int i=0;i<commands.size();i++){
if(commands.get(i)=="UP"){
row-=1;
}
else if(commands.get(i)=="DOWN"){
row+=1;
}
else if(commands.get(i)=="LEFT"){
col-=1;
}
else{
col+=1;
}
}
return (row * n) + col;
}
}
Was expecting the right answer.
New contributor
Gaurav Rawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.