LeetCode算法入门- Longest Substring Without Repeating Characters-day4

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:

Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

方法一:暴力破解法:时间复杂度为:0(n^3)

class Solution {public int lengthOfLongestSubstring(String s) {int n = s.length();int ans = 0;for(int i = 0; i < n; i++){for(int j = i+1; j <= n; j++){if(allUnique(s, i, j))//取最大值ans = Math.max(ans,j-i);}}return ans;}public static boolean allUnique(String s, int start, int end){Set<Character> set = new HashSet<>();for(int i = start; i < end; i++){Character ch = s.charAt(i);//通过set数据结构的contains方法来判断是否重复if(set.contains(ch))return false;set.add(ch);}return true;}
}

方法二:基本思路为:i,j都是从0开始,当s.charAt(j)不在set中的时候,j++,同时set.add(s.charAt(j));当s.charAt(j)在set当中时,i++,同时移除掉s.charAt(i),注意i+1加到set中不含s.charAt(j)为止。取出ans与j-i之间的最大值即可。
利用set数据结构

class Solution {public int lengthOfLongestSubstring(String s) {int n = s.length();Set<Character> set =new HashSet<>();int ans = 0;int i = 0;int j = 0;while(i < n && j <n){if(!set.contains(s.charAt(j))){set.add(s.charAt(j++));ans = Math.max(ans, j - i);}else{set.remove(s.charAt(i++));}}return ans;}
}

LeetCode算法入门- Longest Substring Without Repeating Characters-day4相关推荐

  1. 【LeetCode】3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  2. Leetcode 3:Longest Substring Without Repeating Characters(最长不重复子串)

    Description Given a string, find the length of the longest substring without repeating characters. 给 ...

  3. leetcode算法—无重复字符的最长子串 Longest Substring Without Repeating Characters

    关注微信公众号:CodingTechWork,一起学习进步. 题目 Longest Substring Without Repeating Characters: Given a string, fi ...

  4. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  5. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  6. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

  7. [LeetCode]3.Longest Substring Without Repeating Characters

    [题目] Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. LeetCode——Longest Substring Without Repeating Characters

    原问题 Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  9. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

最新文章

  1. oracle glogin.sql sql _user,为什么我的login.sql不执行?
  2. 一种可移除点云中动态目标的鲁棒方法
  3. jquery实现层级显示 效果图_php运用无限级分类实现评论及回复功能
  4. 大厂首发!尚硅谷docker高级
  5. java 动态添加定时器_Spring整合Quartz实现动态定时器的示例代码
  6. LED计数电路,5输入按键编码器,7段数码管显示驱动集成为LED计数测试电路
  7. 想入职AI算法岗?BAT的工程师去学了这门课
  8. 《Spark核心技术与高级应用》——3.2节构建Spark的开发环境
  9. 离线安装 KubeSphere 2.1.1 与 Kubernetes
  10. drools 7.x 模板的简单使用
  11. jQuery:实现显示更多动画
  12. 【pytorch】RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of si
  13. XMind8 pro 免费破解版!速度
  14. WORD锁定修订功能,记录别人更改你文件的步骤?
  15. Report中的Drill down
  16. 强烈建议三星S7edge刷番茄花园V14终结版
  17. 没有软件开发人员,可以办理CMMI3级吗?
  18. 【JDK7】新特性(3) JDBC4.1
  19. webpack : 无法加载文件 C:\Users\zgl\AppData\Roaming\npm\webpack.ps1,因为在此系统上禁止运行脚本 。
  20. 微博相互关注互粉mysql表实现

热门文章

  1. 回文子序列_计算回文子序列的总数
  2. 你不知道的RabbitMQ集群架构全解
  3. golang开发环境配置及Beego框架安装
  4. 2.Pycharm + Django + Python进行WEB路由配置
  5. Opencv dnn实现人类性别检测和年龄预测
  6. LeetCode题库整理【Java】—— 3 无重复字符的最长子串
  7. webjs求数组的中位数‘_算法:一道常见的数组题,但很多人却写不出来(JAVA)...
  8. cmd在执行hadoop version时出现系统找不到指定的路径。 Error: JAVA_HOME is incorrectly set
  9. linux上安装python2.6.9_在Linux CentOS 6.6上安装Python 2.7.9
  10. axure文本框提示文字_Axure教程:一个中继器实现密码验证