Longest Substring Without Repeating Characters
来源:互联网 发布:html img 属性 js 编辑:程序博客网 时间:2024/06/16 06:12
题目(Longest Substring Without Repeating Characters)-medium
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
C语言解答
int lengthOfLongestSubstring(char* s) { int x = 0; int y = 0; int longest = 1; int tmplongest = 1; int letters[200]={0};//利用一个letters字符数组来标记某个字符是否访问过,时间复杂度低,效率更高。当然这种方法不够严谨,尤其配合空格 //int length = sizeof(s) / sizeof(s[0]); int length = strlen(s);//正确计算char数组长度的方法 int i = 0; for(i = 0; i< 200;i++){ letters[i] = 0; } letters[s[x]-' '] = 1;//空格是比较小的一个字符,虽然在ASCII码中不是最小的,但是从题目能表示的字符中,可以这样来达到要求。 if(length == 0){ longest = 0;//如果是空字符串,也要将longest置为0 } while(y<length){ if(letters[s[y+1]-' '] == 0){ y++; tmplongest++; if(longest < tmplongest){ longest = tmplongest; } letters[s[y]-' '] = 1; }else{ letters[s[x]-' '] = 0; x++; tmplongest--; } } return longest;}
收获
1. char[2] array1 = {'a','b'};和char* array2 = "ab";这两者长度的计算方法是不一样的;
array1的length = sizeof(array1)/sizeof(array1[0]);
array2的length = strlen(array2);
2. 比较难考虑的点:
(1) 如果s为空;
(2) 如何判断某个字符已经被访问过。
0 0
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- 二叉树的创建和遍历
- stm32f103zet6+enc28j60上移植lwip,建立TCP客户端通信
- Android API Guides---Layout Resource
- 缓存文件的路径及特点
- Android OkHttp完全解析 是时候来了解OkHttp了
- Longest Substring Without Repeating Characters
- Windows PHP/phpStudy 连接 甲骨文Oracle 数据库 oci8
- JSPatch使用
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- 虚拟内存与物理内存
- IntelliJ IDEA 2016.1(64) 注册码
- nsstring 编码encode类型
- 【毕业生推荐系统】(1) 项目简介
- maven搭建ssh