我在参加的一次采访中遇到过这个问题而且我很难过。
给定如下所示的输入字符串,我们需要打印在字符串的最后一个字符处获得的坐标。
路线:
L-Left
U-Up
R-Right
D-Down
X-Delete the previous move.
假设:
start with the coordinates (0,0)
以下是计算输出的方法。
给定输入:
3L5UR2DDX2LR
让我们一步一步地做。
3L - Move 3 points to the left of (0,0) i.e (-3,0)
5U- Move 5 points upper to (-3,0) i.e (-3,5)
R - Move 1 point to the right of (-3,5) i.e (-2,5)
2D - Move 2 points down to (-2,5) i.e (-2,3)
D - Move 1 point further down i.e (-2,2)
x - Delete the previous move.(current value is (-2,3))
2L -Move 2 left to (-2,3) i.e(-4,3)
R- Move 1 Right to (-4,3) i.e (-3,3)
最终输出为(-3,3)
我试图把它放在代码中但是,我没有得到如何打破这个的起点。任何帮助都将受到高度赞赏。
答案 0 :(得分:2)
您可以执行以下操作。这是我将使用的算法。
设计Coordinate类。这将有助于您轻松解决问题。
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int modifyX(int xDiff){
return (getX()+xDiff);
}
public int modifyY(int yDiff){
return (getY()+yDiff);
}
}
答案 1 :(得分:1)
我认为这个问题是对移动指令的识别,每个移动指令都有一个可选的因子(多少步)和一个强制方向。省略该因子时,实际上意味着1
。
因此,这些可以表示为正则表达式中的模式:
String regex = "(?<factor>\\d*)"
+ "(?<dir>[LURDX])";
一旦完成,我们只需要将方向映射到相应的方向
在坐标(dx
,dy
)中更改,然后应用更改(乘以因子的值),因为我们在while循环中处理移动指令正则表达式匹配。
请注意X
是一种特殊情况,可以通过永远记住来处理
最后一个位置为lastX
和lastY
。
以下是我的实施:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Walk {
enum Move {
L (-1, 0)
, U (0, 1)
, R (1, 0)
, D (0, -1)
, X (0, 0)
;
private int dx;
private int dy;
private Move(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getDx() {
return dx;
}
public int getDy() {
return dy;
}
}
public static void main(String[] args) {
String input = "3L5UR2DDX2LR";
String regex = "(?<factor>\\d*)"
+ "(?<dir>[LURDX])";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int x = 0;
int y = 0;
int lastX = 0;
int lastY = 0;
while (m.find()) {
String factorString = m.group("factor");
int factor;
if (factorString.length()==0) {
factor=1;
} else {
factor=Integer.parseInt(factorString);
}
String dirString = m.group("dir");
Move move = Move.valueOf(dirString);
System.out.format("(%d,%d) last was (%d, %d) %d %s -> "
, x, y
, lastX, lastY
, factor, move.name());
if (move==Move.X) {
x = lastX;
y = lastY;
} else {
lastX = x;
lastY = y;
x += factor * move.getDx();
y += factor * move.getDy();
}
System.out.format("(%d,%d)%n", x, y);
}
System.out.format("finally arrive at (%d,%d)%n", x, y);
}
}
这个程序的输出是:
(0,0) last was (0, 0) 3 L -> (-3,0)
(-3,0) last was (0, 0) 5 U -> (-3,5)
(-3,5) last was (-3, 0) 1 R -> (-2,5)
(-2,5) last was (-3, 5) 2 D -> (-2,3)
(-2,3) last was (-2, 5) 1 D -> (-2,2)
(-2,2) last was (-2, 3) 1 X -> (-2,3)
(-2,3) last was (-2, 3) 2 L -> (-4,3)
(-4,3) last was (-2, 3) 1 R -> (-3,3)
finally arrive at (-3,3)
答案 2 :(得分:0)
输入字符串:“ UUUDULR”,输出为:{0,3}
输入字符串:“ ULLLDUDUURLRLR”,输出为:{-2,2}
下面是上述问题的简单C ++实现:
void findCoordinate(string s)
{
int dx,dy;
int x=0, y=0;
for(int i=0; s[i]!='\0'; i++)
{
if(s[i]=='U')
{
dx=0;
dy=1;
}
else if(s[i]=='D')
{
dx=0;
dy=-1;
}
else if(s[i]=='L')
{
dx=-1;
dy=0;
}
else if(s[i]=='R')
{
dx=1;
dy=0;
}
x+=dx;
y+=dy;
}
cout<<"Coordinates are:"<<x<<" "<<y;
}