From b90445c79d08f1317082c4dd4d506a82884c38c9 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 21 Feb 2025 09:17:44 +0800 Subject: [PATCH] Add solution and test-cases for problem 1780 --- .../README.md | 30 +++++++++++-------- .../Solution.go | 20 +++++++++++-- .../Solution_test.go | 12 ++++---- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/README.md b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/README.md index 44bb7cce3..003d01a65 100755 --- a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/README.md +++ b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/README.md @@ -1,28 +1,32 @@ # [1780.Check if Number is a Sum of Powers of Three][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an integer `n`, return `true` if it is possible to represent `n` as the sum of distinct powers of three. Otherwise, return `false`. + +An integer `y` is a power of three if there exists an integer `x` such that `y == 3^x`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 12 +Output: true +Explanation: 12 = 31 + 32 ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Check if Number is a Sum of Powers of Three -```go +``` +Input: n = 91 +Output: true +Explanation: 91 = 30 + 32 + 34 ``` +**Example 3:** + +``` +Input: n = 21 +Output: false +``` ## 结语 diff --git a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution.go b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution.go index d115ccf5e..eaf8080db 100644 --- a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution.go +++ b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) bool { + arr := make([]int, 15) + arr[0] = 1 + for i := 1; i < 15; i++ { + arr[i] = arr[i-1] * 3 + } + var ok func(int, int) bool + ok = func(index, cur int) bool { + if cur == 0 { + return true + } + if index == 15 { + return false + } + + return ok(index+1, cur-arr[index]) || ok(index+1, cur) + } + return ok(0, n) } diff --git a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution_test.go b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution_test.go index 14ff50eb4..0f170e42a 100644 --- a/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution_test.go +++ b/leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 12, true}, + {"TestCase2", 91, true}, + {"TestCase3", 21, false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }