1079. 活字印刷
生活随笔
收集整理的這篇文章主要介紹了
1079. 活字印刷
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
你有一套活字字模?tiles,其中每個字模上都刻有一個字母?tiles[i]。返回你可以印出的非空字母序列的數目。
注意:本題中,每個活字字模只能使用一次。
示例 1:
輸入:"AAB"
輸出:8
解釋:可能的序列為 "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA"。
示例 2:
輸入:"AAABBC"
輸出:188
提示:
思路:用hashmap來存儲每個字符的個數,然后依次遍歷
class Solution {int num;public int numTilePossibilities(String tiles) {if(tiles == null || tiles.length() <= 0) return 0;char ch[] = tiles.toCharArray();num = 0;HashMap<Character,Integer> hm = new HashMap<>();for(char c : ch) {if(hm == null || !hm.containsKey(c)) hm.put(c,1);else hm.put(c,hm.get(c)+1);}help(ch,hm);return num;}private void help(char[] ch,HashMap<Character,Integer> hm){for(Map.Entry<Character, Integer> c : hm.entrySet()){if(c.getValue() <=0) continue;else {num++;hm.put(c.getKey(),c.getValue()-1);help(ch,hm);hm.put(c.getKey(),c.getValue()+1);}}} }?
總結
以上是生活随笔為你收集整理的1079. 活字印刷的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统可行性研究
- 下一篇: 计算机音乐文本制作,《计算机音乐制作》课