首页 > 其他 > 详细

leetcode395

时间:2018-10-06 11:36:31      阅读:209      评论:0      收藏:0      [点我收藏+]

分治法。

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!