文章标题

来源:互联网 发布:水果网络营销策划方案 编辑:程序博客网 时间:2024/05/21 18:41

leetcode 刷题日记

题目介绍

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.

我的解决思路

因为昨天刚用了示波器,受到了启发。所以我一开始的想法是使用两个cursor,相当于一个游标的作用,在字符串中通过游标的移动来实现。但是,TMD这leetcode里的测试集也太精确了,这个方法我的逻辑错误太多,而且好多脚标也没搞清楚,所以最后放弃了。
这里贴一个这样思路的解决好像是C++?

吃完晚饭,我又受到了以前看的cctv2的一个推价格游戏的启发,这次这个代码提交了九次终于通过了。

s = 'abcabcbb'def long(s):      k = 0      chi = ""      for i in range(0,len(s)):          if s[i] not in chi:             chi = (chi + s[i])             if k < len(chi):               k = len(chi)             print("chi is "+chi)             print("k is %d"%(k))          else:            if k < len(chi):                 k = len(chi)            chi = ""+chi[chi.find(s[i])+1:] +s[i]          print(chi)          print(k)      return(k)print("=======================")print("s is "+s)print(long(s))

别人的解决思路

由于我没有学过数据结构,所以并不知道有哈希表这个东西。然后才看了大神是怎么做的哈希表做法


总结

做不出来先停一停,整理一下思路,不要死磕,不然会很坑。 转cs之路很漫长,小白从今天开始上路了。