【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 ...
随机推荐
- HDWIKI6.0后台任意文件删除/下载
- 使用Python过程中遇到的一些坑及其解决方法(持续更新)
1.列表不能直接赋值 nums1 = nums2 x nums1[:] = nums2 正确 2.返回列表某一元素的值可以使用index函数 aList = [123, 'xyz', 'runoob' ...
- 用docker 跑 logstash节点 - kibana界面汉化
0.docker中logstash的配置文件 [root@VM_0_6_centos pipeline]# cat logstash.yml #http.host: "0.0.0.0&quo ...
- VS显示代码行号
工具>文本编辑器>常规 如图勾选行号选项,点击确定即可显示行号. 如果没有行号的选项,则需先勾选下图中左下角的‘’显示所有设置选‘’‘项.
- ssh远程连接的故障排查详解
排查故障: 1.两个机器之间是否通畅,看物理网络(网线网卡,IP是不是正确) ping ip -t 来检测物理网络是否通畅 通 不通 不通: 1.客户端到服务器端物理链路有问题 网卡 ,IP , 网 ...
- [转] c++加载外部库文件探究
首先介绍:用#import导入dll和用#pragma comment导入lib还有在程序中LoadLibrary加载dll有什么区别 (1) #import导入的dll是com组建的dll,主要用来 ...
- 【Linux 网络编程】REUSADDR
(1)服务器端尽可能使用REUSEADDR.(2)在绑定之前尽可能调用setsockopt来设置REUSEADDR套接字选项.(3)使用REUSEADDR选项可以使得不必等待TIME_WAIT状态消失 ...
- ZooKeeper原理及介绍
Zookeeper简介 1.1 什么是Zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是大数据生态中的重要组件.它是 ...
- java web 二维码生成
pom支持: <!-- 二维码支持包 start--> <dependency> <groupId>com.google.zxing</groupId> ...
- js判断设备,跳转app应用、android市场或者AppStore
js移动设备判断方法大全 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" > ...