剑指offer-上
1. 二维数组中查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
//思路:从左下角/右上角开始遍历,每走一步就可以剔除一行或者一列。
//复杂度:O(m+n) public boolean Find(int target, int [][] array) { int rowCount = array.length; int colCount = array[0].length; int i,j; for(i=rowCount-1,j=0;i>=0&&j<colCount;) { if(target == array[i][j]) return true; if(target < array[i][j]){ i--; continue; } if(target > array[i][j]){ j++; continue; } } return false; }
2. 替换函数
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
/*问题1:替换字符串,是在原来的字符串上做替换,还是新开辟一个字符串做替换! 问题2:在当前字符串替换,怎么替换才更有效率(不考虑java里现有的replace方法) 从前往后替换,后面的字符要不断往后移动,要多次移动,所以效率低下 从后往前,先计算需要多少空间,然后从后往前移动,则每个字符只为移动一次,这样效率更高一点。*/ public class Solution { public String replaceSpace(StringBuffer str) { int spacenum=0; for(int i=0;i<str.length();i++) if(str.charAt(i)==' ') spacenum++; int indexHold=str.length()-1; int newlength=str.length()+spacenum*2; int indexNew = newlength -1; str.setLength(newlength); for(;indexHold>=0;indexHold--){ if(str.charAt(indexHold)==' '){ str.setCharAt(indexNew--,'0'); str.setCharAt(indexNew--,'2'); str.setCharAt(indexNew--,'%'); } else str.setCharAt(indexNew--,str.charAt(indexHold)); } return str.toString(); } }
3. 反向遍历链表
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.*; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { Stack<Integer> result = new Stack<Integer>(); ArrayList<Integer> list = new ArrayList<Integer>(); while(listNode!=null) { result.push(listNode.val); listNode=listNode.next; } int value=0; while(!result.isEmpty()){ value=result.pop(); list.add(value); } return list; } }
4. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode reConstructBinaryTree(int [] pre,int [] in){ TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1); return root; } //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6} //先左后右,按照前序遍历的顺序 private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre, int [] in, int startIn, int endIn){ if(startPre>endPre||startIn>endIn) return null; TreeNode root=new TreeNode(pre[startPre]); for(int i=startIn;i<=endIn;i++) if(in[i]==pre[startPre]){//前序遍历是以子树根节点顺序进行的 root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn, in,startIn,i-1);//左子树 root.right=reConstructBinaryTree(pre,startPre+i-startIn+1,endPre, in,i+1,endIn);//右子树 break; } return root; } }
5. 两个栈实现队列
import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); //stack1实现入队列,stack2实现出队列 public void push(int node) { stack1.push(node); } public int pop() { if(stack1.isEmpty()&&stack2.isEmpty()) throw new RuntimeException("Queue is empty!"); if(stack2.isEmpty()) while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } return stack2.pop(); } }
6. 旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
public int minNumberInRotateArray(int [] array) {
if(array.length==0) return 0;
int low=0,high=array.length-1,mid=0;
while(array[low]>=array[high]){//不满足这个说明是从小到大顺序,根本没变
if(high-low==1){
mid=high;
break;
}
mid = low + (high - low) / 2;
if(array[mid]>=array[low]) low=mid;
else high=mid;
}
return array[mid];
}
7. 斐波那契数列
public int Fibonacci(int n) { if(n==0) return 0; if(n==1||n==2) return 1; return Fibonacci(n-1)+Fibonacci(n-2); }
public int Fibonacci(int n) {//动态规划
int f = 0, g = 1;
while(n-->0){
g += f;//前一项
f = g - f;//前二项
}
return f;
}
8. (DP)跳台阶(1或2)
public int JumpFloor(int target) { int f = 1, g = 1; while(target-->0){ g += f; f = g - f; } return f;//主要注意返回的是前面一阶而不是现在阶的数目 }
9. (贪心)变态跳台阶(1,2...n)
public int JumpFloorII(int target) { if(target<=0) return 0; if(target==1) return 1; int a=1,b=1; while(target-->0){ a=b; b*=2; }//经过变化,b是当前跳法,a是前一阶跳法 return a;//平台不算台阶 }
10.(递归)矩形覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
public int RectCover(int target) {
if(target<=0) return 0;
if(target==1) return 1;
if(target==2) return 2;
return RectCover(target-1)+RectCover(target-2);//横着竖着
}
11.(反码补码)二进制1的个数
/* 分析一下代码:这段小小的代码,很是巧妙。 如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0, 原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。 举个例子:一个二进制数1100,从右边数起第三位是处于最右边的一个1。 减去1后,第三位变成0,它后面的两位0变成了1,而前面的1保持不变,因此得到的结果是1011. 我们发现减1的结果是把最右边的一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算, 从原来整数最右边一个1那一位开始所有位都会变成0。如1100&1011=1000. 也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0. 那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。 */ public class Solution { public int NumberOf1(int n) { int count=0; while(n!=0){ n=n & (n-1); count++; } return count; } }
12. 数值的整数次方
public double Power(double base, int exponent) {
if(base==0) return 0;
if(base==1) return 1;
if(exponent==0) return 1;
if(exponent==1) return base;
double temp = base;
if(exponent>0){
for(;exponent>1;exponent--){
base = base * temp;
}
}
else {
for(;exponent<=0;exponent++){
base = base / temp;
}
}
return base;
}//注意幂次是负数的情况
13. 调整数组次序,奇数在前,偶数在后
1.插入我觉得太蠢了,o(n**2),归并o(n*logn)
2.空间换时间,o(n)
public void reOrderArray(int [] array) { if (array != null){ int[] even = new int[array.length]; int indexOdd = 0; int indexEven = 0; for (int num : array){ if ((num & 1) == 1){ array[indexOdd++] = num; } else{ even[indexEven++] = num; } } for (int i = 0; i < indexEven; i++){ array[indexOdd + i] = even[i]; } } }
14. 链表第k个元素
public ListNode FindKthToTail(ListNode head,int k) {//需要空间少
ListNode pre=null,p=null;
p=head; pre=head; //两个指针都指向头结点
int a=k; //记录k
int count=0; //记录节点的个数
//p指针先跑,并且记录节点数,当p指针跑了k-1个节点后,pre指针开始跑,
//当p指针跑到最后时,pre所指指针就是倒数第k个节点
while(p!=null){
p=p.next;
count++;
if(k<1){
pre=pre.next;
}
k--;
} //如果节点个数小于所求的倒数第k个节点,则返回空
if(count<a) return null;
return pre;
}
15. 反转链表
public ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode pre = null, next = null; while(head!=null){ next=head.next;//防止丢失下个节点 head.next=pre; pre=head; head=next; } return pre; }
16. 合并链表(单调顺序不变)
public ListNode Merge(ListNode list1,ListNode list2) { //新建一个头节点,用来存合并的链表。 ListNode head=new ListNode(-1); head.next=null; ListNode root=head; while(list1!=null&&list2!=null){ if(list1.val<list2.val){ head.next=list1; head=list1; list1=list1.next; } else{ head.next=list2; head=list2; list2=list2.next; } } //把未结束的链表连接到合并后的链表尾部 if(list1!=null){ head.next=list1; } if(list2!=null){ head.next=list2; } return root.next; }
17. 树的子结构(空树非任何树子结构)
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { //两个if不能颠倒顺序,否则即使root2完成遍历,也会因为root1而返回false private boolean doesTree1HasTree2(TreeNode root1,TreeNode root2){ if(root2==null) return true; if(root1==null) return false; if(root1.val!=root2.val) return false; return doesTree1HasTree2(root1.left,root2.left)&& doesTree1HasTree2(root1.right,root2.right); } public boolean HasSubtree(TreeNode root1,TreeNode root2) { if(root2==null||root1==null) return false; boolean result=false; if(root1.val==root2.val){ result = doesTree1HasTree2(root1,root2); } if(result==false) result=HasSubtree(root1.left,root2);//不一定判断节点本身的值相同, if(result==false) result=HasSubtree(root1.right,root2);//子树中包含子结构就可以 return result; } }
18. 镜像翻转二叉树
public void Mirror(TreeNode root) {
if(root==null) return ;//判断null的情况,注意返回值
TreeNode temp = null;
if(root!=null){//当前节点存在就进行翻转
temp=root.left;
root.left=root.right;
root.right=temp;
}
if(root.left!=null) Mirror(root.left);
if(root.right!=null) Mirror(root.right);
}
19. 顺时针由外向内打印矩阵
本题最重要的是确定打印圈数,以及何时不是打印一整圈而是打印一行或一列,每打一圈,长度减2,注意转角处的数字不要打印重复
import java.util.ArrayList; public class Solution { public ArrayList<Integer> printMatrix(int [][] matrix) { if(matrix==null) return null; ArrayList<Integer> ans = new ArrayList<Integer>(); int rows = matrix.length; int cols = matrix[0].length; int circle = (rows/2+rows%2)<(cols/2+cols%2)?(rows/2+rows%2):(cols/2+cols%2);//确定圈数 for(int i=0;i<circle;i++){ if(rows%2==0||cols%2==0){//所有圈都是完整打印的 for(int j=i;j<cols-i-1;j++) ans.add(matrix[i][j]);//注意起止,转角处不要重复打印 for(int j=i;j<rows-i-1;j++) ans.add(matrix[j][cols-i-1]); for(int j=cols-i-1;j>i;j--) ans.add(matrix[rows-i-1][j]); for(int j=rows-i-1;j>i;j--) ans.add(matrix[j][i]); } else if(i!=circle-1){//除最后一行或一列,其余完整打印 for(int j=i;j<cols-i-1;j++) ans.add(matrix[i][j]); for(int j=i;j<rows-i-1;j++) ans.add(matrix[j][cols-i-1]); for(int j=cols-i-1;j>i;j--) ans.add(matrix[rows-i-1][j]); for(int j=rows-i-1;j>i;j--) ans.add(matrix[j][i]); } else if(rows%2==1&&rows<cols) for(int j=i;j<cols-i;j++) ans.add(matrix[i][j]);
//打印最后一行 else if(cols%2==1&&rows>=cols) for(int j=i;j<rows-i;j++) ans.add(matrix[j][cols-i-1]);
//打印最后一列 } return ans; } }
20. 实现栈
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
采用ArrayList作为数据域,且不使用辅助栈,即同样使用ArrayList存储min,
import java.util.ArrayList; public class Solution { private ArrayList<Integer> dataList = new ArrayList<>(); private ArrayList<Integer> minList = new ArrayList<>(); private Integer min = Integer.MAX_VALUE; public void push(int node) { dataList.add(node); if (node <= min) { minList.add(node); min = node; } else { minList.add(min); } } public int getSize() { return dataList.size(); } public void pop() { int end = getSize() - 1; dataList.remove(end); minList.remove(end); min = minList.get(getSize() - 1); } public int top() { return dataList.get(getSize() - 1); } public int min() { return min; } }
21. 栈的压入/弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
public boolean IsPopOrder(int [] pushA,int [] popA) { Stack<Integer> stack = new Stack<Integer>(); if(pushA.length==0) return true; if(pushA.length==1) return pushA[0]==popA[0]; for(int i=0,j=0;i<pushA.length;i++){ stack.push(pushA[i]); //原则先进后出,所以后进入的元素一定最先出来 while(j < popA.length && stack.peek() == popA[j]){ stack.pop(); j++; } } return stack.isEmpty(); }
22. 逐层遍历二叉树
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<Integer> ans = new ArrayList<Integer>(); ArrayList<TreeNode> order = new ArrayList<TreeNode>(); if(root!=null){ order.add(root); for(int i=0;i<order.size();i++){ if(order.get(i).left!=null) order.add(order.get(i).left); if(order.get(i).right!=null) order.add(order.get(i).right); } for(TreeNode n:order){ ans.add(n.val); } } return ans; }
23. 判断数组是不是某二叉搜索树的后序遍历的结果
/* BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x(也就是根), 如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x, 且这两段(子树)都是合法的后序序列。完美的递归定义 */ public class Solution { private boolean judge(int[] a,int start,int end){ if(start >= end) return true; int i = start; while(a[i] < a[end]) ++i; for(int j=i;j<end;j++) if(a[j] < a[end]) return false; return judge(a,start,i-1) && judge(a,i,end-1); } public boolean VerifySquenceOfBST(int [] sequence) { if(sequence.length==0) return false; if(sequence.length==1) return true; return judge(sequence,0,sequence.length-1); } }
24. 二叉树中结点值的和为输入整数的所有路径
public class Solution { private ArrayList<ArrayList<Integer>> listAll = new ArrayList<ArrayList<Integer>>(); private ArrayList<Integer> list = new ArrayList<Integer>(); public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { if(root == null) return listAll; list.add(root.val); target -= root.val; if(target == 0 && root.left == null && root.right == null)//满足要求并且到了叶子节点去 listAll.add(new ArrayList<Integer>(list)); FindPath(root.left, target); FindPath(root.right, target); list.remove(list.size()-1); return listAll; } }
25. 复杂链表的复制(递归,注意判断头尾的null)
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
/* public class RandomListNode { int label; RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) { this.label = label; } } */ public class Solution { public RandomListNode Clone(RandomListNode pHead) { if(pHead==null) return null; RandomListNode head = new RandomListNode(pHead.label); RandomListNode rand = null; if(pHead.random!=null ) rand = new RandomListNode(pHead.random.label); head.random = rand; if(pHead.next!=null) head.next=Clone(pHead.next); else head.next=null; return head; } }
26. 二叉搜索树转为双向链表
public class Solution { private TreeNode leftLast = null; public TreeNode Convert(TreeNode root) { if(root==null) return null; if(root.left==null&&root.right==null){ leftLast = root;// 最后的一个节点可能为最右侧的叶节点 return root; } // 1.将左子树构造成双链表,并返回链表头节点 TreeNode left = Convert(root.left); // 3.如果左子树链表不为空的话,将当前root追加到左子树链表 if(left!=null){ leftLast.right = root; root.left = leftLast; } leftLast = root;// 当根节点只含左子树时,则该根节点为最后一个节点 // 4.将右子树构造成双链表,并返回链表头节点 TreeNode right = Convert(root.right); // 5.如果右子树链表不为空的话,将该链表追加到root节点之后 if(right!=null){ right.left = root; root.right = right; } return left!=null?left:root; } }
27. 字符串的排列
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
递归的出口,就是只剩一个字符的时候,递归的循环过程,就是从每个子串的第二个字符开始依次与第一个字符交换,然后继续处理子串。
因为if-else而浪费很多时间
/** * 1、递归算法 * * 解析:http://www.cnblogs.com/cxjchen/p/3932949.html (感谢该文作者!) 对于无重复值的情况,固定第一个字符,递归取得首位后面的各种字符串组合;再把第一个字符与后面每一个字符交换, 并同样递归获得首位后面的字符串组合; 递归的出口,就是只剩一个字符的时候, 递归的循环过程,就是从每个子串的第二个字符开始依次与第一个字符交换,然后继续处理子串。 假如有重复值呢?由于全排列就是从第一个数字起,每个数分别与它后面的数字交换, 我们先尝试加个这样的判断——如果一个数与后面的数字相同那么这两个数就不交换了。 例如abb,第一个数与后面两个数交换得bab,bba。然后abb中第二个数和第三个数相同,就不用交换了。 但是对bab,第二个数和第三个数不 同,则需要交换,得到bba。 由于这里的bba和开始第一个数与第三个数交换的结果相同了,因此这个方法不行。 换种思维,对abb,第一个数a与第二个数b交换得到bab,然后考虑第一个数与第三个数交换, 此时由于第三个数等于第二个数,所以第一个数就不再用与第三个数交换了。 再考虑bab,它的第二个数与第三个数交换可以解决bba。此时全排列生成完毕!*/ import java.util.*; public class Solution { private ArrayList<String> ret = new ArrayList<String>(); private String swap(String str, int i, int j){ int n = str.length(); char[] chars = new char[n]; for(int k=0;k<n;++k){ if(k==i) chars[k]=str.charAt(j); else if(k==j) chars[k]=str.charAt(i); else chars[k]=str.charAt(k); } return String.valueOf(chars); } private void dfs(String str, int s){ int n = str.length(); if(s==n){ ret.add(str); return ; } for(int i=s;i<n;++i){ if(i!=s && str.charAt(s)==str.charAt(i)) continue; str = swap(str,s,i); dfs(str,s+1); str = swap(str,s,i); } return ; } public ArrayList<String> Permutation(String str) { if(!str.equals("")) dfs(str,0); Collections.sort(ret); return ret; } }
28. 数组中出现超过一半的数字(打擂算法)
只连续出现一次的被替换为下一个
public int MoreThanHalfNum_Solution(int [] array) { int cnt=0, x=array[0] , n=array.length; for(int i=1; i<n; i++){ if(cnt==0 || array[i]==x) { x=array[i]; cnt++; } else cnt--; } cnt=0;//注意清空 for(int i=0; i<n; i++) if(array[i]==x) cnt++; return (cnt*2>n)?x:0; }
29. 最小的k个数
//冒泡排序k次即可,时间复杂度为O(n*k) public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> al = new ArrayList<Integer>(); if (k > input.length) return al; for (int i = 0; i < k; i++) { for (int j = 0; j < input.length - i - 1; j++) { if (input[j] < input[j + 1]) { int temp = input[j]; input[j] = input[j + 1]; input[j + 1] = temp; } } al.add(input[input.length - i - 1]); } return al; } //基于堆排序算法,构建最大堆。时间复杂度为O(nlogk) import java.util.*; public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> list=new ArrayList<Integer>(); //检查输入的特殊情况 if(input==null || input.length<=0 || input.length<k) return list; //构建最大堆 for(int len=k/2-1; len>=0; len--) adjustMaxHeapSort(input,len,k-1); //从第k个元素开始分别与最大堆的最大值做比较,如果比最大值小,则替换并调整堆。 //最终堆里的就是最小的K个数。 int tmp; for(int i=k; i<input.length; i++){ if(input[i]<input[0]){ tmp=input[0]; input[0]=input[i]; input[i]=tmp; adjustMaxHeapSort(input,0,k-1); } } for(int j=0; j<k; j++) list.add(input[j]); return list; } public void adjustMaxHeapSort(int[] input, int pos, int length){ int temp; int child; for(temp=input[pos]; 2*pos+1<=length; pos=child){ child=2*pos+1; if(child<length && input[child]<input[child+1]) child++; if(input[child]>temp) input[pos]=input[child]; else break; } input[pos]=temp; } }
30. 连续子数组的最大和
例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和
public int FindGreatestSumOfSubArray(int[] array) { int res = array[0]; //记录当前所有子数组的和的最大值 int max=array[0]; //包含array[i]的连续数组最大值 for (int i = 1; i < array.length; i++) { max=Math.max(max+array[i], array[i]); res=Math.max(max, res); } return res; }
31. 1的个数
求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)
public int NumberOf1Between1AndN_Solution(int n) { int ones = 0; for (int m = 1; m <= n; m *= 10) { int a = n/m, b = n%m; ones += (a + 8) / 10 * m + ((a % 10 == 1) ? 1:0) * (b + 1); }//a+8是分析当前位在0,1时,0没有,1的话前面-1,后面最后不是m种而是b+1种 return ones; }
32. 把数组转为最小数
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323
/* 解题思路: * 先将整型数组转换成String数组,然后将String数组排序,最后将排好序的字符串数组拼接出来。关键就是制定排序规则。 * 排序规则如下: * 若ab > ba 则 a > b, * 若ab < ba 则 a < b, * 若ab = ba 则 a = b; * 解释说明: * 比如 "3" < "31"但是 "331" > "313",所以要将二者拼接起来进行比较*/ public String PrintMinNumber(int [] numbers) { if(numbers == null || numbers.length == 0) return ""; int len = numbers.length; String[] str = new String[len]; StringBuilder sb = new StringBuilder(); for(int i = 0; i < len; i++) str[i] = String.valueOf(numbers[i]); Arrays.sort(str,new Comparator<String>(){ @Override public int compare(String s1, String s2) { String c1 = s1 + s2; String c2 = s2 + s1; return c1.compareTo(c2); } }); for(int i = 0; i < len; i++) sb.append(str[i]); return sb.toString(); }