leetcode
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> iListList= new ArrayList<List<Integer>>(); int depth=0; exploreTreeNode(root,iListList,depth ); return iListList; } public void exploreTreeNode(TreeNode root, List<List<Integer>> iListList, int depth){ if(root==null) return; if( depth >= iListList.size() ){ List<Integer> iList = new ArrayList<Integer>(); iListList.add(depth, iList); iList.add(root.val); } else{ iListList.get(depth).add(root.val); } if(null!=root.left){ exploreTreeNode(root.left, iListList , depth+1); } if(null!=root.right){ exploreTreeNode(root.right, iListList , depth+1); } } }
public class Solution { private int getPivot(int[] nums){ //System.out.println("start:"+start+"end:"+end); if(nums[0]<nums[nums.length-1]){ return -1; } if(nums.length <=1){ return 0; } int start=0; int step=2; while(true){ step=2; if(nums[start]>nums[start+step-1]){ return start+step-1; } while(step<=nums.length){ if((nums[start]>nums[start+step-1]) ||(nums[start]>nums[nums.length-1])){ start=start+(step/2); break; } step=step*2; //System.out.println(step); } } } private int getTarget(int[] nums, int start, int end, int target){ int middle=(start+end)/2; if(nums[middle]==target) { return middle; } if((start==end)&&(nums[middle]!=target)){ return -1; } if(target<nums[middle]){ return getTarget(nums,start, middle , target); }else{ return getTarget(nums,middle+1, end , target); } //return 0; } public int search(int[] nums, int target) { if(nums.length<=0){ return -1; } int pivot=getPivot(nums); //System.out.println("pivot:"+pivot); if(pivot==-1){ return getTarget(nums,0, nums.length-1,target); } int start=0; int end = nums.length-1; int tempPivot=pivot; tempPivot=((tempPivot-1)>=0)?(tempPivot-1):tempPivot; if((target>=nums[0])&&(target<=nums[tempPivot])){ return getTarget(nums,0,tempPivot, target); } else{ return getTarget(nums,pivot,end, target); } //return 0; } }
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode listNode= new ListNode(0); listNode.next=null; ListNode headNode=null; int temp1=0; int temp2=0; headNode=listNode; ListNode tempNode; int oneAdd=0; while(true){ if((l1==null)&&(l2==null)){ break; } temp1=(l1==null)?0:l1.val; temp2=(l2==null)?0:l2.val; //System.out.println("total"+temp1+temp2+oneAdd); if((temp1+temp2+oneAdd)>=10){ listNode.val=(temp1+temp2+oneAdd)-10; oneAdd=1; } else{ listNode.val=(temp1+temp2+oneAdd); oneAdd=0; } //System.out.println("val:"+listNode.val); if(l1!=null){ //System.out.println("l1:"+l1.val); l1=l1.next; } if(l2!=null){ //System.out.println("l2:"+l2.val); l2=l2.next; } if(!((l1==null)&&(l2==null))){ //System.out.println("add new node"); tempNode=new ListNode(0); listNode.next=tempNode; listNode=listNode.next; listNode.next=null; } } if(oneAdd>0){ tempNode=new ListNode(1); listNode.next=tempNode; listNode=listNode.next; listNode.next=null; } return headNode; } }
public class Solution { public String longestPalindrome(String s) { int start=0; int end=0; int orderP=0; int reverseP=s.length()-1; int tempOrderP; int tempEnd=0; if(s.length()==1){ return s.substring(0,1); } while(orderP<s.length()){ reverseP=s.length()-1; tempOrderP=orderP; //System.out.println("tempOrderP begin:"+tempOrderP); while(true) { tempOrderP=orderP; while(reverseP>tempOrderP){ if(s.substring(tempOrderP,tempOrderP+1).equals(s.substring(reverseP,reverseP+1))) break; else reverseP--; } tempEnd=reverseP; while(reverseP>tempOrderP){ if(s.substring(tempOrderP,tempOrderP+1).equals(s.substring(reverseP,reverseP+1)) == false) break; else{ reverseP--; tempOrderP++; } } if(reverseP<=tempOrderP){ if((tempEnd-orderP)>(end-start)){ start=orderP; end=tempEnd; //System.out.println("start :"+start); //System.out.println("end :"+end); } break; }else{ reverseP=tempEnd-1; //System.out.println("reverseP :"+reverseP); } } orderP++; } if(start==end){ return s.substring(0,1); }else{ return s.substring(start,end+1); } } }
public class Solution { public String convert(String s, int numRows) { ArrayList<String> sArrayList= new ArrayList<String>(); if((s.length() <= numRows) ||(numRows==1) ){ return s; } int i=0; while(i<numRows){ sArrayList.add(new String(s.substring(i,i+1))); i++; } int column=1; int columnMax=0; int columnMaxPrev=0; String tempStr=null; while(i<s.length()){ if(column%2==1){ if(i>columnMax){ columnMax=(i+numRows-1-1); } tempStr=sArrayList.get(columnMax-i)+s.substring(i,i+1); sArrayList.set(columnMax-i,tempStr); //sArrayList.get(columnMax-i)=sArrayList.get(columnMax-i);//+s.substring(i,i+1); i++; if(i>columnMax){ column++; } } else{ tempStr=sArrayList.get(i-columnMax)+s.substring(i,i+1); sArrayList.set(i-columnMax,tempStr); //sArrayList.get(i-columnMax)=sArrayList.get(i-columnMax)+s.substring(i,i+1); i++; if(i>(columnMax+numRows-1)){ column++; } } } String strResult=new String(""); Iterator iterSArrayList=sArrayList.iterator(); while(iterSArrayList.hasNext()){ strResult=strResult+iterSArrayList.next(); } return strResult; } }
MySQL限时解答,24小时内友哥专业解答
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指