标签:long 代码 mat span space col math 思路 substr
一:解题思路
Time:O(n),Space:O(1)
二:完整代码示例 (C++版和Java版)
C++:
class Solution { public: int lengthOfLongestSubstring(string s) { if (s.size() == 0) return 0; int maxLen = 0; vector<int> index(256,-1); for (int i=0,j = 0; j < s.size(); j++) { i = max(index[s[j]]+1,i); maxLen = max(maxLen,j-i+1); index[s[j]] = j; } return maxLen; } };
Java:
class Solution { public int lengthOfLongestSubstring(String s) { if(s==null || s.length()==0) return 0; int[] index=new int[256]; Arrays.fill(index,-1); int maxLen=0; for(int i=0,j=0;j<s.length();j++) { i=Math.max(index[s.charAt(j)]+1,i); maxLen=Math.max(maxLen,j-i+1); index[s.charAt(j)]=j; } return maxLen; } }
标签:long 代码 mat span space col math 思路 substr
原文地址:https://www.cnblogs.com/repinkply/p/12639280.html