假设点P处于坐标轴原点,W表示向上,A表示向左,S表示向下,D表示向右,输入一串指令代表P点的移动轨迹,输出最终P的位置
例如 "2W2D"表示向上移动两个坐标位置,再向右移动两个坐标位置
"W2D"表示向上移动一个坐标位置,再向右移动两个坐标位置
class Solution:
def GetEndPoint(self , order ):
# write code here
import re
x, y = 0, 0
order = order.upper()
for match in re.findall("\d+[A-Z]", order):
if match.endswith("W"):
y += int(match[:-1])
elif match.endswith("S"):
y -= int(match[:-1])
elif match.endswith("A"):
x -= int(match[:-1])
elif match.endswith("D"):
x += int(match[:-1])
for match in re.findall("(?<![0-9])[A-Z]+", order):
for direction in match:
if direction.endswith("W"):
y += 1
elif direction.endswith("S"):
y -= 1
elif direction.endswith("A"):
x -= 1
elif direction.endswith("D"):
x += 1
return [x, y] import java.util.*;
public class Solution {
int X=0,Y=0;
public int[] GetEndPoint (String order) {
int index = 0;
while(index<order.length()){
int start = index;
int end = start;
while(end<order.length() && Character.isDigit(order.charAt(end))){
end++;
index++;
}
int tmp;
if(start==end)tmp=1;
else{
tmp=Integer.parseInt(order.substring(start,end));
System.out.println(tmp);
}
char c1 = order.charAt(index);
if(c1=='A'||c1=='a'){
X-=tmp;
}else if(c1=='D'||c1=='d'){
X+=tmp;
}else if(c1=='S'||c1=='s'){
Y-=tmp;
}else if(c1=='W'||c1=='w'){
Y+=tmp;
}
index++;
}
return new int[]{X,Y};
}
} class Solution {
public:
struct cmp{
bool operator()(const char& a,const char& b){
return tolower(a) < tolower(b);
}
};
vector<int> GetEndPoint(string order) {
// write code here
int x = 0, y = 0;
int n = 0;
map<char,int, cmp> memo;
for(int i = 0; i < order.size(); i++){
if(order[i] >= '0' && order[i] <= '9')
n = n*10 +order[i] -'0';
else {
memo[order[i]] += n == 0 ? 1 : n;
n = 0;
}
}
return {memo['D'] - memo['A'],memo['W'] - memo['S']};
}
}; import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型一维数组
*/
public static int[] GetEndPoint (String order) {
// write code here
int[] arr1={0,0};
String[] arr2=new String[order.length()];
String str="WwSsDdAa";
List<Character> list=new ArrayList<>();
for (int i=0;i<str.length();i++){
list.add(str.charAt(i));
}
int m=0;
int n=0;
for (int i=0;i<order.length();i++){
if (list.contains(order.charAt(i))){
arr2[m]=order.substring(n,i+1);
n=i+1;
m++;
}
}
for (int i=0;i<m;i++){
if (arr2[i].charAt(0)>89){
if (arr2[i].charAt(0)=='W'||arr2[i].charAt(0)=='w'){
arr1[1]++;
}
if (arr2[i].charAt(0)=='S'||arr2[i].charAt(0)=='s'){
arr1[1]--;
}
if (arr2[i].charAt(0)=='D'||arr2[i].charAt(0)=='d'){
arr1[0]++;
}
if (arr2[i].charAt(0)=='A'||arr2[i].charAt(0)=='a'){
arr1[0]--;
}
}else{
if (arr2[i].charAt(arr2[i].length()-1)=='W'||arr2[i].charAt(arr2[i].length()-1)=='w'){
arr1[1]=arr1[1]+Integer.parseInt(arr2[i].substring(0,arr2[i].length()-1));
}
if (arr2[i].charAt(arr2[i].length()-1)=='S'||arr2[i].charAt(arr2[i].length()-1)=='s'){
arr1[1]=arr1[1]-Integer.parseInt(arr2[i].substring(0,arr2[i].length()-1));
}
if (arr2[i].charAt(arr2[i].length()-1)=='D'||arr2[i].charAt(arr2[i].length()-1)=='d'){
arr1[0]=arr1[0]+Integer.parseInt(arr2[i].substring(0,arr2[i].length()-1));
}
if (arr2[i].charAt(arr2[i].length()-1)=='A'||arr2[i].charAt(arr2[i].length()-1)=='a'){
arr1[0]=arr1[0]-Integer.parseInt(arr2[i].substring(0,arr2[i].length()-1));
}
}
}
return arr1;
}
} func GetEndPoint(order string) []int {
if len(order) == 0 {
return []int{0, 0}
}
num := 1
x, y := 0, 0
r := 0
for r < len(order) {
if order[r]-'0' <= 9 {
num = 0
for order[r]-'0' <= 9 {
num = num*10 + int(order[r]-'0')
r++
}
} else {
if order[r] == 'd' || order[r] == 'D' {
x += num
}
if order[r] == 'w' || order[r] == 'W' {
y += num
}
if order[r] == 'a' || order[r] == 'A' {
x -= num
}
if order[r] == 's' || order[r] == 'S' {
y -= num
}
r++
num = 1
}
}
return []int{x, y}
} // 解析字符串
// 数字+字母
// 字母+字母
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型一维数组
*/
private int x = 0;
private int y = 0;
public int[] GetEndPoint (String order) {
// write code here
char[] arr = order.toCharArray();
int id = 0;
while(id < arr.length) {
if (isNum(arr[id])) {
int end = id;
while (end < arr.length && isNum(arr[end])) {end++;}
move(Integer.parseInt(order.substring(id, end)), arr[end]);
id = end+1;
} else {
move(1, arr[id]);
id += 1;
}
}
return new int[] {x, y};
}
private boolean isNum(char a) {
if (a >= '0' && a <= '9') {
return true;
}
return false;
}
private void move(int step, char d) {
switch (d) {
case 'W' : y+=step;
break;
case 'A' : x-=step;
break;
case 'D' : x+=step;
break;
case 'S' : y-=step;
break;
case 'w' : y+=step;
break;
case 'a' : x-=step;
break;
case 'd' : x+=step;
break;
case 's' : y-=step;
break;
}
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型一维数组
*/
public int[] GetEndPoint (String order) {
order = order.toUpperCase();
// write code here
int[] ans = new int[]{0, 0};
int num = 0;
Character dir = null;
for(int i = 0; i < order.length(); i++){
//if(num == 0 && dir != null) continue;
if(order.charAt(i) >= '0' && order.charAt(i) <= '9'){
num = num * 10 + order.charAt(i) - '0';
}else{
if(num == 0) num = 1;
dir = order.charAt(i);
if(dir == 'W') ans[1] += num;
else if(dir == 'S') ans[1] -= num;
else if(dir == 'A') ans[0] -= num;
else ans[0] += num;
num = 0;
dir = null;
}
}
return ans;
}
} import java.util.*;
public class Solution {
public int[] GetEndPoint (String order) {
if (order == null) {
return new int[]{0, 0};
}
char[] cs = order.toCharArray();
int[] point = {0, 0};
Map<Character, int[]> map = new HashMap<Character, int[]>(){{
put('D', new int[]{1, 0});
put('S', new int[]{0, -1});
put('A', new int[]{-1, 0});
put('W', new int[]{0, 1});
put('d',new int[]{1, 0});
put('s', new int[]{0, -1});
put('a', new int[]{-1, 0});
put('w', new int[]{0, 1});
}};
int n = cs.length;
int last = 1;
for (int i = 0; i < n; i++) {
if (cs[i] >= '0' && cs[i] <= '9') {
int j = i, res = 0;
while (cs[j] >= '0' && cs[j] <= '9') {
res = res * 10 + (cs[j] - '0');
j++;
}
last = res;
i = j - 1;
} else {
int[] next = map.get(cs[i]);
point[0] += next[0] * last;
point[1] += next[1] * last;
//防止没有last的情况。
last = 1;
}
}
return point;
}
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型一维数组
* @return int* returnSize 返回数组行数
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
int* GetEndPoint(char* order, int* returnSize ) {
// write code here
int x = 0, y = 0, t = 0;
for(int i = 0; i < strlen(order); i++){
if(!(order[i] == 'W' || order[i] == 'w') && !(order[i] == 'S' || order[i] == 's') && !(order[i] == 'A' || order[i] == 'a') && !(order[i] == 'D' || order[i] == 'd')){
t += order[i]-'0';
t *= 10;
printf("i=%d t=%d\n",i,t/10);
}else{
if(t == 0)t = 10;
if(order[i] == 'W' || order[i] == 'w'){
y = y + t/10;
}else if(order[i] == 'S' || order[i] == 's'){
y = y - t/10;
}else if(order[i] == 'A' || order[i] == 'a'){
x = x - t/10;
}else{
x = x + t/10;
}
t = 0;
printf("x=%d y=%d\n", x, y);
}
}
int* res = (int*)malloc(sizeof(int)*2);
res[0] = x;
res[1] = y;
*returnSize = 2;
return res;
}
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型vector
*/
vector<int> GetEndPoint(string order) {
// write code here
vector<int> res;
bool flag = false; // 如果为true表示某动作前面有数字
int start = -1; //数字第一次出现的位置
Point p;
p.x = 0;
p.y = 0;
for(int i=0; i<order.size(); i++)
{
if(isdigit(order[i])){ //当前为数字
if(flag){ // 前面已经也是数字了
continue;
}
else{ // 第一次出现数字
start = i;
flag = true;
}
}
else{ // 当前为字母
char c = toupper(order[i]); // 小写字母转换成大写
switch(c)
{
case 'W': // 上
if(flag){
string temp = order.substr(start, i-start);
p.y += stoll(temp);
flag = false;
}
else{
p.y += 1;
}
break;
case 'S': // 下
if(flag){
string temp = order.substr(start, i-start);
p.y -= stoll(temp);
flag = false;
}
else{
p.y -= 1;
}
break;
case 'A': // 左
if(flag){
string temp = order.substr(start, i-start);
p.x -= stoll(temp);
flag = false;
}
else{
p.x -= 1;
}
break;
case 'D': // 右
if(flag){
string temp = order.substr(start, i-start);
p.x += stoll(temp);
flag = false;
}
else{
p.x += 1;
}
break;
}
}
}
res.push_back(p.x);
res.push_back(p.y);
return res;
}
}; class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型vector
*/
inline int checkdigit(char s){
if(s>='0'&&s<='9') return s-'0';
return -1;
}
vector<int> GetEndPoint(string order) {
// write code here
int stepLength = 0;
int orderLength = order.size();
int x=0, y=0;
for(char i:order){
if(checkdigit(i)<0){
if(stepLength == 0) stepLength=1;
if(i == 'W'){
y+=stepLength;
}else if(i == 'D'){
x+=stepLength;
}else if(i == 'S'){
y-=stepLength;
}else{
x-=stepLength;
}
stepLength = 0;
} else {
stepLength = stepLength*10 + checkdigit(i);
}
}
return {x,y};
}
}; import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param order string字符串
* @return int整型一维数组
*/
public int[] GetEndPoint (String order) {
char[] orders=order.toCharArray();
int A=0;
int D=0;
int W=0;
int S=0;
int[] res=new int[2];
if(orders.length==0){
return res;
}
int sum=0;
for(inti=0;i<orders.length;i++){
if(orders[i]!='A'&&orders[i]!='D'&&orders[i]!='W'&&orders[i]!='S'){
sum*=10;
sum+=(orders[i]-'0');
}else{
if(orders[i]=='A'){
if(sum==0){
A-=1;
}else{
A-=sum;
sum=0;
}
}
if(orders[i]=='D'){
if(sum==0){
D+=1;
}else{
D+=sum;
sum=0;
}
}
if(orders[i]=='W'){
if(sum==0){
W+=1;
}else{
W+=sum;
sum=0;
}
}
if(orders[i]=='S'){
if(sum==0){
S-=1;
}else{
S-=sum;
sum=0;
}
}
}//else
}//for
res[0]=numOfW+numOfS;
res[1]=numOfA+numOfD;
return res;
}
} 大佬们,我这写的为什么只过了2/10的用例啊?