LeetCode OJ 之 Longest Substring Without Repeating Characters 解题报告


题目: Longest Substring Without Repeating Characters

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.


Solution:

class Solution {
public:
    int lengthOfLongestSubstring(string str){
		int result = 0;
		int temp_res = 0;
		int rec_arr[128];
		memset(rec_arr, 0, sizeof(rec_arr));
		for (int i = 0; i < str.length(); i++)
		{
			temp_res = 0;
			memset(rec_arr, 0, sizeof(rec_arr));
			for (int j = i; j < str.length(); j++)
			{
				if (str[j] >= 128 || str[j] < 0)
				{
					continue;
				}
				if (rec_arr[str[j]] != 0)
				{
					break;
				}
				else
				{
					rec_arr[str[j]]++;
					temp_res++;
				}
			}
			if (temp_res > result)
			{
				result = temp_res;
			}
		}
		return result;
	}
};

思路:

题目要求求给定字符串中无重复字符的最长子串的长度。

则从字符串的每一个位置依次向后开始遍历,与出现次数的数组成员比较,到出现相同字符时停止或者字符串结尾停止。在此之前如不相同,则将记录出现次数的数组相应成员进行自增操作。

收获:

1.在对数组进行初始化时,使用memset函数,应注意:memset的填充是以字节为单位的,如果你的数组元素不是单个字节的,填充成别的值就会出错只能填充0或者空值,可 自行验证这一结论。另外,在以后应该熟练使用memst函数对数组进行清零操作。

2.在字符串的处理中应熟练掌握Ascii码,在这道题中使用Ascii码更加简单


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值