<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)481:神奇字符串

          共 821字,需瀏覽 2分鐘

           ·

          2022-01-01 14:56

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?神奇字符串,我們先來看題面:
          https://leetcode-cn.com/problems/magical-string/



          神奇字符串 s 僅由 '1' 和 '2' 組成,并需要遵守下面的規(guī)則:

          神奇字符串 s 的神奇之處在于,串聯(lián)字符串中 '1' 和 '2' 的連續(xù)出現(xiàn)次數(shù)可以生成該字符串。
          s 的前幾個元素是 s = "1221121221221121122……" 。如果將 s 中連續(xù)的若干 1 和 2 進行分組,可以得到 "1 22 11 2 1 22 1 22 11 2 11 22 ......" 。每組中 1 或者 2 的出現(xiàn)次數(shù)分別是 "1 2 2 1 1 2 1 2 2 1 2 2 ......" 。上面的出現(xiàn)次數(shù)正是 s 自身。

          給你一個整數(shù) n ,返回在神奇字符串 s 的前 n 個數(shù)字中 1 的數(shù)目。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          示例 1:
          輸入:n = 6
          輸出:3
          解釋:神奇字符串 s 的前 6 個元素是 “122112”,它包含三個 1,因此返回 3 。

          示例 2:
          輸入:n = 1
          輸出:1


          解題


          解題思路:
          分為兩個字符串:1表示原始字符串,2表示分組后的字符串
          結(jié)合第一個字符串向第二個字符串中添加元素
          結(jié)合第二個字符串向第一個字符串中添加元素。

          class?Solution?{
          ????public?int?magicalString(int?n) {
          ????????if(n <= 0)
          ????????????return?0;
          ????????if(n <= 3)
          ????????????return?1;
          ????????StringBuilder base?= new?StringBuilder();
          ????????StringBuilder sub = new?StringBuilder();
          ????????base.append("122");
          ????????sub.append("12");
          ????????int?index = 0;
          ????????int?temp = 0;
          ????????int?counts = 1;
          ????????while?(base.length() < n){
          ????????????index = sub.length();
          ????????????temp = base.charAt(index)-'0';
          ????????????sub.append(temp);
          ????????????if(temp==2){
          ????????????????temp = base.charAt(base.length()-1)-'0';
          ????????????????if(temp == 2){
          ????????????????????base.append("11");
          ????????????????????if(base.length() <= n)
          ????????????????????????counts += 2;
          ????????????????????else
          ????????????????????????counts += 1;
          ????????????????}else?{
          ????????????????????base.append("22");
          ????????????????}
          ????????????}else?{//temp == 1
          ????????????????temp = base.charAt(base.length()-1)-'0';
          ????????????????if(temp == 2){
          ????????????????????base.append("1");
          ????????????????????counts += 1;
          ????????????????}else?{
          ????????????????????base.append("2");
          ????????????????}
          ????????????}
          ????????}
          ????????return?counts;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-480題匯總,希望對你有點幫助!


          瀏覽 17
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人无码精品 | 欧美激情网址 | 日韩手机在线视频 | 日韩一级黄色A片 | 青娱乐国产AV |