鱼C论坛

 找回密码
 立即注册
查看: 1624|回复: 6

[已解决]关于找无重复最长字符串的问题

[复制链接]
发表于 2022-8-16 20:31:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如图,我实在想不出来我的代码出啥问题了
最佳答案
2022-8-16 21:30:10
  1. class Solution:
  2.     def lengthOfLongestSubstring(self, s: str) -> int:
  3.         count = 0
  4.         string = ""
  5.         for i in range(len(s)):
  6.             if s[i] not in string:
  7.                 string += s[i]
  8.             else:
  9.                 count = len(string) if len(string) > count else count
  10.                 exist_index = string.find(s[i])
  11.                 string = string[(exist_index+1):] + s[i]
  12.         else:
  13.             count = len(string) if len(string) > count else count
  14.         return count
复制代码
QQ截图20220816203121.png
20220816203024.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 21:28:59 | 显示全部楼层
  1. s , m = input() . strip() , 0
  2. for i in range(len(s)):
  3.     e , n = s[i] , 1
  4.     while len(e) == len(set(e)) and i + n < len(s):
  5.         n += 1
  6.         e = e + s[i + n - 1]
  7.     if n - 1 > m :
  8.         m = n - 1
  9. print(m)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-16 21:30:10 | 显示全部楼层    本楼为最佳答案   
  1. class Solution:
  2.     def lengthOfLongestSubstring(self, s: str) -> int:
  3.         count = 0
  4.         string = ""
  5.         for i in range(len(s)):
  6.             if s[i] not in string:
  7.                 string += s[i]
  8.             else:
  9.                 count = len(string) if len(string) > count else count
  10.                 exist_index = string.find(s[i])
  11.                 string = string[(exist_index+1):] + s[i]
  12.         else:
  13.             count = len(string) if len(string) > count else count
  14.         return count
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-16 21:54:23 | 显示全部楼层

我想问一下,break能跳出嵌套if不,刚刚将break缩进提前了一层,就通过了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-16 21:56:50 | 显示全部楼层
叶墨沫 发表于 2022-8-16 21:54
我想问一下,break能跳出嵌套if不,刚刚将break缩进提前了一层,就通过了

当然可以的啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-16 22:01:04 | 显示全部楼层

抱歉抱歉,我忽略了个小细节,break放里面的if了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-16 22:47:48 | 显示全部楼层
本帖最后由 jackz007 于 2022-8-16 23:00 编辑
  1. class Solution(str):
  2.     def lengthofLongestSubstring(self):
  3.         r = 0
  4.         for i in range(len(self)):
  5.             n = 0
  6.             while len(self[i : i + n + 1]) == len(set(self[i : i + n + 1])) and i + n < len(self):
  7.                 n += 1
  8.             if r < n:
  9.                 r = n
  10.         return r

  11. s = Solution(input())
  12. print(s . lengthofLongestSubstring())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表