如何解决阵列问题

时间:2019-05-14 00:13:45

标签: java arrays

因此,我正在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

以此类推...

我现在的输出是输入的单词,并且每行上都显示“不使用区分字母”。我知道问题出在嵌套循环中,但我不知道如何解决。谢谢您的帮助。

5 个答案:

答案 0 :(得分:1)

解决编程问题时,我要做的第一件事是绘制/绘制/写出sudo 伪代码和图片。我总是 这样做。随着编程技能的成熟,它会变得更加容易。

面对这样的问题,我首先会考虑如何解决 core 问题。

核心问题:检测具有重复字符的字符串。

牢记这一点,我的第一个想法是只遍历每个字符串,检查每个位置是否与以前的位置相对。

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

这个答案很简单,对于大尺寸的输入将不起作用(太慢了!)。回答了核心问题后,您只需要编写其余代码即可!

希望这会有所帮助!

答案 1 :(得分:1)

要解决您可能会喜欢的这类问题,此解决方案可以非常轻松地解决您的问题,但是在这里,我们只占用一个固定空间,即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..");
            }

我希望你有个主意。

答案 2 :(得分:0)

我个人不会使用数组来执行此操作-我会使用地图。即使必须使用数组,我仍然会本着地图的精神解决这个问题。

此处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]

答案 3 :(得分:0)

您可以使用字符位置来确定它是否存在于字符串的其他位置。 考虑以下解决方案:

        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

答案 4 :(得分:-1)

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