失效链接处理 |
keep公司Java笔试面试 PDF 下载
本站整理下载:
相关截图:
主要内容:
第一题 AC 100%
String[] s1 = str1.substring(1, str1.length() - 1).split(",");
String[] s2 = str2.substring(1, str2.length() - 1).split(",");
如果输入语句如同上面处理,只能 AC60%,报错数组越界
改正,
想法一:发现数组越界,想到可能字符串的长度可能很长,想着将输入的字符串分割后用
arraylist 来存储。
嗯忙活了很久,发现行不通
想法二:猜测可能输入的字符串中间有空格。
添加 replace(" ","") 替换到输入的字符串中的空格,AC100%
String[] s1 = str1.substring(1, str1.length() - 1).replace(" ", "").split(",");
String[] s2 = str2.substring(1, str2.length() - 1).replace(" ", "").split(",");
package middleLinkCode;
import java.util.Scanner;
public class keep1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
String[] s1 = str1.substring(1, str1.length() - 1).replace(" ", "").split(",");
String[] s2 = str2.substring(1, str2.length() - 1).replace(" ", "").split(",");
int[] arr1 = new int[s1.length];
int[] arr2 = new int[s2.length];
for (int i = 0; i < s1.length; i++)
arr1[i] = Integer.valueOf(s1[i]);
for (int i = 0; i < s2.length; i++)
arr2[i] = Integer.valueOf(s2[i]);
System.out.println(cal(arr1, arr2));
sc.close();
}
private static int cal(int[] water, int[] cost) {
// TODO Auto-generated method stub
if (water == null || water.length == 0 || cost == null || cost.length == 0)
return -1;
int sum = 0, total = 0, temp = 0;
for (int i = 0; i < water.length; i++) {
|