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

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;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558888.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 多线程 —— 常用并发容器

引言 本博客基于常用的并发容器&#xff0c;简单概括其基本特性和简单使用&#xff0c;并不涉及较深层次的原理分析和全面的场景用法。 适合对不了解并发容器的同学&#xff0c;工作中遇到类似的场景&#xff0c;能够对文中提到的并发容器留有简单印象就好。 一、Concurrent…

LeetCode算法入门- Longest Palindromic Substring-day5

LeetCode算法入门- Longest Palindromic Substring-day5 Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: “babad” Output: “bab” Note: “a…

Tomcat运行三种模式:http-bio|http-nio|http-apr介绍

转自《tomcat运行三种模式:http-bio|http-nio|http-apr介绍》 Tomcat是一个小型的轻量级应用服务器&#xff0c;也是JavaEE开发人员最常用的服务器之一。不过&#xff0c;许多开发人员不知道的是&#xff0c;Tomcat Connector(Tomcat连接器)有bio、nio、apr三种运行模式&#…

LeetCode算法入门- Reverse Integer-day6

LeetCode算法入门- Reverse Integer-day6 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 class Solution {public int reverse(int x) {long…

Java工具方法——属性拷贝方法:BeanUtils.copyProperties(Object, Object)

介绍 org.springframework.beans.BeanUtils.copyProperties(Object, Object)是spring 框架的对象工具类&#xff1a;BeanUtils下的一个拷贝对象属性的方法。 官方注释 把给定的源对象属性值拷贝到目标对象中。 注意&#xff1a;源对象类与目标对象类不一定非要完全匹配&…

LeetCode算法入门- String to Integer (atoi)-day7

LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. The…

LeetCode算法入门- Roman to Integer Integer to Roman -day8

LeetCode算法入门- Roman to Integer -day8 Roman to Integer&#xff1a; 题目描述&#xff1a; Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol    Value I        1 V        5 X        10 L…

Git初学札记(九)————EGit检出远程分支

引言 现在有这样一个使用场景&#xff1a;团队中的其他开发者提交了一个新的特性分支&#xff08;如feature_1&#xff09;&#xff0c;要求我们一同开发&#xff0c;并将自己修改的代码也全部提交到这个分支上去。那么如何将这个分支检出&#xff0c;并将本地检出的分支与这个…

LeetCode算法入门- 3Sum -day9

LeetCode算法入门- 3Sum -day9 题目描述&#xff1a; Given an array nums of n integers, are there elements a, b, c in nums such that a b c 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain dup…

XML学习(一)————XML简介

引言 作为数据传输界鼎鼎大名的扛把子&#xff0c;XML被应用于各个方面&#xff0c;但随着弱结构化标记语言如JSON、YAML等的出现&#xff0c;人们慢慢的脱离了XML的统治&#xff0c;但在互联网早期的发展当中XML是不可或缺的一部分&#xff0c;比如各种微信开发中的数据传输&…

Java核心篇之Java锁--day2

Java核心篇之Java锁–day2 乐观锁&#xff1a;乐观锁是一种乐观思想&#xff0c;即认为读多写少&#xff0c;每次去取数据的时候都认为其他人不会修改&#xff0c;所以不会上锁&#xff1b;但是在更新的时候会判断一下在此期间别人有没有去修改它&#xff0c;如果有人修改的话…

XML学习(二)————属性还是标签?

引言 xml中并没有规则要求我们什么时候使用属性&#xff0c;什么时候使用标签。 属性和标签都可以存储数据&#xff0c;但是在XML的使用中&#xff0c;我们需要探讨一下对属性和标签的选择问题。 约定规则 XML 应该避免使用属性来存储数据&#xff0c;这与HTML的推荐规则不…

LeetCode算法入门- 3Sum Closest -day10

LeetCode算法入门- 3Sum Closest -day10 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one …

Spring Boot————Web应用启动时自动执行ApplicationListener用法

原文&#xff1a;《web服务启动spring自动执行ApplicationListener的用法》 引言 我们知道&#xff0c;一般来说一个项目启动时需要加载或者执行一些特殊的任务来初始化系统&#xff0c;通常的做法就是用servlet去初始化&#xff0c;但是servlet在使用Spring bean时不能直接注…

LeetCode算法入门- 4Sum -day11

LeetCode算法入门- 4Sum -day11 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a b c d target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution se…

Spring Boot————ApplicationListener实现逃课事件监听

引言 上一篇文章转了一篇关于ApplicationListener用于在Web项目启动时做一些初始化的用法。 但是&#xff0c;在实际生产过程中&#xff0c;当一个事件产生&#xff0c;又是如何被onApplicationEvent()方法监听到&#xff0c;并执行一系列动作呢&#xff1f;简单搜索了一下&a…

Java核心篇之Redis--day4

Java核心篇之Redis–day4 Redis有哪些数据结构&#xff1f; 字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。 1.String&#xff1a;字符串&#xff0c;常用命令&#xff1a;get&#xff0c;set&#xff0c;decr&#xff0c;incr&#xff0c;mget&#xff08;查…

软件版本GA、RC、beta等含义

原文《软件版本GA、RC、beta等含义》 GA General Availability&#xff0c;正式发布的版本&#xff0c;官方开始推荐广泛使用&#xff0c;国外有的用GA来表示release版本。 RELEASE 正式发布版&#xff0c;官方推荐使用的版本&#xff0c;有的用GA来表示。比如spring。 Sta…

Java核心篇之泛型--day5

Java核心篇之泛型–day5 泛型是JDK5时引入的一个新特性&#xff0c;泛型提供了编译时类型安全检查的机制&#xff0c;该机制允许程序猿在编译时检测到非法的类型输入。 泛型的本质是参数化类型&#xff0c;也就是说操作的类型被指定为一个参数。 假定我们有一个需求&#xff…

Spring Boot————AOP入门案例及切面优先级设置

看了这篇文章&#xff0c;如果你还是不会用AOP来写程序&#xff0c;请你打我&#xff01;&#xff01; .||| 引言 Spring AOP是一个对AOP原理的一种实现方式&#xff0c;另外还有其他的AOP实现如AspectJ等。 AOP意为面向切面编程&#xff0c;是通过预编译方式和运行期动态代…