【问题标题】:runtime error for hankerrank's 'Alternating Characters'hankerrank 的“交替字符”的运行时错误
【发布时间】:2014-12-20 11:05:10
【问题描述】:

这是 hankerrank 问题“交替字符”的代码。这段代码在我的系统上很好。它清除了所有的 TESTCASE,但在 hankerrank 中,它通过运行时错误。运行时错误是

Compiler Message
Runtime Error
Error (stderr)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Solution.main(Solution.java:17) 

这是我的代码。

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.Scanner; 
public class Solution4 {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner in=new Scanner(System.in);
    Scanner tin=new Scanner(System.in);
    int tc=in.nextInt();
    String [] strA=new String[tc];
   // System.out.println("strL"+strA.length);
    for(int i=0;i<strA.length;i++){
          strA[i]=tin.nextLine();
         // System.out.print(" i= "+i+" sr = "+strA[i]);
    }
    for(int i=0;i<strA.length;i++){
         String str=strA[i];
         int k=0;
         int d=0;
         for(int j=1;j<str.length();j++){

             if(str.charAt(k)==str.charAt(j))
                  d++;
             else
                k=j; 
         }
         System.out.println(d);
    }
}

}

【问题讨论】:

  • 你尝试过的输入是什么?
  • 样本输入 5 AAAA BBBBB ABABABAB BABABA AAABBB 样本输出 3 4 0 0 4
  • 这里 5 不是。输入
  • 不,每个都在单独的行中,甚至输出也是如此

标签: java string algorithm data-structures


【解决方案1】:

如果提供的输入较少,您的代码将无法运行。尝试使用

for(int i=0;i<strA.length;i++){
      if(tin.hasNextLine())
        strA[i]=tin.nextLine();
      else
      {
        System.out.println("An error occured");
        return;
      }
    }

或者如果效果不佳,请尝试将整个代码更改为:

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.Scanner; 
public class Solution4 {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner in=new Scanner(System.in);
    Scanner tin=new Scanner(System.in);
    int tc=in.nextInt();
    String [] strA=new String[tc];

    for(int i=0;i<strA.length;i++){
      if(tin.hasNextLine()) //if input is there
        strA[i]=tin.nextLine();
    }       

    for(int i=0;i<strA.length;i++){
         int d=0;
         if(strA[i]!=null) //this will be true when the input is less
         {
           String str=strA[i];
           int k=0;
           for(int j=1;j<str.length();j++){
               if(str.charAt(k)==str.charAt(j))
                    d++;
               else
                  k=j; 
           }
         }
     System.out.println(d);
   }
}

}

【讨论】:

  • 现在我得到“发生错误”,你能解释一下,为什么会这样。我在我的系统上执行了,很好。那为什么它不在 hankerrank 上
  • Hackerrank 只给出了一些输入量,这少于所需的输入量。如果这不是您想要的消息,请检查尝试我发布的代码
  • 那我该怎么办?你是怎么发现它在输入较少的情况下不起作用的?我很想知道。
  • @sa8 ,阅读错误消息。它清楚地显示“找不到行”,这意味着 Hackerrank 提供的输入较少。没有更多输入,strA[i]=tin.nextLine(); 抛出异常。试试我的答案中的代码
  • 您的代码在我的系统上很好,但在 hankerrank 上不行。错误答案是 hankerrank 上的消息
【解决方案2】:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {

        String s;
        int t;

        Scanner in = new Scanner(System.in);
        t = in.nextInt();

        for (int f = 0; f < t; ++f) {
            s = in.next();
            getResult(s);
        }
        in.close(); 
    }

    public static void getResult(String s){
        char[] cArr;

        cArr = s.toCharArray();
        int delCount = 0;
        for(int i=0;i<cArr.length-1;i++){
            if(cArr[i]==cArr[i+1]){ 
                delCount++;
            }
        }
        System.out.println(delCount);
    }
}

【讨论】:

    【解决方案3】:

    [https://www.hackerrank.com/challenges/alternating-character][1]s

    这应该可行。

    import java.io.*;
    
    public class Solution {
        public static void main(String[] args) throws IOException{
            StringBuffer sb = new StringBuffer();
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
            //For each test case
            for(byte T = Byte.parseByte(br.readLine()); T > 0; --T){
    
                //Solve
                sb.append(getMinDeletions(br.readLine().toCharArray()) + "\n");
            }
            System.out.print(sb);
        }
    
        private static int getMinDeletions(final char[] S){
            int deletions = 0;
            for(int i = 1, N = S.length; i < N; ++i){
                if(S[i] == S[i-1]){
                    ++deletions;
                }
            }
            return deletions;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2018-09-10
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多