Longest Substring Without Repeating Characters题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/


Description

Given a string, find the length of the longest substring without repeating characters..

Example

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.

Solution

int lengthOfLongestSubstring(char* s) {
int len = strlen(s);
if (len == 0)
return 0; int charbucket[300] = {0};
int startidx, i;
int maxlen = 1, substrlen = 0;
for (startidx = 0; startidx < len; startidx++) {
memset(charbucket, 0, sizeof(charbucket));
for (i = startidx; i < len; i++) {
if (charbucket[s[i]] == 0) {
charbucket[s[i]] = 1;
} else {
break;
}
}
substrlen = 0;
for (i = 0; i < 300; i++) {
if (charbucket[i] == 1)
substrlen++;
}
if (substrlen > maxlen)
maxlen = substrlen;
}
return maxlen;
}

解题描述

这道题目的是找到给定字符串最长的子串的长度,且子串的字符不能重复。我的突破口是针对字符不能重复这个点,使用一个类似C++中的布尔数组,记录子串中出现的字符。然后通过不断查找到最长字串的长度更新结果值。

最新文章

  1. 现代3D图形编程学习-基础简介(2) (译)
  2. ORA-12516:TNS:listener could not find available handler with matching protocol stack
  3. 创建你的第一个JavaScript库
  4. 不想说作用域scope,因为是scopeTree,
  5. 响应式Web设计 - 布局
  6. 提供一个表单,进行post数据处理
  7. python爬取并下载麦子学院所有视频教程
  8. 面向对象_python
  9. dp重拾-01背包--HDU 2602
  10. 斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感[转]
  11. 取消界面的title
  12. WWDC2015—图解
  13. NSAssert使用摘抄
  14. 容易被忽略的两个方法:onSaveInstanceState()和onRestoreInstanceState()
  15. 你应该知道的8个Java牛人
  16. Elasticsearch最佳实践之分片使用优化
  17. SnackbarUtilDemo【Snackbar的封装类】
  18. 命令行批量修改IP并ping测试
  19. VS 统计整个项目总的代码行数
  20. Android开发教程 - 使用Data Binding Android Studio不能正常生成相关类/方法的解决办法

热门文章

  1. Python 3基础教程27-字典
  2. JMeter上传图片
  3. php处理三级分类数据
  4. 树莓派配置 USB 无线网卡
  5. 在Android Studio中创建(或添加)第一个Hello World应用程序
  6. Week7 Teamework from Z.XML-任务分配
  7. 【转】 The user specified as a definer (&#39;root&#39;@&#39;&#39;) does not exist when using LOCK TALBE
  8. 【转】webpack4
  9. postman工具【接口自动化测试关于断言】
  10. HDU 1338 Game Prediction