第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何解決這個(gè)陣列問題

如何解決這個(gè)陣列問題

慕斯王 2019-04-19 18:15:41
所以我在java編程課中學(xué)習(xí)數(shù)組,并且我得到了一個(gè)對(duì)我來說非常具有挑戰(zhàn)性的程序。我必須編寫一個(gè)程序,其中包含一系列行的輸入文件,每行一個(gè)單詞。我必須編寫一個(gè)程序,告訴用戶該單詞是否使用不同的字母(重復(fù)的字母)。這是我的輸入文件:UNCOPYRIGHTABLE FLIPPER EXECUTABLE UNPROFITABLE QUESTIONABLE WINDOW TAMBOURINE這就是我現(xiàn)在所擁有的: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 LOOPSystem.out.println(line + " " + ans);}//WHILE DF我的輸出應(yīng)該是:UNCOPYRIGHTABLE USES DISTINCT LETTERS FLIPPER DOES NOT USE DISTINCT LETTERS EXECUTABLE DOES NOT USE DISTINCT LETTERS等等...我現(xiàn)在的輸出是每一行的輸入和“不要使用獨(dú)特的字母”。我知道問題是在嵌套循環(huán)中,但我不知道如何解決這個(gè)問題。感謝您的幫助。
查看完整描述

5 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

        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


查看完整回答
反對(duì) 回復(fù) 2019-05-15
?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

要解決這類問題,這個(gè)解決方案可以很容易地解決你的問題,但這里我們只是采用一個(gè)256長度數(shù)組的常量空間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..");
            }

我希望你有個(gè)主意..


查看完整回答
反對(duì) 回復(fù) 2019-05-15
?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

就個(gè)人而言,我不會(huì)使用數(shù)組來做到這一點(diǎn) - 我會(huì)使用地圖。即使你必須使用數(shù)組,我仍然會(huì)以地圖的精神解決這個(gè)問題。

這里的counter數(shù)組就像一個(gè)map(key = index,value = count)。

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]


查看完整回答
反對(duì) 回復(fù) 2019-05-15
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2019-05-15
  • 5 回答
  • 0 關(guān)注
  • 507 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)