招行信用卡笔试只做出来第一道题,分享思路
招行信用卡第一题AC思路,主要就是基于取余的思想,使用memo数组保存R到右边最近L的距离或L到最近R的距离
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
String s = in.nextLine();
int n = s.length();
String[] ss = new String[s.length()];
for(int i=0;i<=n-1;i++) {
ss[i] = s.charAt(i)+"";
}
test(ss,n);
}
}
private static void test(String[] ss,int n) {
String s = "";
//String s1 = "";
int[] memo = new int[n];
int[] ret = new int[n];
for(int i=0;i<=n-1;i++) {
if(ss[i].equals("R")) {
int c = 1;
while(ss[i+c].equals("L")==false)
c++;
memo[i] = c;
}
if(ss[i].equals("L")) {
int c = 1;
while(ss[i-c].equals("R")==false)
c++;
memo[i] = c;
}
}
/*for(int i=0;i<=n-1;i++) {
s1 = s1+memo[i]+" ";
}
System.out.println(s1.trim());*/
for(int i=0;i<=n-1;i++) {
if(ss[i].equals("R")&&memo[i]%2==0)
ret[i+memo[i]] = ret[i+memo[i]]+1;
if(ss[i].equals("R")&&memo[i]%2==1)
ret[i+memo[i]-1] = ret[i+memo[i]-1]+1;
if(ss[i].equals("L")&&memo[i]%2==0)
ret[i-memo[i]] = ret[i-memo[i]]+1;
if(ss[i].equals("L")&&memo[i]%2==1)
ret[i-memo[i]+1] = ret[i-memo[i]+1]+1;
}
for(int i=0;i<=n-1;i++) {
s = s+ret[i]+" ";
}
System.out.println(s.trim());
}
} 