鱼C论坛

 找回密码
 立即注册
查看: 1588|回复: 0

[学习笔记] 763_(划分字母区间)Partition Labels

[复制链接]
发表于 2019-7-18 05:24:58 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Seawolf 于 2019-7-18 05:27 编辑

763_(划分字母区间)Partition Labels

1 问题描述、输入输出与样例

1.1 问题描述

字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段。

返回一个表示每个字符串片段的长度的列表。

注意:

S的长度在[1, 500]之间。
S只包含小写字母'a'到'z'。
1.2 输入与输出

输入:

string S:带划分的字符串 S
输出:

vector< int>:划分为尽可能多的片段, 每个字符串片段的长度的列表
1.3 样例

1.3.1 样例1

输入: S = "ababcbacadefegdehijhklij"

输出: [9,7,8]

解释:划分结果为 "ababcbaca", "defegde", "hijhklij"。

每个字母最多出现在一个片段中。像 "ababcbacadefegde", "hijhklij" 的划分是错误的,因为划分的片段数较少。


  1. S = "ababcbacadefegdehijhklij"

  2. lst = []

  3. memo = []

  4. memo1 = []

  5. for i in S:

  6.     lst.append(i)

  7. def get_head(L):

  8.     if L == []:

  9.         return []

  10.     else:

  11.         return L[0]

  12. def get_tail(L):


  13.     if L ==[]:

  14.         return []

  15.     else:
  16.         return L[1:]

  17. def member(X,L):

  18.     if L ==[]:
  19.         return False

  20.     if X == get_head(L):

  21.         return True
  22.     else:

  23.         return member(X,get_tail(L))

  24. def length(L):

  25.     if L == []:

  26.         return 0

  27.     else:
  28.         return 1 + length(get_tail(L))

  29. def split(L):

  30.     len = length(L)

  31.     for i in range(len):

  32.         start = 100

  33.         end = 0

  34.         temp = L[i]

  35.         for j in range(len):

  36.             if j < start and L[i] == L[j]:

  37.                 start = j

  38.             if member(temp, L[j:]):

  39.                 if j > end:

  40.                     end = j

  41.         memo.append([start,end])

  42. def print_all():

  43.     for i in range(length(memo)):

  44.         small = memo[i][0]

  45.         big = memo[i][1]

  46.         for j in range(length(memo)):

  47.             if not (memo[j][1]< small or memo[j][0] > big):

  48.                 if memo[j][0] < small:

  49.                     small =  memo[j][0]

  50.                 elif memo[j][1] > big:

  51.                     big = memo[j][1]

  52.         if [small,big+1] not in memo1:

  53.             memo1.append([small,big+1])


  54.     for i in range(length(memo1)):

  55.         print(S[memo1[i][0]:memo1[i][1]])


  56. split(lst)
  57. print_all()
复制代码


output:

  1. ababcbaca
  2. defegde
  3. hijhklij
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 01:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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