[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.

Return a list of all powerful integers that have value less than or equal to bound.

You may return the answer in any order.  In your answer, each value should occur at most once.

Example 1:

Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
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

Example 2:

Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]

Note:

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


给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 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


8ms

 1 class Solution {
 2     func powerfulIntegers(_ x: Int, _ y: Int, _ bound: Int) -> [Int] {
 3         var xs:[Int] = [1]
 4         var ys:[Int] = [1]
 5
 6         if x > 1
 7         {
 8             var p:Int = x
 9             while(p <= bound)
10             {
11                 xs.append(p)
12                 p *= x
13             }
14         }
15
16         if y > 1
17         {
18             var p:Int = y
19             while(p <= bound)
20             {
21                 ys.append(p)
22                 p *= y
23             }
24         }
25
26         var s:Set<Int> = Set<Int>()
27         for xx in xs
28         {
29             for yy in ys
30             {
31                 if xx + yy <= bound
32                 {
33                     s.insert(xx + yy)
34                 }
35             }
36         }
37         return Array(s)
38     }
39 }

原文地址:https://www.cnblogs.com/strengthen/p/10228288.html

时间: 2024-10-31 22:55:38

[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers的相关文章

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2

[Swift Weekly Contest 118]LeetCode969.煎饼排序 | Pancake Sorting

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort t

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 118]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle

Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it is impossible to form any triangle of non-zero area, return 0. Example 1: Input: [2,1,2] Output: 5 Example 2: I

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

LeetCode Weekly Contest 118

要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerfu

Weekly Contest 118

970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerful integers that have value less than or equal to bound. You may retur

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t