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 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
题目意思很清楚就不解释了。说下两个很坑的数据吧,输出结果可以任意顺序, 第一个:输入 1 1 1 输出 [1] 第二个 1 2 100 输出 [2,3,5,9,17,33,65]
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
vector<int> ans;
if( x < y ) swap(x, y);
int px = , py = ;
while( px <= bound ) {
py = ;
while( px + py <= bound ) {
ans.push_back(px+py);
py = py*y;
if( y == ) break;
}
px = px * x;
if( x == ) break;
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans;
}
};
969. 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 kelements of A
. We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A
.
Return the k-values corresponding to a sequence of pancake flips that sort A
. Any valid answer that sorts the array within 10 * A.length
flips will be judged as correct.
Example 1:
Input: [3,2,4,1]
Output: [4,2,4,3]
Explanation:
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: A = [3, 2, 4, 1]
After 1st flip (k=4): A = [1, 4, 2, 3]
After 2nd flip (k=2): A = [4, 1, 2, 3]
After 3rd flip (k=4): A = [3, 2, 1, 4]
After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.
Example 2:
Input: [1,2,3]
Output: []
Explanation: The input is already sorted, so there is no need to flip anything.
Note that other answers, such as [3, 3], would also be accepted.
Note:
1 <= A.length <= 100
A[i]
is a permutation of[1, 2, ..., A.length]
题意:规定了翻转规则是:选择一个数k将数组前面的k个数字移到数组后面。现在问你给你一个打乱顺序的数组A,你要尽量多少次这样的翻转才能将其变成一个有序的数组(1-n排列)。
思路:无论怎么翻转,最后的结果肯定是A[i]=i+1,所以直接去数组中找对应的数字然后直接按照规则翻转就好。
class Solution {
public:
vector<int> pancakeSort(vector<int>& A) {
vector<int> ans;
int n = A.size(); for (int i = n; i > ; i--) {
int index = find(A.begin(), A.end(), i) - A.begin();
ans.push_back(index + );
reverse(A.begin(), A.begin() + index + );
ans.push_back(i);
reverse(A.begin(), A.begin() + i);
} return ans;
}
};
LeetCode Weekly Contest 118的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- C++接口与实现的抽象分离
IPerson.h #ifndef I_PERSON_H_ #define I_PERSON_H_ #include <string> #include <ostream> c ...
- 使用Eureka作为springcloud的注册机
使用springcloud做项目的负载均衡,需要导的jar这里不再显示,具体配置如下: 作为被注册服务配置: 启动多台服务端就可以实现集群,相应的localhost需要转成真实的ip 当然一个项目还要 ...
- Python 学习笔记10 函数
函数其实一段带名字的代码段,我们可以根据代码段,重复执行某一段代码段,或者有条件的执行某一段代码段. 将一段代码定义成函数后,我们可以很方便的根据自己的需求,随时调用该代码段.遇到需求变化的时候,只需 ...
- JAVA 第五周学习总结
20175303 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 •使用关键字interface来定义一个接口,定义接口分包含接口声明和接口体. •接口体中包 ...
- 基础作业 本周没上课,但是请大家不要忘记学习。 本周请大家完成上周挑战作业的第一部分:给定一个整数数组(包含正负数),找到一个具有最大和的子数组,返回其最大的子数组的和。 例如:[1, -2, 3, 10, -4, 7, 2, -5]的最大子数组为[3, 10, -4, 7, 2] 输入: 请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。 输出 在不删除原有文件内容
1丶 实验代码 #include<stdio.h> int main(void) { int tt,nn,i,j,c[11][11]; int flag=1; scanf("%d ...
- Windows10 ntoskrnl.exe占用大量的磁盘空间(100%)
一.解决办法: 1.此电脑(右键)> 管理(点击)> 系统工具 > 任务计划程序 > 任务计划程序库 > Microsoft > windows > .NE ...
- python 包下载地址
https://www.lfd.uci.edu/~gohlke/pythonlibs/
- awk命令使用经验
1.为什么要使用awk 举一个简单的例子,作为一个java开发人员,在查看日志服务器(即时保存所有线上环境的日志)上的日志的时候,由于部署了服务的服务器不止一台,当想要查找某一个特定信息的时候,由于不 ...
- FreeSwitch 终端命令详细介绍
FreeSwitch版本:1.6.9 以下为部分终端命令 alias 语法: alias [add|stickyadd] <alias> <command> | del [&l ...
- android模拟器访问PC本地接口
一般来讲PC本地接口是localhost:8080 而在安卓模拟器上用的话,他会映射模拟器本身的,也就是说,可以把模拟器也当成一个PC端来看待,这样会好理解点吧 而在模拟器上想要访问PC本地的loca ...