要死要死,第一题竟然错误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.lengthflips 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. 1 <= A.length <= 100
  2. 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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. 配置ssh框架启动tomcat服务器报异常Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

    在Spring中配置jdbc时,引用的是dbcp.jar包,在db.properties配置文件中,使用了之前的properties配置文件的用户名username(MySql用户名) 然后在启动服务 ...

  2. 如何将两个PDF文件合并到一个页面中

    在目前职场办公中,很多使用的文件格式是PDF文件格式,由于工作的需要,经常需要将PDF文件合并在一起,但由于PDF文件不能直接编辑修改,不能OFFICE,WPS那样,通过复制粘贴将两者合并,那如何解决 ...

  3. 测试自动化学习3-python3简单操作

    1.列表操作 增 stu = []stus.append('lili') #在list的末尾增加一个元素 stus.insert(9,'yjk') #在指定的位置插入元素, 查 print('单个取, ...

  4. mac下git安装与使用

    1.下载git客户端,下载地址为:https://git-scm.com/download/mac 2.打开安装包,可以看到此时的界面为:   我们需要把.pkg的安装包安装到系统当中.我双击了安装包 ...

  5. VMware+CentOS7学习记录

    CentOS命令记录 1.su root 进入最高权限 2.cd  /位置       即进入该文件 3.中文与英文之间的切换:win+空格 添加中文的步骤:https://blog.csdn.net ...

  6. odoo 响应下载文件

    odoo中如何实现点击按钮下载文件报告.报表到页面左下角 models.py # -*- coding: utf-8 -*-from openerp import models, fields, ap ...

  7. 队列->队列的表示和实现

    文字描述 队列是和栈相反,队列是一种先进先出(first in first out,缩写FIFO)的线性表,它只允许在表的一端进行插入,而在另一端进行删除.和生活中的排队相似,最早进入队列的元素最早离 ...

  8. Linux_磁盘分布_以及分区

    运用 Xshell  工具链接到你的服务器 1.     Fdisk -l    这是查看磁盘挂载列表情况 2.      Fdisk /dev/vdc   这是分区这个磁盘   m    是查看信息 ...

  9. Python reverse()方法--list

    描述 reverse()方法:用于反转列表元素的排列顺序. 语法 语法格式:list.reverse() 参数 NA 返回值 无返回值 实例 #!/usr/bin/python3 a = ['abc' ...

  10. Spring Boot(三):AOP&日志操作&异常处理

    一.AOP:HttpAspect.java 二.操作日志 在HttpAspect.java中调用org.slf4j.Logger.org.slf4j.LoggerFactory 三.异常处理 1.定义 ...