Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 846 Bytes

1237.找出给定方程的正整数解.md

File metadata and controls

35 lines (30 loc) · 846 Bytes

1237.找出给定方程的正整数解

/*
 * @lc app=leetcode.cn id=1237 lang=typescript
 *
 * [1237] 找出给定方程的正整数解
 */

// @lc code=start
/**
 * // This is the CustomFunction's API interface.
 * // You should not implement it, or speculate about its implementation
 * class CustomFunction {
 *      f(x: number, y: number): number {}
 * }
 */

function findSolution(customfunction: CustomFunction, z: number): number[][] {}
// @lc code=end

解法 1: 双指针

function findSolution(customfunction: CustomFunction, z: number): number[][] {
  const res: [number, number][] = []
  const check = (x: number, y: number) => customfunction.f(x, y)
  for (let i = 1, j = 1000; i <= 1000 && j; i++) {
    while (j && check(i, j) > z) j--
    if (j && check(i, j) === z) res.push([i, j])
  }
  return res
}