简单的Java数组字符串算法问题

huangapple go评论72阅读模式
英文:

Simple java array string algorithms question

问题

下面是您提供的代码的翻译部分:

public class CheckStrings {
	/**
	 * 如果字符串数组按字母顺序排序,则返回true,否则返回false。
	 * 
	 * @param w 要检查的字符串数组
	 * @return 如果字符串数组按字母顺序排序,则返回true,否则返回false。
	 */
	public static boolean isSorted(String[] w) {
		boolean bob = true;
		for (int i = 0; i < w.length-1; i++)
		{
			if (w[i].compareTo(w[i+1]) >= 0)
			{
				bob = true;
			}
			else
			{
				bob = false;
				i = w.length + 50;
			}
		}
		
		return bob;
	}
	
	/**
	 * 如果至少有一个字符串在w中出现了多次,则返回true,否则返回false。
	 * 
	 * @param w 要检查的字符串数组
	 * @return 如果至少有一个字符串在w中出现了多次,则返回true,否则返回false。
	 */
	public static boolean hasDuplicate(String[] w) {
		boolean bob = true;
		for (int i = 0; i < w.length-1; i++)
		{
			if (w[i].equals(w[i+1]))
			{
				bob = true;
				i = w.length + 50;
			}
			else
			{
				bob = false;
				
			}
		}
		return bob;
	}
}

如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

My two functions isSorted and hasDuplicate created below should work, right? My test class for the assignment wasn't working. The instructions for both are in the comments below. isSorted returns true if the string array is sorted alphabetically, and false - if not. hasDuplicate returns true if there's at least one set of duplicate strings and false - if not.

public class CheckStrings {
	/**
	 * Returns true if the Strings in w appear in sorted order
	 * (alphabetical order) and false otherwise.
	 * 
	 * @param w the array of Strings to check
	 * @return true if the Strings in w appear in sorted order
	 * and false otherwise.
	 */
	public static boolean isSorted(String[] w) {
		boolean bob = true;
		for (int i = 0; i &lt; w.length-1; i++)
		{
			if (w[i].compareTo(w[i+1]) &gt;= 0)
			{
				bob = true;
			}
			else
			{
				bob = false;
				i = w.length + 50;
			}
		}
		
		return bob;
	}
	
	/**
	 * Returns true if at least one String in w appears more than once
	 * and false otherwise.
	 * 
	 * @param w the array of Strings to check
	 * @return true if at least one String in w appears more than once
	 * and false otherwise.
	 */
	public static boolean hasDuplicate(String[] w) {
		boolean bob = true;
		for (int i = 0; i &lt; w.length-1; i++)
		{
			if (w[i].equals(w[i+1]))
			{
				bob = true;
				i = w.length + 50;
			}
			else
			{
				bob = false;
				
			}
		}
		return bob;
	}
}

答案1

得分: 1

递交这份作业:

public static boolean isSorted(String[] w) {
   return Arrays.stream(w).reduce("", (a, b) -> a == null || a.compareTo(b) > 0 ? null : b) != null;
}

public static boolean hasDuplicate(String[] w) {
   return Arrays.stream(w).distinct().count() == w.length;
}

还有,谁是Bob?

英文:

Submit this for your homework:

public static boolean isSorted(String[] w) {
return Arrays.stream(w).reduce(&quot;&quot;,(a, b) -&gt; a == null || a.compareTo(b) &gt; 0 ? null : b) != null;
}
public static boolean hasDuplicate(String[] w) {
return Arrays.stream(w).distinct().count() == w.length;
}

And who is Bob?

答案2

得分: 0

public final class CheckStrings {

    private CheckStrings() {
    }

    public static boolean isSorted(String... w) {
        if (w == null || w.length == 0)
            return false;

        for (int i = 0, j = w.length - 1; j < w.length; i++, j++)
            if (w[i].compareToIgnoreCase(w[j]) > 0)
                return false;

        return true;
    }

    public static boolean hasDuplicate(String... w) {
        if (w == null || w.length == 0)
            return false;

        Set<String> unique = new HashSet<>();

        for (String str : w)
            if (!unique.add(str.toLowerCase()))
                return false;

        return true;
    }
}

Output:

String[] arr = {"a", "a", "b", "b", "c", "c"};
System.out.println(CheckStrings.isSorted(arr));     // true
System.out.println(CheckStrings.hasDuplicate(arr)); // true
英文:
public final class CheckStrings {
private CheckStrings() {
}
public static boolean isSorted(String... w) {
if (w == null || w.length == 0)
return false;
for (int i = 0, j = w.length - 1; j &lt; w.length; i++, j++)
if (w[i].compareToIgnoreCase(w[j]) &gt; 0)
return false;
return true;
}
public static boolean hasDuplicate(String... w) {
if (w == null || w.length == 0)
return false;
Set&lt;String&gt; unique = new HashSet&lt;&gt;();
for (String str : w)
if (!unique.add(str.toLowerCase()))
return false;
return true;
}
}

Output:

String[] arr = {&quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;};
System.out.println(CheckStrings.isSorted(arr));     // true
System.out.println(CheckStrings.hasDuplicate(arr)); // true

答案3

得分: 0

如果您尚未被允许使用Java 8的Stream API,isSortedhasDuplicate的实现可能如下所示:

public static boolean isSorted(String[] w) {
    if (null == w || w.length == 0) {
        return false;
    }

    for (int i = 0; i < w.length-1; i++) {
        if (w[i].compareTo(w[i+1]) > 0) {  // 或者如果不区分大小写,则使用compareToIgnoreCase
            return false;
        }
    }
        
    return true;
}

public static boolean hasDuplicate(String[] w) {
    if (null == w || w.length < 2) return false;
        
    Set<String> set = new HashSet<>();
    for (int i = 0; i < w.length-1; i++) {
        String s = w[i];        // 如果需要不区分大小写的查找,使用s = w[i].toLowerCase();
        if (set.contains(s)) {
            return true;
        }
        set.add(s);
    }
    return false;
}
英文:

In case you're not allowed yet to use Java 8 Stream API, implementation of the isSorted and hasDuplicate may look as follows:

public static boolean isSorted(String[] w) {
    if (null == w || w.length == 0) {
        return false;
    }

    for (int i = 0; i &lt; w.length-1; i++) {
        if (w[i].compareTo(w[i+1]) &gt; 0) {  // or compareToIgnoreCase if case insensitive is ok
            return false;
        }
    }
        
    return true;
}

public static boolean hasDuplicate(String[] w) {
    if (null == w || w.length &lt; 2) return false;
        
    Set&lt;String&gt; set = new HashSet&lt;&gt;();
    for (int i = 0; i &lt; w.length-1; i++) {
        String s = w[i];        // if case-insensitive lookup needed use s = w[i].toLowerCase();
        if (set.contains(s)) {
            return true;
        }
        set.add(s);
    }
    return false;
}

</details>

huangapple
  • 本文由 发表于 2020年9月21日 05:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定