github_34366006 2016-03-20 17:18 采纳率: 0%
浏览 1634

Leetcode第三题,本地运行72ms,为何提交后TLE?

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
使用了HashSet,会不会是因为Mono的HashSet实现比微软的性能差很多?本地跑相同测试用例只用了72ms,提交后还是TLE
代码如下:

        public int LengthOfLongestSubstring(string s)
        {
            if (s.Length <= 1)
            {
                return s.Length;
            }
            int length = 0;
            for (int i = 0; i < s.Length; i++)
            {
                HashSet<char> set = new HashSet<char>();
                for (int j = 0; i + j < s.Length; j++)
                {
                    int offset = 0;
                    if ((offset = Add(set, s[i + j])) != -1)
                    {
                        length = length > j ? length : j;
                        i += offset;
                        break;
                    }
                }
            }
            return length;
        }

        public int Add(HashSet<char> set, char ch)
        {
            if (!set.Add(ch))
            {
                for (int i = 0; i < set.Count; i++)
                {
                    if (set.ElementAt(i) == ch)
                    {
                        return i + 1;
                    }
                }
            }
            return -1;
        }
  • 写回答

1条回答

  • devmiao 2016-03-20 20:27
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果