簡體   English   中英

編程練習

[英]Programming exercise

嗨,我一直在做 Javabat 練習,但我發現自己遇到了這個問題:

如果對於字符串中的所有 'x' 字符,在字符串的后面某處存在一個 'y' 字符,我們會說一個字符串是 xy 平衡的。 所以“xxy”是平衡的,但“xyx”不是。 一個“y”可以平衡多個“x”。 如果給定的字符串是 xy 平衡的,則返回 true。

xyBalance("aaxbby") → true

xyBalance("aaxbb") → false

xyBalance("yaaxbb") → false

public boolean xyBalance(String str) {

  if(str.length() < 2){

    if(str == "x"){

    return false;

    }

    return true;

  }

  for (int i = 0 ; i < str.length()- 1;i++){

  if (str.charAt(i)=='x' && str.charAt(i + 1) == 'y'){

  return true;

  }

  }

  return false;
}
  1. 找到最后一個x的 position
  2. 找到最后y的position
  3. 返回xPos < yPos

(我會留下特殊情況,例如如果沒有找到x或沒有y作為另一個練習;-)

public boolean xyBalance(String str) {
    if(!str.contains("x")) { return true; }
    int x = str.lastIndexOf("x");
    int y = str.lastIndexOf("y");
    return x < y;
}

從上到下:如果字符串中沒有x,則必須平衡所以返回true。 獲取 x 的最后一個實例。 獲取 y 的最后一個實例。 如果最后一個 x 在最后一個 y 之前,則返回 true,否則返回 false。

這是我能想到的最簡單、最干凈的方法。

這是一種使用 charAt() 和迭代循環來解決此問題的方法:

   public boolean xyBalance(String str) {
      //start from the end of the string
      for (int i = str.length()-1;i>=0;i--)
      {
        if (str.charAt(i) == 'x')
        {
          //starting from the index of the last 'x', check the rest of the string to see if there is a 'y'
          for (int j = i; j < str.length(); j++)
          {
            if (str.charAt(j) == 'y')
            {
              //balanced
              return true;          
            }
          }
          //no 'y' found so not balanced
          return false; 
        }     
      }
      //no 'x' found at all so we are balanced
      return true;
    }

一旦在給定的字符串中找到一個'x'后跟一個'y' ,您的方法就會立即返回 true。 因此,在大多數情況下,它會給您的原始問題提供不正確的結果。

我沒有給你完整的解決方案,只是一個提示,讓你真正學會自己解決問題。 基本上,您需要確定在最后一次出現'x'之后字符串中是否有'y' ' 。 為此,請使用String.lastIndexOf

你的邏輯是有缺陷的:只要你找到一個 x 后跟一個 y,你就會返回 true(即結束循環並給出結果)。 這不是程序應該做的。

此外,如果字符串長度小於 2,則您將字符串與 == 進行比較。 這將比較引用(指針)而不是字符串的內容。 使用 s1.equals(s2) 比較兩個字符串的內容。

這是我將如何編寫算法(使用 indexOf 的其他解決方案可能更有效,但它們不使用循環。如果您想繼續使用循環,這個解決方案應該可以工作)。

  • 初始化一個 boolean 變量balanced為真
  • 開始循環字符串的每個字符。
  • 如果當前字符是 x,則將 balance 設置為 false。
  • 如果當前字符是 y,則將 balance 重置為 true。
  • 當循環結束時,返回balanced的值。
    public boolean xyBalance(String str) {
//intialize x and y value to 0
     int x = 0;
     int y = 0;
//run a for loop and check for x value
     for (int i = 0; i < str.length(); i++) {
      if (str.charAt(i) == 'x') {
//if condition is true increment x value
       x++;
//now run a for loop for y only if x condition is true , here it will run from "i" position where we got x value
       for (int j = i; j < str.length(); j++) {
        if (str.charAt(j) == 'y') {
//once we get value which matches 'y' increment y and break from here so that it will not count more 'y'
         y++;
         break;
        }
       }
      }
     }

//after this check x and y count  
     if (x == y) {
      return true;
     } else {
      return false;
     }

    }

我的解決方案:

public static boolean xyBalance(String str) {

      boolean xBefore = false;
      boolean yAfter = false;

      for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i)=='x') {
            xBefore = true;
            yAfter = false;
        }
        if (str.charAt(i)=='y') {
            xBefore = false;    
            yAfter = true;
        }

    }
      if (yAfter || xBefore==false) {
          return true;
      }

      return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM