[Leetcode] Minimum Window Substring

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   for   sp   div   

演算法思想: http://www.cnblogs.com/lichen782/p/leetcode_minimum_window_substring_3.html

 1 public class Solution { 2     public String minWindow(String S, String T) { 3         int N = S.length(); 4         int M = T.length(); 5         if(N < M) 6             return ""; 7         int[] need=new int[256]; 8         int[] find=new int[256]; 9         for(int i=0;i<M;++i)10             need[T.charAt(i)]++;11         12         int count=0;13         int resStart=-1;14         int resEnd=N;15         16         for(int start=0,end=0;end<N;++end){17             if(need[S.charAt(end)]==0)18                 continue;19             if(find[S.charAt(end)]<need[S.charAt(end)])20                 count++;21             find[S.charAt(end)]++;22             if(count!=M)23                 continue;24             25             for(;start<end;++start){26                 if(need[S.charAt(start)]==0)27                     continue;28                 if(find[S.charAt(start)]<=need[S.charAt(start)])29                     break;30                 find[S.charAt(start)]--;31             }32             if(end-start<resEnd-resStart){33                 resStart=start;34                 resEnd=end;35             }36         }37         return (resStart==-1)?"":S.substring(resStart, resEnd+1);38     }39 }

 

[Leetcode] Minimum Window Substring

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.