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 ...
随机推荐
- SPFA找最大比例环
SPFA找最大比例环 ans=Σ点权/Σ边权 所以 可以变式为 Σ边权*ans-Σ点权=0 要找出最大的ans 可以二分 边权值变为 目的地点权-ans*边权 检查是否有负环 有则可以更优 #incl ...
- Unity 和android 交互 记录
参考文章 http://www.jianshu.com/p/c06063a403c6 趟坑如下 icon 冲突问题: 设置不了unity icon,显示的是默认的 android 小人 解决方法: 在 ...
- Django之JWT理解及简单应用
Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(S ...
- 初入 vue
基于 vue.js 的前端开发环境,用于前后端分离后的单页应用开发. 搭建 vue 项目 按官方指引,使用 vue-cli 搭建 vue 的项目. # 安装依赖库,建议指定 vue 和 element ...
- win7下编译Microsoft版的caffe包的MATLAB接口(CPU模式)
本博客是基于http://www.cnblogs.com/njust-ycc/p/5776286.html这篇博客修改的,做出了更正与补充. 本人机器的环境:Win7+MATLAB2014b+VS20 ...
- webservice学习教程(二)--理论
一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...
- plsql导入.dmp, .sql步骤
plsql导入.sql和.dmp文件时,会经常用到,对于初学者来说可能没有那么简单,毕竟oracle数据库比较麻烦. 下面是我自己导入.sql和.dmp文件的步骤. 1.导入.sql文件(sql文件是 ...
- vi光标移动
1.上下左右移动 k :上移一行 j :下移一行 h :左移一行 l :右移一行 2.移到当前屏幕的首.中.尾部 H :移到当前屏幕的首部 M :移到当前屏幕的中部 L :移到当前屏幕的尾部 ...
- C语言第02次作业--循环结构
1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 1- 经过这两周的学习,我深切地体会C语言非常的难(对于我而言).大部分情况都是题目不理解和没有思路,或者编译 ...
- Windows使用tail命令跟踪日志
我们知道如果是Unix/Linux环境可以直接使用 tail -f xxx.log即可. 但是Windows并没有自带这个命令,不过从网上可以找到tail.zip 实测可以将其解压放在C:\Windo ...