2021-02-18:給定一個字符串str,給定一個字符串類型的數(shù)組arr,

<p>2021-02-18:給定一個字符串str命迈,給定一個字符串類型的數(shù)組arr,出現(xiàn)的字符都是小寫英文因俐。arr每一個字符串精肃,代表一張貼紙,你可以把單個字符剪開使用潜慎,目的是拼出str來捡多。返回需要至少多少張貼紙可以完成這個任務(wù)。例子:str= "babac"铐炫,arr = {"ba","c","abcd"}垒手。a + ba + c? 3? abcd + abcd 2? abcd+ba 2。所以返回2倒信。</p><p/><p>福哥答案2021-02-18:</p><p>自然智慧即可科贬。</p><p>帶記憶的遞歸。對“babac”排序鳖悠,變成“aabbc”榜掌,然后根據(jù)數(shù)組,依次消掉a乘综,然后b憎账,最后c,這是一個優(yōu)化點(diǎn)卡辰。有代碼胞皱。</p><p>代碼用golang編寫邪意,代碼如下:</p><p/><p>go</p><p>package main</p><p/><p>import (</p><p>&nbsp; &nbsp; &quot;fmt&quot;</p><p>&nbsp; &nbsp; &quot;strings&quot;</p><p>)</p><p/><p>const MAX = int(^uint(0) &gt;&gt; 1) //構(gòu)造0111111111....&nbsp; &nbsp;9223372036854775807</p><p>const MIN = int(^MAX)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //構(gòu)造10000000...&nbsp; &nbsp;-9223372036854775808</p><p>func main() {</p><p>&nbsp; &nbsp; ret := minStickers([]string{&quot;ba&quot;, &quot;c&quot;, &quot;abcd&quot;}, &quot;babac&quot;)</p><p>&nbsp; &nbsp; fmt.Println(ret)</p><p>}</p><p/><p>func minStickers(stickers []string, target string) int {</p><p>&nbsp; &nbsp; N := len(stickers)</p><p>&nbsp; &nbsp; counts := make([][]int, N)</p><p>&nbsp; &nbsp; for i := 0; i &lt; N; i++ {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; counts[i] = make([]int, 26)</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; for i := 0; i &lt; N; i++ {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; str := stickers[i]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; for _, cha := range str {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counts[i][cha-&#39;a&#39;]++</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; dp := make(map[string]int)</p><p>&nbsp; &nbsp; dp[&quot;&quot;] = 0</p><p>&nbsp; &nbsp; ans := process(counts, target, dp)</p><p>&nbsp; &nbsp; if ans == MAX {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return -1</p><p>&nbsp; &nbsp; } else {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return ans</p><p>&nbsp; &nbsp; }</p><p>}</p><p>func process(stickers [][]int, t string, dp map[string]int) int {</p><p/><p>&nbsp; &nbsp; if _, ok := dp[t]; ok {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return dp[t]</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; tcounts := make([]int, 26)</p><p>&nbsp; &nbsp; for _, cha := range t {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; tcounts[cha-&#39;a&#39;]++</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; N := len(stickers)</p><p>&nbsp; &nbsp; min := MAX</p><p>&nbsp; &nbsp; for i := 0; i &lt; N; i++ {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; sticker := stickers[i]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; if sticker[t[0]-&#39;a&#39;] &gt; 0 {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder := strings.Builder{}</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j := 0; j &lt; 26; j++ {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tcounts[j] &gt; 0 {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nums := tcounts[j] - sticker[j]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k := 0; k &lt; nums; k++ {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.WriteByte(&#39;a&#39; + byte(j))</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rest := builder.String()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = getMin(min, process(stickers, rest, dp))</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; ans := min</p><p>&nbsp; &nbsp; if min != MAX {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; ans++</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; dp[t] = ans</p><p>&nbsp; &nbsp; return ans</p><p>}</p><p>func getMin(a int, b int) int {</p><p>&nbsp; &nbsp; if a &lt; b {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return a</p><p>&nbsp; &nbsp; } else {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return b</p><p>&nbsp; &nbsp; }</p><p>}</p><p></p><p>執(zhí)行結(jié)果如下:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/22862325-054ab5fef8a193da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>***</p><p>左神java代碼</p><p>評論</p><p>
</p>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市反砌,隨后出現(xiàn)的幾起案子雾鬼,更是在濱河造成了極大的恐慌,老刑警劉巖于颖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呆贿,死亡現(xiàn)場離奇詭異,居然都是意外死亡森渐,警方通過查閱死者的電腦和手機(jī)做入,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來同衣,“玉大人竟块,你說我怎么就攤上這事∧推耄” “怎么了浪秘?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長埠况。 經(jīng)常有香客問我耸携,道長,這世上最難降的妖魔是什么辕翰? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮喜命,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘壁榕。我一直安慰自己牌里,他們只是感情好贪染,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布催享。 她就那樣靜靜地躺著票髓,像睡著了一般洽沟。 火紅的嫁衣襯著肌膚如雪踪区。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機(jī)與錄音眷细,去河邊找鬼。 笑死奔害,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的揭厚。 我是一名探鬼主播太援,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼碱蒙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤磕瓷,失蹤者是張志新(化名)和其女友劉穎翎承,沒想到半個月后甸各,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涂邀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了衣洁。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡砖第,死狀恐怖梧兼,靈堂內(nèi)的尸體忽然破棺而出羽杰,到底是詐尸還是另有隱情欲虚,我是刑警寧澤益涧,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站扭弧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏呼巴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一府瞄、第九天 我趴在偏房一處隱蔽的房頂上張望碘箍。 院中可真熱鬧遵馆,春花似錦敲街、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽复隆。三九已至,卻和暖如春挽拂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亏栈。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工宏赘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人察署。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親休吠。 傳聞我的和親對象是個殘疾皇子业簿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容