Leetcode Q3:Longest Substring Without Repeating Characters
来源:互联网 发布:sql查询每小时 编辑:程序博客网 时间:2024/06/03 21:05
题目3:
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.
#define MAP_SIZE 256 int lengthOfLongestSubstring(char* s) { int i = 0; int start = 0; char* p = NULL; int temp_max_size = 0; /* 当次找到的最大子串长度 */ int max_size = 0; static int MapRecord[MAP_SIZE]; /* 如果将这两个数组放在全局变量中,在leetcode上就会报错 */ memset(MapRecord, -1, MAP_SIZE*sizeof(int)); p = s; start = 0; /* 记录当前查询到的最长子串的起始位置 */ while (*p != '\0') { if (MapRecord[*p] == -1) { MapRecord[*p] = p - s; /* map记录已经在子串中 */ temp_max_size++; p++; } else /* 遇到相同的字符 */ { /* 当前查找到的最大子串中,清空相同字符前的map,子串从相同字符串位置后一个开始记录 */ for (i = start; i < MapRecord[*p]; i++) { MapRecord[*(s+i)] = -1; } if (temp_max_size > max_size) { max_size = temp_max_size; } temp_max_size -= (MapRecord[*p] - start); start = MapRecord[*p] + 1; /* 修改当前最大子串的开始位置 */ MapRecord[*p] = p - s; p++; } } /* 字符串结束时,输出结果 */ if (temp_max_size > max_size) { max_size = temp_max_size; } return max_size;}
0 0
- Leetcode Q3:Longest Substring Without Repeating Characters
- [LeetCode]Q3. Longest Substring Without Repeating Characters
- LeetCode-Q3-Longest Substring Without Repeating Characters
- [leetcode Q3] —— Longest Substring Without Repeating Characters
- leetCode Q3:Longest Substring Without Repeating Characters(java)
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- 做动态规划题的步骤和例子(还在完善中)
- Python version 2.7 required, which was not found in the registry
- Android入门-从firstactivity开始-button
- 最小生成树之prim算法
- Leetcode: Sliding Window Maximum
- Leetcode Q3:Longest Substring Without Repeating Characters
- word左边左对齐,右边右对齐
- mysql mysql SELECT 自增数显示
- windows系统突然死机,windows系统重新启动后,再启动虚拟机,发现ubuntu系统进入进入initramfs,输入exit都无法退出
- hdoj-3371-Connect the Cities【最小生成树】
- Mysql语法及使用命令总结
- Ubuntu14.04 root登录 ssh 连接
- 小白学习freemark的过程(代码全贴+详细介绍)
- 【用户界面:android-Ultra-Pull-To-Refresh】之一:android-Ultra-Pull-To-Refresh开篇