ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-8-28 每日一题-二分查找-剑指offer-字典树

2022-08-28 15:00:49  阅读:152  来源: 互联网

标签:count 2022 offer int 28 long words public String


793. 阶乘函数后 K 个零

难度困难

 f(x) 是 x! 末尾是 0 的数量。回想一下 x! = 1 * 2 * 3 * ... * x,且 0! = 1 。

  • 例如, f(3) = 0 ,因为 3! = 6 的末尾没有 0 ;而 f(11) = 2 ,因为 11!= 39916800 末端有 2 个 0 。

给定 k,找出返回能满足 f(x) = k 的非负整数 x 的数量。

 1 class Solution {
 2     public int preimageSizeFZF(int k) {
 3         // 左边界
 4         long l=0L,r=5000000000L;
 5         while (l<r){
 6             long mid=l+(r-l)/2;
 7             if (zeroGet(mid)==k){
 8                 r=mid;
 9             }else if (zeroGet(mid)>k){
10                 r=mid;
11             }else{
12                 l=mid+1;
13             }
14         }
15         long left=l;
16         // 右边界
17         l=0L;
18         r=5000000000L;
19         while (l<r){
20             long mid=l+(r-l)/2;
21             if (zeroGet(mid)==k){
22                 l=mid+1;
23             }else if (zeroGet(mid)>k){
24                 r=mid;
25             }else{
26                 l=mid+1;
27             }
28         }
29         //System.out.println(l+" "+left);
30         return (int)(l-left);
31     }
32 
33     public long zeroGet(long x){
34         long sum=0;
35         long mul=5;
36         while (mul<=x){
37             sum+=x/mul;
38             mul*=5;
39         }
40         return sum;
41     }
42 }

思路:二分查找左右边界。注意左右边界要用LONG。

 

剑指 Offer II 065. 最短的单词编码

难度中等

单词数组 words 的 有效编码 由任意助记字符串 s 和下标数组 indices 组成,且满足:

  • words.length == indices.length
  • 助记字符串 s 以 '#' 字符结尾
  • 对于每个下标 indices[i] ,s 的一个从 indices[i] 开始、到下一个 '#' 字符结束(但不包括 '#')的 子字符串 恰好与 words[i] 相等

给定一个单词数组 words ,返回成功对 words 进行编码的最小助记字符串 s 的长度 。

 

 1 class Solution {
 2     static int  ans;
 3     static int count;
 4     public int minimumLengthEncoding(String[] words) {
 5         Trie trie = new Trie();
 6         for (String word:words){
 7             String t=reverse(word);
 8             if (!trie.search(t)) trie.addWord(t);
 9         }
10         ans=0;
11         count=0;
12         dfs(0,trie);
13         return ans+count;
14     }
15     public String reverse(String w){
16         StringBuilder sb=new StringBuilder();
17         for (int i=w.length()-1;i>=0;i--){
18             sb.append(w.charAt(i));
19         }
20         return sb.toString();
21     }
22     public void dfs(int len,Trie root){
23         if (root.isWord){
24             ans+=len;
25             count++;
26             return;
27         }
28         for (int i=0;i<26;i++){
29             if (root.child[i]!=null) dfs(len+1,root.child[i]);
30         }
31     }
32 
33     public static void main(String[] args) {
34         String[] a={"t"};
35         System.out.println(new Solution().minimumLengthEncoding(a));
36     }
37 }
38 
39 class Trie{
40     Trie[] child;
41     boolean isWord;
42     Trie(){
43         isWord=false;
44         child=new Trie[26];
45     }
46 
47     public void addWord(String word){
48         Trie root=this;
49         for (int i=0;i<word.length();i++){
50             if (root.child[word.charAt(i)-'a']==null){
51                 root.child[word.charAt(i)-'a']=new Trie();
52             }
53             root.isWord=false;
54             root=root.child[word.charAt(i)-'a'];
55         }
56         root.isWord=true;
57     }
58 
59     public boolean search(String word){
60         Trie root=this;
61         for (int i=0;i<word.length();i++){
62             if (root.child[word.charAt(i)-'a']==null) return false;
63             else root=root.child[word.charAt(i)-'a'];
64         }
65         return true;
66     }
67 }

思路:倒序字典树,统计最长的单词以及数量。

标签:count,2022,offer,int,28,long,words,public,String
来源: https://www.cnblogs.com/benbicao/p/16632771.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有