二、使用步骤
1.引入库
代码如下 :
import java.util.ArrayList;
import java.util.Scanner;
public class HackerRankTest1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String lineNumStr = scanner.nextLine();
String trim = lineNumStr.trim();
Integer integer = Integer.valueOf(trim);
int count=0;
StringBuilder stringBuilder = new StringBuilder();
while (true){
String nextLine = scanner.nextLine();
stringBuilder.append(nextLine);
stringBuilder.append("\n");
count++;
if (count>=integer){
break;
}
}
String toString = stringBuilder.toString();
String[] splitStrArr = toString.split("\\n");
ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
for (int i = 0; i < splitStrArr.length; i++) {
String tempStr = splitStrArr[i];
Integer deletionsFromQueStr = getDeletionsFromQueStr(tempStr);
integerArrayList.add(deletionsFromQueStr);
}
System.out.println(integerArrayList);
}
/**
* keep alternate characters
* get deletions
* string length 1 10to5
* queries count minimum 1 maximum 10
*
* @param s
* @return
*/
public static Integer getDeletionsFromQueStr(String s) {
if (s == null) {
return null;
}
if (s.isEmpty()) {
return null;
}
int retVal = 1;
for (int i = 0; i < 5; i++) {
retVal *= 10;
}
int length = s.length();
if (length < 1 || length > retVal) {
return null;
}
for (int i = 0; i < s.length(); i++) {
char charAt = s.charAt(i);
if (charAt == 'A' || charAt == 'B') {
continue;
} else {
return null;
}
}
/**
* input output output(test required)
* 5
* AAAA A 3
* BBBBB B 3
* ABABABAB ABABABAB 0
* BABABA BABABA 0
* AAABBB AB 4
*/
String strTemp=s;
int length1 = strTemp.trim().length();
int count=0;
for (int i = 0; i < length1; i++) {
char charAt = strTemp.charAt(0);
char charAt1 = strTemp.charAt(1);
if (charAt==charAt1) {
String substring = strTemp.trim().substring(i + 1);
strTemp = substring;
count++; // deletions
length1 = strTemp.length();
i=0;
}
}
return count;
}
}
2.读入数据
代码如下 :
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。