Java
1. Give two String, write a method to decide if one is a permutation of the other.
for example: string s1="dog" string s2="god" is true; string s3="dgo" is also true;
public boolean isPermutation(String s1,String s2){ boolean flag=false; if(s1.length()==0 && s1.length()!=s2.length()){ flag = false; }else { String str1=new String(s1); String str2=new String(s2); char[] str1ToChar=s1.toCharArray(); char[] str2ToChar=s2.toCharArray(); Arrays.sort(str1ToChar); Arrays.sort(str2ToChar); if(Arrays.equals(str1ToChar,str2ToChar)){ flag = true; } } return flag; }
2. F(0)=0;F(1)=0; F(n)=F(n-1)+F(n-2) for n>1(递归)
public int recursion(int n){ if(n==1 || n==2){ n=1; }else{ n=recursion(n-1)+recursion(n-2); } return n; }
时间: 2024-10-23 12:07:13