文章目錄
- 1丶判定是否互為字符重排
- 2、楊輝三角
- 3丶某公司的1個面試題(字符串包含問題)
1丶判定是否互為字符重排
這個題我們用一個非常簡單的思想就能實現,我們先將字符串轉換為字符數組,然后對字符數組進行排序,然后再把排序完的字符數組轉換為字符串,比較他們是否相等。
class Solution {public boolean CheckPermutation(String s1, String s2) {char[] s1Chars = s1.toCharArray(); // 將字符串轉換成字符數組char[] s2Chars = s2.toCharArray(); // 將字符串轉換成字符數組Arrays.sort(s1Chars); // 對字符數組進行排序Arrays.sort(s2Chars); // 對字符數組進行排序String str1=new String(s1Chars);String str2=new String(s2Chars);return str1.equals(str2); // 然后再將字符數組轉換成字符串,比較是否相等;}
}
2、楊輝三角
class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> ret=new ArrayList<>();List<Integer> row= new ArrayList<>();row.add(1);ret.add(row);//第一行for (int i=1; i<numRows;i++){ //其余行List<Integer> preRow=ret.get(i-1); //前一行List<Integer> curRow= new ArrayList<>();curRow.add(1);//每一行第一個1for (int j=1;j<i;j++){//每一行中間元素的賦值int x=preRow.get(j)+preRow.get(j-1);curRow.add(x);}curRow.add(1);//每一行最后一個1ret.add(curRow);}return ret;}
}
3丶某公司的1個面試題(字符串包含問題)