簡體   English   中英

如何解決這個數組問題

[英]How to solve this array problems

因此,我正在Java編程類中學習數組,並且得到了一個對我來說具有挑戰性的程序。 我必須編寫一個包含一系列行的輸入文件的程序,每行一個字。 我必須編寫一個程序來告訴用戶單詞是否使用不同的字母(重復的字母)。

這是我的輸入文件:

UNCOPYRIGHTABLE
FLIPPER
EXECUTABLE
UNPROFITABLE
QUESTIONABLE
WINDOW
TAMBOURINE

這就是我現在所擁有的:

Scanner df = new Scanner (new Files (distinctlet.in"));

while (df.hasNextLine())
{
    String line = df.nextLine();
    String array [] = line.split("");

    String ans = "";

    for (int k = 0; k < array.length; k++)
    {

        for (int m = k + 1; m < array.length; m++)
        {
            if (!array[k].equals(array[m])
            {
                ans = "USES DISTINCT LETTERS";
            }
            else
            {
                ans = "DOES NOT USE DISTINCT LETTERS";
            }

        }//FOR LOOP2

    }//FOR LOOP

    System.out.println(line + " " + ans);

}//WHILE DF

我的輸出應該是:

UNCOPYRIGHTABLE USES DISTINCT LETTERS
FLIPPER DOES NOT USE DISTINCT LETTERS
EXECUTABLE DOES NOT USE DISTINCT LETTERS

等等...

我現在的輸出是輸入的單詞,並且每行都顯示“請勿使用區分字母”。 我知道問題出在嵌套循環中,但我不知道如何解決。 感謝您的幫助。

解決編程問題時,我要做的第一件事是繪制/繪制/寫出sudo偽代碼和圖片。 總是這樣做。 隨着您編程技巧的成熟,它會變得更加容易。

面對這樣的問題,我首先考慮如何解決核心問題。

核心問題:檢測具有重復字符的字符串。

考慮到這一點,我的第一個想法是只遍歷每個字符串,將每個位置與先前的位置進行比較。

String input = "inputString";
for(int i = 1; i < input.length(); i++){
    for(int j = 0; j < i; j++){
        if(input[i].equalsIgnoreCase(input[j])){
            return false; //duplicate detected
        }
    }
}
return true; //No duplicates

這個答案很簡單,對於大尺寸的輸入將不起作用(太慢了!)。 回答了核心問題后,您只需要編寫其余代碼即可!

希望這可以幫助!

為了解決您可能會喜歡的這類問題,此解決方案可以非常輕松地解決您的問題,但是在這里,我們只是采用一個固定的空間,該空間為256個長度的數組, complexity will be O(n)

int []star = new int[256];
        while (df.hasNextLine())
        {
            Arrays.fill(star,0);
            String line = df.nextLine();
            for(int i=0;i<line.length();i++){
                star[line.charAt(0)]++;
            }
            for(int i=0;i<256;i++){
                if(star[i]>0 && star[i]>1){
                    System.out.println("Duplicate characters present..");
                }
            }
            System.out.println("No Duplicate characters present..");
            }

我希望你有個主意。

我個人不會使用數組來執行此操作-我會使用地圖。 即使必須使用數組,我仍然會本着地圖的精神解決此問題。

此處, counter數組的作用類似於映射(鍵=索引,值=計數)。

public class Test {

    public static void main(String[] args) throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get("data/input.csv"));
        String s = new String(encoded, Charset.defaultCharset());
        String[] split = s.split("\n");
        System.out.println("Input: " + Arrays.toString(split));
        System.out.println("Output: " + Arrays.toString(check(split)));
    }

    private static String[] check(String[] strings) {
        for (int i = 0; i < strings.length; i++)
            strings[i] += distinct(strings[i])
                    ? " USES DISTINCT LETTERS"
                    : " DOES NOT USE DISTINCT LETTERS";
        return strings;
    }

    private static boolean distinct(String string) {
        int[] counter = new int[string.length()];
        for (char c : string.toCharArray())
            if (++counter[string.indexOf(c)] > 1) return false;
        return true;
    }
}
Input: [UNCOPYRIGHTABLE, FLIPPER, EXECUTABLE, UNPROFITABLE, QUESTIONABLE, WINDOW, TAMBOURINE]
Output: [UNCOPYRIGHTABLE USES DISTINCT LETTERS, FLIPPER DOES NOT USE DISTINCT LETTERS, EXECUTABLE DOES NOT USE DISTINCT LETTERS, UNPROFITABLE USES DISTINCT LETTERS, QUESTIONABLE DOES NOT USE DISTINCT LETTERS, WINDOW DOES NOT USE DISTINCT LETTERS, TAMBOURINE USES DISTINCT LETTERS]

您可以使用字符位置來確定它是否存在於字符串的其他位置。 考慮以下解決方案:

        while (df.hasNextLine()) {
            String line = df.nextLine();
            String array[] = line.split("");
            String ans = "USES DISTINCT LETTERS";

            for (int k = 0; k < array.length; k++) {
                if(line.indexOf(array[k]) != line.lastIndexOf(array[k])){
                    ans = "DOES NOT USE DISTINCT LETTERS";
                    break;
                }

            }//FOR LOOP

            System.out.println(line + " " + ans);

        }//WHILE DF
for (int k = 0; k < array.length; k++)
{

  for (int m = k + 1; m < array.length; m++)
  {
      if (!array[k].equals(array[m])
      {
        ans = "USES DISTINCT LETTERS";
      }
      else
      {
        ans = "DOES NOT USE DISTINCT LETTERS";
        // Break out of the two loops once you know the characters are matching. 
        //Otherwise it loops again and the last match of character is what you get the ans.
      }

   }//FOR LOOP2

}//FOR LOOP

暫無
暫無

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

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