【leetcode】1237. Find Positive Integer Solution for a Given Equation
题目如下:
Given a function
f(x, y)
and a valuez
, return all positive integer pairsx
andy
wheref(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_id
and a targetz
as input, wherefunction_id
represent 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 <= 9
1 <= z <= 100
- It's guaranteed that the solutions of
f(x, y) == z
will 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 ...
随机推荐
- 【VS开发】关于SEH的简单总结
尽管以前写过一篇SEH相关的文章<关于SEH的简单总结>, 但那真的只是皮毛,一直对Windows异常处理的原理似懂非懂, 看了下面的文章 ,一切都豁然开朗. 1997年文章,Windo ...
- Spring @Import注解 —— 导入资源
在应用中,有时没有把某个类注入到IOC容器中,但在运用的时候需要获取该类对应的bean,此时就需要用到@Import注解.示例如下: 先创建两个类,不用注解注入到IOC容器中,在应用的时候在导入到当前 ...
- zk安装管理
参考: https://www.cnblogs.com/yinzhengjie/p/9209319.html 10.52.110.48 bi-kafka-310.52.48.92 bi-kafka-1 ...
- 【有奖征集】报表模板库邀您提反馈,轻松赢取P30!
>>立即参赛 赛事初衷 大数据时代,数据的价值愈发彰显,什么样的报表才能真正帮助业务决策?这几乎是所有信息化建设的企业和个人都在思考的问题. 作为报表领域标杆企业,葡萄城于2017年推出了 ...
- 客户A数据统计
-------------------------------------------------- --数据准备 /*将数据调入临时表,对advalues进行计算,并将月份更新到字段int1 */ ...
- AcWing175电路维修
这是一道在luogu的蓝题,在yxc大佬的讲解下AC掉了(百般调试) 首先这道题给了一个字符串矩阵,/ \表示相连哪两个节点,只可以走/ \所连接的两个点,但我们可以旋转每一个边,询问从1,1 走到 ...
- HDU 3416 Marriage Match IV (最短路建图+最大流)
(点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...
- python-day42(正式学习)
目录 数据库 卸载 安装 连接数据库 用户信息查看 数据库的基本操作 表的基本操作 记录的基本操作 复习 今日内容 数据库配置 数据库修改信息 用户操作:重点 表的修改 创建表的完整语法 数据库表的引 ...
- JDK 监控和故障处理工具总结 (转)
出处: JDK 监控和故障处理工具总结 JDK 监控和故障处理工具总结 JDK 命令行工具 jps:查看所有 Java 进程 jstat: 监视虚拟机各种运行状态信息 jinfo: 实时地查看和调整 ...
- Windows 窗体消息大全(速查)
Windows窗口消息大全,全不全自己撸 通用窗口消息 WM_NULL:--------->空消息,可检测程序是否有响应等 WM_CREATE:--------->新建一个窗口 WM_DE ...