Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SOLVED.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
| 1390 | [Four Divisors](https://leetcode.com/problems/four-divisors/) | [Code](src/leetcode/problems/four-divisors.cpp) / [Test](test/leetcode/problems/four-divisors.cpp) |
| 1391 | [Check if There is a Valid Path in a Grid](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/) | [Code](src/leetcode/problems/check-if-there-is-a-valid-path-in-a-grid.cpp) / [Test](test/leetcode/problems/check-if-there-is-a-valid-path-in-a-grid.cpp) |
| 1400 | [Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Code](src/leetcode/problems/construct-k-palindrome-strings.cpp) / [Test](test/leetcode/problems/construct-k-palindrome-strings.cpp) |
| 1404 | [Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/) | [Code](src/leetcode/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp) / [Test](test/leetcode/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp) |
| 1411 | [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/) | [Code](src/leetcode/problems/number-of-ways-to-paint-n-3-grid.cpp) / [Test](test/leetcode/problems/number-of-ways-to-paint-n-3-grid.cpp) |
| 1417 | [Reformat The String](https://leetcode.com/problems/reformat-the-string/) | [Code](src/leetcode/problems/reformat-the-string.cpp) / [Test](test/leetcode/problems/reformat-the-string.cpp) |
| 1418 | [Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/) | [Code](src/leetcode/problems/display-table-of-food-orders-in-a-restaurant.cpp) / [Test](test/leetcode/problems/display-table-of-food-orders-in-a-restaurant.cpp) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "leetcode/core.h"

namespace leetcode {
namespace problem_1404 {

using Func = std::function<int(string)>;

class NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution : public SolutionBase<Func> {
public:
//! 1404. Number of Steps to Reduce a Number in Binary Representation to One
//! https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
int numSteps(string s);

NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution();
};

} // namespace problem_1404
} // namespace leetcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "leetcode/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.h"

namespace leetcode {
namespace problem_1404 {

// 模拟二进制操作
// 从字符串末尾开始处理,维护进位
// 时间复杂度: O(n), 空间复杂度: O(1)
static int solution1(string s) {
int steps = 0;
int carry = 0;
int n = s.size();

// 从后往前处理,除了最高位(s[0])
for (int i = n - 1; i > 0; --i) {
int bit = (s[i] - '0') + carry;
if (bit % 2 == 0) {
// 偶数:除以 2,步骤 +1
steps++;
} else {
// 奇数:加 1,步骤 +2(加1需要1步,除2需要1步)
// 加1会产生进位
steps += 2;
carry = 1;
}
}

// 处理最高位
int lastBit = (s[0] - '0') + carry;
if (lastBit == 2) {
// 最高位 1 + carry 1 = 2 (二进制 10)
// 还需要一步除以 2 变成 1
steps++;
}
// lastBit == 1 时,已经是 1,不需要额外步骤

return steps;
}

NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution::NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution() {
setMetaInfo({.id = 1404,
.title = "Number of Steps to Reduce a Number in Binary Representation to One",
.url = "https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/"});
registerStrategy("Simulation", solution1);
}

int NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution::numSteps(string s) {
return getSolution()(s);
}

} // namespace problem_1404
} // namespace leetcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "leetcode/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.h"

#include "gtest/gtest.h"

namespace leetcode {
namespace problem_1404 {

class NumberOfStepsToReduceANumberInBinaryRepresentationToOneTest : public ::testing::TestWithParam<string> {
protected:
void SetUp() override { solution.setStrategy(GetParam()); }

NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution solution;
};

TEST_P(NumberOfStepsToReduceANumberInBinaryRepresentationToOneTest, Example1) {
string s = "1101";
int expected = 6;
EXPECT_EQ(expected, solution.numSteps(s));
}

TEST_P(NumberOfStepsToReduceANumberInBinaryRepresentationToOneTest, Example2) {
string s = "10";
int expected = 1;
EXPECT_EQ(expected, solution.numSteps(s));
}

TEST_P(NumberOfStepsToReduceANumberInBinaryRepresentationToOneTest, Example3) {
string s = "1";
int expected = 0;
EXPECT_EQ(expected, solution.numSteps(s));
}

INSTANTIATE_TEST_SUITE_P(
LeetCode, NumberOfStepsToReduceANumberInBinaryRepresentationToOneTest,
::testing::ValuesIn(NumberOfStepsToReduceANumberInBinaryRepresentationToOneSolution().getStrategyNames()));

} // namespace problem_1404
} // namespace leetcode