【问题标题】:Counting of word repetitions in a string计算字符串中的单词重复次数
【发布时间】:2019-03-20 11:45:28
【问题描述】:

机器人可以在平面上沿 4 个方向移动:U - 上,D - 下,L - 左,R - 右。流量示例是 UUDLR、DLRUD。我必须实现“walk”方法,以便在通过指定路径后返回机器人位置。所以我这样做:

class Main {

static int[] walk(String path) {

int[] A = {0,0};
char charFromPath;

for (int i = 0 ; i < path.length() ; i++) {
      charFromPath = path.charAt(i);
      if(charFromPath == 'U')
      {                           
          A[1]++;
      }
      if(charFromPath == 'D')
      {                           
          A[1]--;
      }
      if(charFromPath == 'L')
      {                           
          A[0]--;
      }
      if(charFromPath == 'R')
      {                           
          A[0]++;
      }
  }
    return A;
}

public static void main(String[] args) {
    {
        String path="UUDLR";
        int[] position = walk(path);

        System.out.println("case 1 - path "+path+ " position 0,1");
        if(position[0]==0 && position[1]==1)
            System.out.println("the robot went right");
      else
            System.out.println("the robot smashed");
    }

    {
        String path="DLRUD";
        int[] position = walk(path);

        System.out.println("\ncase 2 - path "+path+ " position 0,-1");
        if(position[0]==0 && position[1]==-1)
            System.out.println("the robot went right");
        else
            System.out.println("the robot smashed");
    }
}}

现在在版本 2 中,根据逻辑 U、D、L、R,命令是 UP DOWN LEFT RIGHT。流量示例是 UPUPLEFTRIGHTUP。

现在在 版本 3 中,命令例如 3xUP 2xLEFT DOWN RIGHT。 3xUP 表示向上移动 3 次,2xLEFT 向左移动 2 次。为机器人添加移动限制,他不能离开 10x10 区域(项目 10.10 或 -10,-10 是最后一个有效值)。

我不知道怎么写。如何计算字符串中字符串的重复次数?

【问题讨论】:

  • 检查答案,并告诉我您是否需要更多帮助以及它是否能回答您的问题。
  • 不是很懂 :( 。这个功能怎么做?

标签: java string find counting word


【解决方案1】:

你可以试试

String str = "UPUPLEFTRIGHTUP";
int countUP = ( str.split("UP", -1).length ) - 1;
int countLEFT = ( str.split("LEFT", -1).length ) - 1;
int countRIGHT = ( str.split("RIGHT", -1).length ) - 1;
int countDOWN = ( str.split("DOWN", -1).length ) - 1;

可以通过将int 值与框的限制(在您的情况下为10*10)进行比较来验证限制。

对于位置,如果我们假设每个动作是1个单位那么:

int x = 0; //starting point in Ox axis
int y = 0; //starting point in Oy axis 
x = countRIGHT - CountLeft;
y = countUP - CountDOWN;

(x,y)这对情侣是你机器人的位置。

移动功能:

int[] walk(String path) {
   int[] position = {0,0};
   int countUP = ( path.split("UP", -1).length ) - 1; //Counts how many UP command
   int countLEFT = ( path.split("LEFT", -1).length ) - 1; //Counts how many LEFT command
   int countRIGHT = ( path.split("RIGHT", -1).length ) - 1; //Counts how many RIGHT command
   int countDOWN = ( path.split("DOWN", -1).length ) - 1; //Counts how many DOWN command
   position[0] = countRIGHT - countLEFT;
   position[1] = countUP - countDown;
   return position;
}

【讨论】:

  • 不是很懂 :( 。这个功能怎么做?
  • 什么版本? @141_MATRIX_141
  • 如果你有时间,可以使用第 2 版,但你可以使用“walk”功能吗?
  • 编辑了我的答案。你只需在你的主函数中调用它:walk("UPUPLEFTRIGHTUP");
  • 没问题 :) @141_MATRIX_141 对于第三个版本你应该使用正则表达式
【解决方案2】:

对于第 2 版,您可以拆分原始字符串并将其放入数组中,并使用您可以调用字符串 String [] tmpsplit = tmp.split(" "); 的方法。这样,您的阵列在每个单元格内都有一个方向(向上或向下或向左或向右)。

然后您可以将此数组放在与版本 1 类似的数组中。将 charFromPath == 'U' 替换为 tmpsplit[i].equals("UP")

【讨论】:

  • 你能写下去吗,因为我不明白;/
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 2011-02-07
相关资源
最近更新 更多