阿里
import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int result=getResult(n-1,n)+getResult(n-2,n)+getResult(n-3,n);
System.out.println(result);
}
public static int getResult(int n,int m)
{
if(n==0) return 1;
if(n==1 || n==2) return 1;
if(n==m-1)
{
return getResult(n-2,n)+getResult(n-3,n);
}
else
{
return getResult(n-1,n)+getResult(n-2,n)+getResult(n-3,n);
}
}
}
網易有道
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++) {
int min = 0, max;
int n = in.nextInt();
int k = in.nextInt();
if(n % 2 == 0) {
if(k > n / 2) {
max = n - k;
} else {
max = k - 1;
}
} else {
if(k > n / 2 + 1) {
max = n - k;
} else {
max = k - 1;
}
}
if(n < 3) {
max = 0;
}
if(max < 0) {
max = 0;
}
System.out.println(min + " " + max);
}
}
}
頭條第三次筆試
1,給定字符串輸出字符串中不重複的最長子串的長度 例如:輸入‘acddw’ 輸出3 輸入‘bbbb’ 輸出 1
def solution(one_str):
res_list=[]
length=len(one_str)
for i in range(length):
tmp=one_str[i]
for j in range(i+1, length):
if one_str[j] not in tmp:
tmp+=one_str[j]
else:
break
res_list.append(tmp)
res_list.sort(lambda x,y:cmp(len(x),len(y)))
return res_list[-1]
ss=solution(s)
print len(ss)