最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 2767. Partition String Into Minimum Beautiful Substring

2023-07-09 09:27 作者:您是打尖兒還是住店呢  | 我要投稿

Given a binary string?s, partition the string into one or more?substrings?such that each substring is?beautiful.

A string is?beautiful?if:

  • It doesn't contain leading zeros.

  • It's the?binary?representation of a number that is a power of?5.

Return?the?minimum?number of substrings in such partition.?If it is impossible to partition the string?s?into beautiful substrings,?return?-1.

A?substring?is a contiguous sequence of characters in a string.

?

Example 1:

Input: s = "1011"

Output: 2

Explanation:

We can paritition the given string into ["101", "1"].?

- The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5.?

- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1.?

It can be shown that 2 is the minimum number of beautiful substrings that s can be?partitioned into.

Example 2:

Input: s = "111"Output: 3

Explanation:?

We can paritition the given string into ["1", "1", "1"].

?- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1.?

It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into.

Example 3:

Input: s = "0"

Output: -1

Explanation: We can not partition the given string into beautiful substrings.

?

Constraints:

  • 1 <= s.length <= 15

  • s[i]?is either?'0'?or?'1'.

Hide Hint 1

To check if number x is a power of 5 or not, we will divide x by 5 while x > 1 and x mod 5 == 0. After iteration if x == 1, then it was a power of 5.

Hide Hint 2

Since the constraint of s.length is small, we can use recursion to find all the partitions.

--------------------感覺是個(gè)經(jīng)典題目;

1:先寫一個(gè)函數(shù)判斷是否是5的冪,

2:然后去遍歷字符串,先從最長的開始,當(dāng)前數(shù)是5的冪,那么就可以去處理剩下的字符串。

剩下的字符串如果不含前導(dǎo)0,那么就返回1+當(dāng)前函數(shù)處理的字符串的長度,返回即可;

遞歸無止境,還是多學(xué)習(xí);

Runtime:?4 ms, faster than?25.00%?of?Java?online submissions for?Partition String Into Minimum Beautiful Substrings.

Memory Usage:?42 MB, less than?25.00%?of?Java?online submissions for?Partition String Into Minimum Beautiful Substrings.


LeetCode 2767. Partition String Into Minimum Beautiful Substring的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
普兰县| 郑州市| 农安县| 天津市| 新晃| 福州市| 龙胜| 盐池县| 资讯 | 湘潭市| 巴塘县| 新乡市| 海口市| 彭山县| 疏附县| 延川县| 上饶县| 朔州市| 吴堡县| 辛集市| 姚安县| 昌吉市| 通榆县| 永寿县| 渭源县| 增城市| 磐安县| 轮台县| 神池县| 兴海县| 新泰市| 探索| 荣昌县| 云浮市| 绥棱县| 班戈县| 龙门县| 科技| 南城县| 边坝县| 来凤县|