这是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);
}
}
}
答案 0 :(得分:0)
似乎如果提供的输入较少,则代码将无法正常工作。请尝试使用
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);
}
}
}
答案 1 :(得分:0)
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);
}
}
答案 2 :(得分:0)
[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;
}
}