2011-08-17 95 views
1

您好我一直在做Javabat练习,我发现自己在一个有点暴牙的这个问题:编程练习

我们会说一个字符串是XY均衡的,如果所有的“X”字符串中的字符,在字符串后面的某处存在'y'字符。所以“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

什么是你的代码错误?任何例外?意外的结果? – Marcelo

+1

考虑转移到(http://codereview.stackexchange.com/)... – maerics

+2

让我明白这一点!如果一个'y'可以平衡多个'x's,那么字符串中单个'y'的出现是不是意味着它是xy平衡的,除非'y'后面跟着一个'x'? – gotomanners

回答

1

你的方法,因为它找到一个'x'紧接着给定的字符串在'y'尽快返回true。所以在大多数情况下,它会给您的原始问题带来不正确的结果。

我不给你完整的解决方案,只是一个提示,让你真正学会自己解决问题。基本上,您需要确定最后一次出现'x'后字符串中是否有'y'。为此,请使用String.lastIndexOf

+0

我underrstand,我不知道如何使该方法返回true,如果它发现一个x之后,没有一个位置它是什么位置。 – user846603

+0

@ user846603,您可以更清楚地说明您的问题,以:-)开头:检查我的更新以获取解决方案提示。 –

2
  1. 找到的最后x
  2. 位置找到最后一个y
  3. 返回xPos < yPos的位置。

(我会留下特殊情况下,比如如果没有x或没有y发现另一个运动;-)

1

你的逻辑是有缺陷的:你返回true(也就是你结束循环,并给予一个结果),只要你找到一个x后面跟着一个y。这不是该程序应该做的。

此外,如果字符串长度小于2,则您将字符串与==比较。这比较了引用(指针)而不是字符串的内容。使用s1.equals(s2)比较两个字符串的内容。

下面是我将如何编码算法(使用indexOf的其他解决方案可能更有效,但它们不使用循环,如果要继续使用循环,此解决方案应该可以工作)。

  • 初始化一个布尔变量balanced为true
  • 开始循环的字符串的每个字符。
  • 如果当前字符是x,则将余额设置为false。
  • 如果当前字符是y,则重置均衡为true。
  • 当循环结束时,返回平衡值。
1
public boolean xyBalance(String str) { 
    if(!str.contains("x")) { return true; } 
    int x = str.lastIndexOf("x"); 
    int y = str.lastIndexOf("y"); 
    return x < y; 
} 

从上到下: 如果字符串中的NO x,必须平衡,以便返回true。 获取x的最后一个实例。 获取y的最后一个实例。 如果最后一个x在最后一个y之前,则返回true,否则返回false。

这是我能想到的最简单最干净的方式。

1

下面就来解决这个使用的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; 
    } 
0
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; 
    } 

    } 
+0

感谢您使用此代码段,这可能会提供一些有限的即时帮助。一个[正确的解释将大大提高其长期价值](/ meta.stackexchange.com/q/114762/350567)通过显示*为什么*这是一个很好的解决方案,并会使它对未来更有用有其他类似问题的读者。请[编辑]你的答案以添加一些解释,包括你所做的假设。 – iBug