给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 class Solution { public int lengthOfLongestSubstring(String s) { //我打算靠它来记录连续的子串 Map<Character, Integer> hash = new HashMap<Character, Integer>(); int temp = 1; //再设立一个数据结构来存储每一段子串的长度 int length = 1; if(s.equals("")){ return 0; } for (int i = 0; i < s.length(); i++) { //设置一个计数器 temp = 1; //先清理一下,每次滑动一格都是新的开始 hash.clear(); hash.put(s.charAt(i),i); for (int j = i+1; j < s.length(); j++) { if (hash.containsKey(s.charAt(j))) { break; }else { hash.put(s.charAt(j),j); temp++; } } if (temp > length) { length = temp; } } return length; } }
leetcode第三题
最新推荐文章于 2024-08-12 18:10:44 发布