Cool说丨970与720

简介: 970. 强整数720. 词典中最长的单词

970. 强整数 看的答案,豁然开朗

给定两个正整数 xy,如果某一整数等于 x^i + y^j,其中整数 i >= 0j >= 0,那么我们认为该整数是一个强整数

返回值小于或等于 bound 的所有强整数组成的列表。

你可以按任何顺序返回答案。在你的回答中,每个值最多出现一次。


示例 1:

输入:x = 2, y = 3, bound = 10

输出:[2,3,4,5,7,9,10]

解释:

2 = 2^0 + 3^0

3 = 2^1 + 3^0

4 = 2^0 + 3^1

5 = 2^1 + 3^1

7 = 2^2 + 3^1

9 = 2^3 + 3^0

10 = 2^0 + 3^2

示例 2:

输入:x = 3, y = 5, bound = 15

输出:[2,4,6,8,10,14]


提示:

  • 1 <= x <= 100
  • 1 <= y <= 100
  • 0 <= bound <= 10^6

第一版,暴力法,加个上限20

执行用时 :4 ms, 在所有 cpp 提交中击败了80.38%的用户

内存消耗 :8.5 MB, 在所有 cpp 提交中击败了83.33%的用户

这里用unordered_set就可以了,不需要set,说了可以返回无顺序的

   vector<int>powerfulIntegers(intx, inty, intbound) {

   unordered_set<int>res;

   for (inti=0; i<20&&pow(x, i) <=bound; i++) {

       for (intj=0; j<20&&pow(y, j) <=bound; j++) {

           intv=int(pow(x, i)) +int(pow(y, j));

           if (v<=bound) res.insert(v);

       }

   }

   vector<int>res2;

   res2.assign(res.begin(), res.end());

   returnres2;

       

   }

720. 词典中最长的单词

给出一个字符串数组words组成的一本英语词典。从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成。若其中有多个可行的答案,则返回答案中字典序最小的单词。

若无答案,则返回空字符串。

示例 1:

输入:

words = ["w","wo","wor","worl", "world"]

输出: "world"

解释:

单词"world"可由"w", "wo", "wor", 和 "worl"添加一个字母组成。

示例 2:

输入:

words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]

输出: "apple"

解释:

"apply"和"apple"都能由词典中的单词组成。但是"apple"得字典序小于"apply"。

注意:

  • 所有输入的字符串都只包含小写字母。
  • words数组长度范围为[1,1000]
  • words[i]的长度范围为[1,30]

第一版,看了提示,其实并没有什么窍门

执行用时 :44 ms, 在所有 cpp 提交中击败了99.73%的用户

内存消耗 :16.2 MB, 在所有 cpp 提交中击败了89.41%的用户

boolcompareSize(conststring&a, conststring&b) {

   if(a.size()!=b.size())

       returna.size() <b.size();

   else

   {

       returna>b;//当size一样时,字典序小的在后面,这一点很厉害

   }

}

stringlongestWord(vector<string>&words) {

   sort(words.begin(), words.end(), compareSize);

   unordered_set<string>unst;

   for (auto&a : words) {

       unst.insert(a);

   }

   stringresult;

   for (inti=words.size()-1; i>=0;--i) {//从后往前走

       result=words[i];

       intlen=result.size();

       while (len--) {

           result.pop_back();

           if (unst.find(result) ==unst.end()) break;

       }

       if (len==0) {

           result=words[i];

           break;

       }

   }

   returnresult;

}


目录
相关文章
|
存储 关系型数据库 Java
数据COOL谈第3期
本文整理自阿里巴巴大淘宝技术部双12队长朱成(锡泽),阿里巴巴业务平台双11队长徐培德(裴度),阿里巴巴数据库双11队长陈锦赋(智盛),InfoQ主编王一鹏,在数据COOL谈第3期的分享。
|
人工智能 BI C#
Cool说丨884与1207
[884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) [1207. 独一无二的出现次数](https://leetcode-cn.com/problems/unique-number-of-occurrences/)
91 0
|
C# C++
Cool说丨717与674
717. 1比特与2比特字符 674. 最长连续递增序列
69 0
|
C# C++
Cool说丨819
819. 最常见的单词
91 0
|
C# C++
Cool说丨力扣290与645
[290. 单词规律](https://leetcode-cn.com/problems/word-pattern/) [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/)
63 0
|
C# C++
Cool说丨力扣367
367. 有效的完全平方数
173 0
|
人工智能 BI C#
Cool说丨力扣67与859
67. 二进制求和 859. 亲密字符串
75 0
|
算法 C# C++
Cool说丨力扣414与581
414. 第三大的数 581. 最短无序连续子数组
75 0
|
C# C++
Cool说丨力扣686、680与434
686. 重复叠加字符串匹配 680. 验证回文字符串 Ⅱ 434. 字符串中的单词数
89 0
|
C# C++
Cool说丨力扣13
[13. 罗马数字转整数](https://leetcode-cn.com/problems/roman-to-integer/)
60 0