码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode395

时间:2018-10-06 11:55:53      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:sts   substr   leetcode   color   string   col   char   new   str   

分治法。

public class Solution
    {
        private int LongestSubstringSub(string s, int k, int start, int end)
        {
            if (start > end)
            {
                return 0;
            }
            int[] count = new int[26];
            for (int i = start; i <= end; i++)
            {
                count[s[i] - a]++;
            }

            for (int i = 0; i < 26; i++)
            {
                if (count[i] > 0 && count[i] < k)
                {
                    var pos = s.IndexOf((char)(i + a), start);
                    return Math.Max(LongestSubstringSub(s, k, start, pos - 1), LongestSubstringSub(s, k, pos + 1, end));
                }
            }

            return end - start + 1;
        }

        public int LongestSubstring(string s, int k)
        {
            return LongestSubstringSub(s, k, 0, s.Length - 1);
        }
    }

 

leetcode395

标签:sts   substr   leetcode   color   string   col   char   new   str   

原文地址:https://www.cnblogs.com/asenyang/p/9746685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!