LeetCode 3 Longest Substring Without Repeating Characters
来源:互联网 发布:pywin32 linux 编辑:程序博客网 时间:2024/05/14 20:47
Problem:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
Solution:
无
题目大意:
题意比较简单,给定一个字符串,要求得出最长子串的长度,子串要求无重复字符。
解题思路:
可以采用Hash存储+动态规划的方式解决,从左到右遍历一遍,标记子串的最左边位置,当遇到重复字符时,说明当前子串已经结束,开始一个新的字符串,更新字符的位置和子串的left。
Java源代码:
public class Solution { public int lengthOfLongestSubstring(String s) { int left=0,Max=0; Map<Character,Integer> map = new HashMap<Character,Integer>(); char[] chs = s.toCharArray(); for(int i=0;i<chs.length;i++){ if(map.containsKey(chs[i]) && map.get(chs[i])>=left){ left = map.get(chs[i])+1; } map.put(chs[i],i); Max = Max>(i-left+1)?Max:(i-left+1); } return Max; }}
C语言源代码:
#include<limits.h>#include<stdio.h>int lengthOfLongestSubstring(char* s) { int i,j,left=0,Max=0,hash[256]; for(j=0;j<256;j++)hash[j]=INT_MAX; for(i=0;s[i];i++){ if(hash[s[i]]!=INT_MAX && hash[s[i]]>=left) left=hash[s[i]]+1; hash[s[i]]=i; Max = Max>(i-left+1)?Max:(i-left+1); } return Max;}
C++源代码:
class Solution {public: int lengthOfLongestSubstring(string s) { int Max=0,left=0; map<int,int> map; for(int i=0;i<s.size();i++){ std::map<int,int>::iterator iter=map.find(s[i]); if(iter!=map.end() && iter->second >=left){ left=iter->second+1; } map[s[i]]=i; Max = Max>(i-left+1)?Max:(i-left+1); } return Max; }};
Python源代码:
class Solution: # @param {string} s # @return {integer} def lengthOfLongestSubstring(self, s): left=0 Max=0 hash={} for i in range(len(s)): ch = s[i] if hash.has_key(ch) and hash[ch]>=left: left=hash[ch]+1 hash[ch]=i Max= Max if Max>i-left+1 else i-left+1 return Max
0 0
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode 3: Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- 利用PHPexcel将excel表导入mysql数据库
- hdu 1014
- nginx.conf文件参数分析
- 欧拉工程第29题:Distinct powers
- POJ1579
- LeetCode 3 Longest Substring Without Repeating Characters
- iOS URL schemes
- 模板函数多个类型参数时候怎么调用
- heroku部署java web项目
- Linked List Cycle —— Leetcode
- android 防止对控件的重复点击和同时点击多个控件
- SDUT 2605 A^X mod P
- maven安装相关
- Android性能调优