LeetCode-3-Longest-Substring-Without-Repeating-Characters 尺取法+Python数组初始化+ASCLL转换
来源:互联网 发布:社交网络可视化工具 编辑:程序博客网 时间:2024/06/07 13:21
给一个字符串,包含各种字母和标点的,求最长的连续的不包含同种字符的字符串的长度,典型的尺取法。
C++:
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.length()==0)return 0; int cur[300]; memset(cur,0,sizeof(cur)); int l=0,r=0; int ans=0,curans=0; while(l<=r&&r<s.length()){ if(cur[s[r]]==0){ cur[s[r]]++; r++; curans+=1; ans=max(ans,curans); } else{ cur[s[l]]-=1; curans-=1; l++; } } return ans; }};
java:
对一个数组快速赋值用Arrays.fill(a,0)
字符串访问元素要用s.charAt(i)
比较二者大小用Math.max(a,b)
public class Solution { public int lengthOfLongestSubstring(String s) { if(s.length()==0)return 0; int cur[]=new int[300]; Arrays.fill(cur, 0); int l=0,r=0; int ans=0,curans=0; // while(l<=r&&r<s.length()){ if(cur[s.charAt(r)]==0){ cur[s.charAt(r)]++; r++; curans+=1; ans=Math.max(ans, curans); } else{ cur[s.charAt(l)]-=1; curans-=1; l++; } } return ans; }}
Python:
初始化数组为100个0
a=[0 for x in range(100)]
用a=range(100)会得出0-99,用range(1,100)得出1-99
a=[[0 for x in range(10)] for y in range(10)]定义一个10*10的二维数组初始化为0
a=[0]*100可以定义出一个大小100的0数组
a=[[0]*10]*10看起来能定义出一个10*10的二维数组,但是我们发现修改其中的一维的数据,其他维的也会被跟着修改,所以这并不是真正意义上的二维数组
此外ord(char)是获取字符的ascll码,chr(int)是获取一个ascll码的字符
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if s.__len__()==0:return 0; cur=[0]*300 l=0;r=0;ans=0;curans=0 while(r<s.__len__() and l<=r): if cur[ord(s[r])]==0: cur[ord(s[r])]+=1 r+=1 curans+=1 ans=max(ans,curans) else: cur[ord(s[l])]-=1 curans-=1 l+=1 return ans
阅读全文
2 0
- LeetCode-3-Longest-Substring-Without-Repeating-Characters 尺取法+Python数组初始化+ASCLL转换
- Leetcode -- Python -- Longest Substring Without Repeating Characters
- 【Leetcode】【python】Longest Substring Without Repeating Characters
- [LeetCode]3 Longest Substring Without Repeating Characters (C++,Python实现)
- Python 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
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- CentOS救援模式
- tomcat端口占用排查
- 反射
- Windows10安装Linux bash(亲试)
- 简单递归函数
- LeetCode-3-Longest-Substring-Without-Repeating-Characters 尺取法+Python数组初始化+ASCLL转换
- [LeetCode]237. Delete Node in a Linked List
- winhex系列1----软件介绍
- javascript变量
- 用Kotlin开发android平台语音识别,语义理解应用(olamisdk)
- Apache服务器的下载与安装
- spring+springMVC+hibernate整合
- CentOS7 Hadoop-2.7.4 分布式部署
- javaScript运算符