如何打印给定字符串的坐标?
How to print the coordinates from the given string?
我在参加的一次面试中遇到了这个问题,我很困惑。
给定如下所示的输入字符串,我们需要打印在字符串的最后一个字符处获得的坐标。
路线:
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)
我正在尝试将其放入代码中但是,我没有得到如何破解的起点this.Any非常感谢帮助。
您可以执行以下操作。这是我会使用的算法。
- 获取输入字符串。
- 将其传递给解析器方法,其中 returns 一个标记数组。每个
token 以数字开头,以字母结尾。如果两个字母
彼此跟随,它们被认为是两个不同的令牌。 X是一个
独立令牌。
- 将标记数组传递给最终 returns 的计算方法
坐标。在计算方法中,读取返回的数组
point 2. 读取每个token后,对坐标做必要的操作。
设计坐标class就好。这将帮助您轻松解决问题。
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);
}
}
我认为问题是移动指令的识别,每个指令都有一个可选的 factor(多少步)和一个强制的 direction.省略因子时,实际表示1
.
的值
因此这些可以表示为正则表达式中的模式:
String regex = "(?<factor>\d*)"
+ "(?<dir>[LURDX])";
完成后,我们只需要将方向映射到相应的
change in co-ordinates (dx
, dy
),然后在处理移动指令时应用更改(乘以 factor 的值)在正则表达式匹配的 while 循环中。
注意X
是特例,可以通过always remember来处理
最后一个位置为 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)
输入字符串:“UUUDULR”,输出为:{0,3}
输入字符串:“ULLLDUDUURLLR”,输出为:{-2,2}
下面是上述问题的简单 C++ 实现:
void findCoordinate(string s)
{
int dx,dy;
int x=0, y=0;
for(int i=0; s[i]!='[=10=]'; 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;
}
我在参加的一次面试中遇到了这个问题,我很困惑。
给定如下所示的输入字符串,我们需要打印在字符串的最后一个字符处获得的坐标。
路线:
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)
我正在尝试将其放入代码中但是,我没有得到如何破解的起点this.Any非常感谢帮助。
您可以执行以下操作。这是我会使用的算法。
- 获取输入字符串。
- 将其传递给解析器方法,其中 returns 一个标记数组。每个 token 以数字开头,以字母结尾。如果两个字母 彼此跟随,它们被认为是两个不同的令牌。 X是一个 独立令牌。
- 将标记数组传递给最终 returns 的计算方法 坐标。在计算方法中,读取返回的数组 point 2. 读取每个token后,对坐标做必要的操作。
设计坐标class就好。这将帮助您轻松解决问题。
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);
}
}
我认为问题是移动指令的识别,每个指令都有一个可选的 factor(多少步)和一个强制的 direction.省略因子时,实际表示1
.
因此这些可以表示为正则表达式中的模式:
String regex = "(?<factor>\d*)"
+ "(?<dir>[LURDX])";
完成后,我们只需要将方向映射到相应的
change in co-ordinates (dx
, dy
),然后在处理移动指令时应用更改(乘以 factor 的值)在正则表达式匹配的 while 循环中。
注意X
是特例,可以通过always remember来处理
最后一个位置为 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)
输入字符串:“UUUDULR”,输出为:{0,3}
输入字符串:“ULLLDUDUURLLR”,输出为:{-2,2}
下面是上述问题的简单 C++ 实现:
void findCoordinate(string s)
{
int dx,dy;
int x=0, y=0;
for(int i=0; s[i]!='[=10=]'; 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;
}