【leetcode】1237. Find Positive Integer Solution for a Given Equation
题目如下:
Given a function
f(x, y)and a valuez, return all positive integer pairsxandywheref(x,y) == z.The function is constantly increasing, i.e.:
f(x, y) < f(x + 1, y)f(x, y) < f(x, y + 1)The function interface is defined like this:
interface CustomFunction {
public:
// Returns positive integer f(x, y) for any given positive integer x and y.
int f(int x, int y);
};For custom testing purposes you're given an integer
function_idand a targetzas input, wherefunction_idrepresent one function from an secret internal list, on the examples you'll know only two functions from the list.You may return the solutions in any order.
Example 1:
Input: function_id = 1, z = 5
Output: [[1,4],[2,3],[3,2],[4,1]]
Explanation: function_id = 1 means that f(x, y) = x + yExample 2:
Input: function_id = 2, z = 5
Output: [[1,5],[5,1]]
Explanation: function_id = 2 means that f(x, y) = x * yConstraints:
1 <= function_id <= 91 <= z <= 100- It's guaranteed that the solutions of
f(x, y) == zwill be on the range1 <= x, y <= 1000- It's also guaranteed that
f(x, y)will fit in 32 bit signed integer if1 <= x, y <= 1000
解题思路:看到1 <= x, y <= 1000时,就可以意识到O(n^2)的复杂度是可以接受的,那么两层循环计算一下吧。
代码如下:
"""
This is the custom function interface.
You should not implement it, or speculate about its implementation
class CustomFunction:
# Returns f(x, y) for any given positive integers x and y.
# Note that f(x, y) is increasing with respect to both x and y.
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
def f(self, x, y): """
class Solution(object):
def findSolution(self, customfunction, z):
"""
:type num: int
:type z: int
:rtype: List[List[int]]
"""
res = []
for x in range(1,1001):
for y in range(1,1001):
if customfunction.f(x,y) == z:
res.append([x,y])
elif customfunction.f(x,y) > z:
break
return res
【leetcode】1237. Find Positive Integer Solution for a Given Equation的更多相关文章
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- Nexus2 yum插件RCE漏洞(CVE-2019-5475)
1.漏洞简介: 安全研究员Christian August Holm Hansen在Nexus Repository Manager 2.X中披露了一个远程命令执行漏洞. 该漏洞默认具有部署权限帐户. ...
- 布隆过滤器(Bloom Filter)原理以及应用
应用场景 主要是解决大规模数据下不需要精确过滤的场景,如检查垃圾邮件地址,爬虫URL地址去重,解决缓存穿透问题等. 布隆过滤器(Bloom Filter)是1970年由布隆提出的.它实际上是一个很长的 ...
- git 添加第二个远程仓库地址,一次修改到处上传~
上传本机git 公钥到对应的代码托管平台 github/码云等 本地仓库执行 git remote set-url --add origin https://gitee.com/qichengTech ...
- Python内置函数Object
英文文档 class object Return a new featureless object. object is a base for all classes. It has the meth ...
- Nginx linux下的安装
1.先把从官网 nginx.io下载 的安装包通过ftp传到服务器上,然后进行解压. 我的安装环境以及nginx版本 :Ubuntu16 ,nginx-1.11.3.tar.gz(经过这个尝试这个版本 ...
- 【一道来自老师的题的题解】equip——奇妙的最短路
这道题真的第一眼完全想不到是最短路啊!!!!!!!! 感谢DR大佬讲解!!!!!90°鞠躬 =u= 暂时没有评测网址,(因为需要special judge)敬请期待 机房另一大佬JYY题解,可以对比参 ...
- 树上选两点(使最短)树的直径+bfs
题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...
- 2.bash术语定义
2.术语定义POSIX:基于Unix的一系列操作系统可移植性的标准.Bash主要和POSIX标准第1003.1号中的<Shell和使用工具>有关.空白符:一个空格或者制表符.内部命令:在s ...
- spring boot jpa criteria api是如何生成JPQL的
当我们使用entityManager.createQuery(query)时,我们发现entityManager的注入对象如下: 也就是它:org.springframework.orm.jpa.Lo ...
- 预约系统(四) 管理页面框架搭建easyUI
Manage控制器用于管理页面 Index视图为管理页面首页,采用easyUi的后台管理框架 Html头部调用,jquery库,easyui库,easyui.css,icon.css,语言包 < ...