633. 平方数之和
代码
javascript
// 633. 平方数之和:https://leetcode.cn/problems/sum-of-square-numbers/description/
// 输入:5
// 输出:true
// 解释:1 * 1 + 2 * 2 = 5
// 枚举
export function judgeSquareSum (c) {
const sqrt = Math.floor(Math.sqrt(c))
for (let a = 0; a <= sqrt; a++) {
const b = Math.floor(Math.sqrt(c - a * a))
if (a * a + b * b === c) return true
}
return false
}
// 双指针
export function judgeSquareSum1 (c) {
let a = 0
let b = Math.floor(Math.sqrt(c))
while (a <= b) {
const sum = a * a + b * b
if (sum === c) {
return true
} else if (sum > c) {
b--
} else {
a++
}
}
return false
}
typescript
// 633. 平方数之和:https://leetcode.cn/problems/sum-of-square-numbers/description/
// 输入:5
// 输出:true
// 解释:1 * 1 + 2 * 2 = 5
// 枚举
export function judgeSquareSum (c: number): boolean {
const sqrt = Math.floor(Math.sqrt(c))
for (let a = 0; a <= sqrt; a++) {
const b = Math.floor(Math.sqrt(c - a * a))
if (a * a + b * b === c) return true
}
return false
}
// 双指针
export function judgeSquareSum1 (c: number): boolean {
let a = 0
let b = Math.floor(Math.sqrt(c))
while (a <= b) {
const sum = a * a + b * b
if (sum === c) {
return true
} else if (sum > c) {
b--
} else {
a++
}
}
return false
}
测试代码
ts
import { expect, test } from 'vitest'
import { judgeSquareSum, judgeSquareSum1 } from './typescript.ts'
import { judgeSquareSum as judgeSquareSumJs, judgeSquareSum1 as judgeSquareSum1Js } from './javascript.js'
test(`judgeSquareSum`, () => {
expect(judgeSquareSum(5)).toBe(true)
expect(judgeSquareSum(3)).toBe(false)
})
test(`judgeSquareSum1`, () => {
expect(judgeSquareSum1(5)).toBe(true)
expect(judgeSquareSum1(3)).toBe(false)
})
test(`judgeSquareSumJs`, () => {
expect(judgeSquareSumJs(5)).toBe(true)
expect(judgeSquareSumJs(3)).toBe(false)
})
test(`judgeSquareSum1Js`, () => {
expect(judgeSquareSum1Js(5)).toBe(true)
expect(judgeSquareSum1Js(3)).toBe(false)
})