Thursday, May 25, 2017

71. Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"

 public String simplifyPath(String path) {  
     if(path==null || path.length()<=1) return path;  
     if(path.charAt(0)!='/') return "";  
     int last=1;  
     Deque st = new LinkedList<String>();  
     for(int i=1;i<=path.length();i++){  
       if(i==path.length() || path.charAt(i)=='/'){  
         String s=path.substring(last,i);  
         if(s.equals(".") || s.equals("")){  
         }  
         else if(s.equals("..")){  
           if(st.size()!=0) st.pop();  
         }  
         else {  
           st.push(s);  
         }  
         last=i+1;  
       }  
     }  
     if(st.size()==0) return "/";  
     StringBuilder sb=new StringBuilder();  
     while(st.size()!=0){  
       sb.append('/');  
       sb.append(st.removeLast());  
     }  
     return sb.toString();  
   }  

Saturday, May 20, 2017

LeetCode 2nd time 48. Rotate Image

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
More intuitive solution.
With up left index and bottom right index layer by layer
Each outer iteration will rotate one layer and after that upleft index -- and bottomright index++, we proceed to process next layer.


  public void rotate(int[][] matrix) {  
     int n=matrix.length;  
     int upleft=0;  
     int bottomright=n-1;  
    while(upleft<bottomright){  
        for(int j=upleft;j<bottomright;j++){  
         int temp=matrix[upleft][j];  
         matrix[upleft][j]=matrix[n-j-1][upleft];  
         matrix[n-j-1][upleft]=matrix[bottomright][n-j-1];  
         matrix[bottomright][n-j-1]=matrix[j][bottomright];  
         matrix[j][bottomright]=temp;  
       }  
       upleft++;  
       bottomright--;  
     }  
   }  

Friday, May 5, 2017

LeetCode 2nd time 7. Reverse Integer Leetcode Java

     public int reverse(int x) {  
    if(x==0 || x==Integer.MIN_VALUE) return 0;  
    boolean neg=(x<0);  
    int res=0;  
    x=Math.abs(x);  
    while(x !=0){  
      if(neg){  
        if((Integer.MIN_VALUE+(x%10))/10>res) return 0;  
        else res=res*10-x%10;  
      }  
      else{  
        if((Integer.MAX_VALUE-(x%10))/10<res) return 0;  
        else res=res*10+x%10;  
      }  
      x=x/10;  
    }  
    return res;  
   }  

LeetCode 2nd time 6. ZigZag Conversion Leetcode Java

     public String convert(String s, int numRows) {  
     if(s==null || numRows<=1 ||numRows>=s.length()) return s;  
     int l=s.length();  
     StringBuilder sb=new StringBuilder();  
     for(int i=0;i<numRows;i++){  
       int j=i;  
       while(j<l){  
         sb.append(s.charAt(j));  
         if((i!=0) && (i!=numRows-1) && ((j+(numRows-1-i)*2)<l)) sb.append(s.charAt(j+(numRows-1-i)*2));  
         j+=(numRows-1)*2;  
       }  
     }  
     return sb.toString();  
   }  

Thursday, May 4, 2017

LeetCode 2nd time 5. Longest Palindromic Substring Leetcode Java

   public String longestPalindrome(String s) {  
    if(s==null || s.length()<=1) return s;  
    int maxL=0;  
    int start=0;  
    for(int i=0; i<s.length();i++){  
      if(helper(s,i,i)>maxL){  
        maxL=helper(s,i,i);  
        start=i-maxL/2;  
      }  
      if((i!=0)&&(helper(s,i-1,i)>maxL)){  
        maxL=helper(s,i-1,i);  
        start=i-maxL/2;  
      }  
    }  
    return s.substring(start,start+maxL);  
   }  
   public int helper(String s, int i, int j){  
     while(i>=0 && j<s.length() && s.charAt(i)==s.charAt(j)){  
       i--;  
       j++;  
     }  
     return j-i-1;  
   }  

