Advertisement
sweet1cris

Untitled

Jan 9th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. public class Solution {    
  2. //方法一:
  3.     int initTargetHash(int []targethash, String Target) {
  4.         int targetnum =0 ;
  5.         for (char ch : Target.toCharArray()) {
  6.             targetnum++;
  7.             targethash[ch]++;
  8.         }
  9.         return targetnum;
  10.     }
  11.     boolean valid(int []sourcehash, int []targethash) {
  12.        
  13.         for(int i = 0; i < 256; i++) {
  14.             if(targethash[i] > sourcehash[i])    
  15.                 return false;
  16.         }
  17.         return true;
  18.     }
  19.     public String minWindow(String Source, String Target) {
  20.         // queueing the position that matches the char in T
  21.         int ans = Integer.MAX_VALUE;
  22.         String minStr = "";
  23.        
  24.         int[] sourcehash = new int[256];
  25.         int[] targethash = new int[256];
  26.        
  27.         initTargetHash(targethash, Target);
  28.         int j = 0, i = 0;
  29.         for(i = 0; i < Source.length(); i++) {
  30.             while( !valid(sourcehash, targethash) && j < Source.length()  ) {
  31.                 sourcehash[Source.charAt(j)]++;
  32.                 j++;
  33.             }
  34.             if(valid(sourcehash, targethash) ){
  35.                 if(ans > j - i ) {
  36.                     ans = Math.min(ans, j - i );
  37.                     minStr = Source.substring(i, j );
  38.                 }
  39.             }
  40.             sourcehash[Source.charAt(i)]--;
  41.         }
  42.         return minStr;
  43.     }
  44. }
  45.  
  46. public class Solution {
  47.     int initTargetHash(int []targethash, String Target) {
  48.         int targetnum =0 ;
  49.         for (char ch : Target.toCharArray()) {
  50.             targetnum++;
  51.             targethash[ch]++;
  52.         }
  53.         return targetnum;
  54.     }
  55.    
  56.     public String minWindow(String Source, String Target) {
  57.         // queueing the position that matches the char in T
  58.         int ans = Integer.MAX_VALUE;
  59.         String minStr = "";
  60.        
  61.         int[] targethash = new int[256];
  62.        
  63.         int targetnum = initTargetHash(targethash, Target);
  64.         int sourcenum = 0;
  65.         int j = 0, i = 0;
  66.         for(i = 0; i < Source.length(); i++) {
  67.             if(targethash[Source.charAt(i)] > 0)
  68.                 sourcenum++;
  69.            
  70.             targethash[Source.charAt(i)]--;
  71.             while(sourcenum>=targetnum) {
  72.                 if(ans > i - j + 1) {
  73.                     ans = Math.min(ans, i - j + 1);
  74.                     minStr = Source.substring(j, i + 1);
  75.                 }
  76.                 targethash[Source.charAt(j)]++;
  77.                 if(targethash[Source.charAt(j)] > 0)
  78.                     sourcenum--;
  79.                 j++;
  80.             }
  81.         }
  82.         return minStr;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement