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

Approach #1: C++.

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
vector<int> arr_x, arr_y;
int num_x = 1, num_y = 1; if (x == 1) arr_x.push_back(1);
else for (int i = num_x; i < bound; i *= x) arr_x.push_back(i); if (y == 1) arr_y.push_back(1);
else for (int j = num_y; j < bound; j *= y) arr_y.push_back(j); set<int> temp; for (int i = 0; i < arr_x.size(); ++i) {
for (int j = 0; j < arr_y.size(); ++j) {
if (arr_x[i] + arr_y[j] <= bound) {
temp.insert(arr_x[i] + arr_y[j]);
} else {
break;
}
}
} vector<int> ans(temp.begin(), temp.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]

Approach #2: C++.

class Solution {
public:
vector<int> pancakeSort(vector<int>& A) {
vector<int> ans;
vector<int> temp(A.begin(), A.end());
while (!is_sorted(A.begin(), A.end())) {
int size = temp.size();
int pos = max_element(temp.begin(), temp.end()) - temp.begin();
if (pos != size-1) {
if (pos != 0) {
ans.push_back(pos+1);
reverse(temp.begin(), temp.begin()+pos+1);
}
ans.push_back(size);
reverse(temp.begin(), temp.begin()+size);
}
A = temp;
temp.resize(size-1);
} return ans;
}
};

  

971. 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 traversal starting from the root.  Call such a sequence of N values the voyage of the tree.

(Recall that a preorder traversal of a node means we report the current node's value, then preorder-traverse the left child, then preorder-traverse the right child.)

Our goal is to flip the least number of nodes in the tree so that the voyage of the tree matches the voyage we are given.

If we can do so, then return a list of the values of all nodes flipped.  You may return the answer in any order.

If we cannot do so, then return the list [-1].

Example 1:

Input: root = [1,2], voyage = [2,1]
Output: [-1]

Example 2:

Input: root = [1,2,3], voyage = [1,3,2]
Output: [1]

Example 3:

Input: root = [1,2,3], voyage = [1,2,3]
Output: []

Note:

  1. 1 <= N <= 100

Approach #1: C++.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> ans;
int i = 0; vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
if (dfs(root, voyage)) return ans;
ans.clear();
ans.push_back(-1);
return ans;
} bool dfs(TreeNode* node, vector<int>& voyage) {
if (node == NULL) return true;
if (node->val != voyage[i++]) return false;
if (node->left && node->left->val == voyage[i]) {
return dfs(node->left, voyage) && dfs(node->right, voyage);
} else if (node->right && node->right->val == voyage[i]) {
if (node->left)
ans.push_back(node->val);
return dfs(node->right, voyage) && dfs(node->left, voyage);
}
return !node->left && !node->right;
}
};

  

972. Equal Rational Numbers

Given two strings S and T, each of which represents a non-negative rational number, return True if and only if they represent the same number. The strings may use parentheses to denote the repeating part of the rational number.

In general a rational number can be represented using up to three parts: an integer part, a non-repeating part, and a repeating part. The number will be represented in one of the following three ways:

  • <IntegerPart> (e.g. 0, 12, 123)
  • <IntegerPart><.><NonRepeatingPart>  (e.g. 0.5, 1., 2.12, 2.0001)
  • <IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)> (e.g. 0.1(6), 0.9(9), 0.00(1212))

The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets.  For example:

1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)

Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.

Example 1:

Input: S = "0.(52)", T = "0.5(25)"
Output: true
Explanation:
Because "0.(52)" represents 0.52525252..., and "0.5(25)" represents 0.52525252525..... , the strings represent the same number.

Example 2:

Input: S = "0.1666(6)", T = "0.166(66)"
Output: true

Example 3:

Input: S = "0.9(9)", T = "1."
Output: true
Explanation:
"0.9(9)" represents 0.999999999... repeated forever, which equals 1. [See this link for an explanation.]
"1." represents the number 1, which is formed correctly: (IntegerPart) = "1" and (NonRepeatingPart) = "".

Note:

  1. Each part consists only of digits.
  2. The <IntegerPart> will not begin with 2 or more zeros.  (There is no other restriction on the digits of each part.)
  3. 1 <= <IntegerPart>.length <= 4
  4. 0 <= <NonRepeatingPart>.length <= 4
  5. 1 <= <RepeatingPart>.length <= 4

Approach #1: C++.

class Solution {
public:
bool isRationalEqual(string S, string T) {
return f(S) == f(T);
} double f(string S) {
auto i = S.find('(');
if (i == string::npos) return stod(S);
string base = S.substr(0, i);
string rep = S.substr(i+1, S.length()-i-1-1);
for (int i = 0; i < 20; ++i) {
base += rep;
}
return stod(base.substr(0, 4+1+4+12));
}
};

  

Analysis:

https://leetcode.com/problems/equal-rational-numbers/discuss/214203/C%2B%2BPython-Easy-Cheat

Weekly Contest 118的更多相关文章

  1. LeetCode Weekly Contest 118

    要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given tw ...

  2. LeetCode Weekly Contest 8

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

  3. Leetcode Weekly Contest 86

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

  4. leetcode weekly contest 43

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

  5. LeetCode Weekly Contest 23

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

  6. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  7. LeetCode Weekly Contest

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

  8. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  9. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

随机推荐

  1. Linux - 创建用户的相关文件

    创建一个用户会与 6 个文件相关 /etc/passwd 储存了所有用户的相关信息 第一行中,从左往右 root 为用户名,: 为分隔符,x 为密码,0 为 uid,0 为 gid,root 为用户的 ...

  2. Linq语句:三表联查

    var db = new DataEntities2();            var sss = ( from c in db.AIRPORT_HELIPORT                   ...

  3. 第十七章 MySQL Replication(待续)

    ··········

  4. app端内容播放,web端的玩具,app通过websocket远程遥控玩具播放内容,玩具管理页面

    一.app端内容播放 下载代码 https://github.com/987334176/Intelligent_toy/archive/v1.0.zip 注意:由于涉及到版权问题,此附件没有图片和音 ...

  5. leetcode427

    本题不会做,从网上找到了python3的解法,记录如下. class Solution: def construct(self, grid): def dfs(x, y, l): if l == 1: ...

  6. MySQL备份还原之一mydumper

    1)源码编译安装 1.下载 mydumper源码 2.解压 [mysql@localhost ~]$ tar -xvf mydumper-0.9.1.tar mydumper-0.9.1/CMakeL ...

  7. 微信小程序中遇到的wx:if问题

    最近在项目中遇到wx:if问题进行梳理一下,有个需求就是有数据的时候显示数据列表,没有数据的时候就显示‘去赚钱’的页面,这可以放在一个页面进行显示,就要用到wx:if判断.我在js中设置了一个变量sh ...

  8. SESSION的知识

    android模拟表单用到了httpclient,但是需要了解Jsessionid的相关知识 如下是从一篇博文摘抄来的 在web应用的开发中我们会经常看到这样的url:http://www.xxx.c ...

  9. codeforce 459DIV2 C题

    题意 一串括号字符串,里面存在一些‘?’,其中‘?’既可以当作 '(' 又可以当作 ')' ,计算有多少对(l,r),在s中[sl,s(l+1),s(l+2),.....sr],内的括号是匹配的.n= ...

  10. 关于PHP的一个坑爹问题(页面刷新)

    最近在用PHP做一个服务端和一个客户端,在快要完工的时候,出现了一个重大问题---- 当在客户端手动输入IP和端口的时候,一按连接,OK,连接成功,嘻嘻,就在我自以为大功告成的时候,来了个晴天霹雳,一 ...