LeetCode 2nd time 4. Median of Two Sorted Arrays Leet code Java

 public double findMedianSortedArrays(int[] nums1, int[] nums2) {  
     int m=nums1.length;  
     int n=nums2.length;  
     if((m+n)%2==1){  
       return (double)findKHelper(nums1,nums2,0,m-1,0,n-1,(m+n+1)/2);  
     }  
     else {  
       return ((double)findKHelper(nums1,nums2,0,m-1,0,n-1,(m+n)/2)+(double)findKHelper(nums1,nums2,0,m-1,0,n-1,(m+n)/2+1))/2;  
     }  
   }  
   public int findKHelper(int[] nums1, int[] nums2, int l1, int r1, int l2, int r2,int k){  
     int m=r1-l1+1;  
     int n=r2-l2+1;  
     if(m>n) return findKHelper(nums2,nums1,l2,r2,l1,r1,k);  
     if(m==0) return nums2[l2+k-1];  
     if(k==1) return Math.min(nums1[l1],nums2[l2]);  
     int posA=Math.min(k/2,m);  
     int posB=k-posA;  
     if(nums1[l1+posA-1]==nums2[l2+posB-1]){  
       return nums1[l1+posA-1];  
     }  
     else if(nums1[l1+posA-1]<nums2[l2+posB-1]){  
       return findKHelper(nums1,nums2,l1+posA,r1,l2,l2+posB-1,k-posA);  
     }  
     else return findKHelper(nums1,nums2,l1,l1+posA-1,l2+posB,r2,k-posB);  
   }  

Wednesday, May 3, 2017

LeetCode 2nd time 3. Longest Substring Without Repeating Characters LeetCode Java

 public int lengthOfLongestSubstring(String s) {  
     if(s==null || s.length()==0) return 0;  
     int maxL=1;  
     int i=0;  
     int j=0;  
     // Set<Character> dupCheck=new HashSet<Character>();  
     Map<Character,Integer> dupCheck=new HashMap<Character,Integer>();  
     while(i<s.length()&&j<s.length()){  
         if(dupCheck.containsKey(s.charAt(j)) && dupCheck.get(s.charAt(j))>=i){  
           i=dupCheck.get(s.charAt(j))+1;  
         }  
        else {  
          maxL=Math.max(maxL,j-i+1);  
        }  
        dupCheck.put(s.charAt(j),j);  
        j++;  
       }  
     return maxL;  
   }  

LeetCode 2nd time 2. Add Two Numbers LeetCode Java

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {  
     if(l1==null) return l2;  
     if(l2==null) return l1;  
     int sum=l1.val+l2.val;  
     int carry=sum/10;  
     ListNode head=new ListNode(sum%10);  
     ListNode currentNode=head;  
     l1=l1.next;  
     l2=l2.next;  
     while(l1!=null || l2!=null || carry!=0){  
      int val1=(l1==null)? 0:l1.val;  
      int val2=(l2==null)? 0:l2.val;  
      sum=val1+val2+carry;  
      carry=sum/10;  
      ListNode nextNode=new ListNode(sum%10);  
      currentNode.next=nextNode;  
      currentNode=nextNode;  
     if(l1!=null) l1=l1.next;  
     if(l2!=null) l2=l2.next;  
     }  
     return head;  
   }  

LeetCode 2nd time 1. Two sum leetcode Java

   public int[] twoSum(int[] nums, int target) {  
     if(nums==null || nums.length<2) {  
      throw new IllegalArgumentException("No two sum solution");   
     }  
     Map<Integer,Integer> helper=new HashMap<Integer,Integer>();  
     for(int i=0;i<nums.length;i++){  
       if(helper.containsKey(target-nums[i])){  
       return new int[]{helper.get(target-nums[i]),i};  
       }  
       else{  
         helper.put(nums[i],i);  
       }  
     }  
      throw new IllegalArgumentException("No two sum solution");  
   }