leetcode 1342. Number of Steps to Reduce a Number to Zero

Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.

Example 1:

Input: num = 14
Output: 6
Explanation: 
Step 1) 14 is even; divide by 2 and obtain 7. 
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3. 
Step 4) 3 is odd; subtract 1 and obtain 2. 
Step 5) 2 is even; divide by 2 and obtain 1. 
Step 6) 1 is odd; subtract 1 and obtain 0.

Example 2:

Input: num = 8
Output: 4
Explanation: 
Step 1) 8 is even; divide by 2 and obtain 4. 
Step 2) 4 is even; divide by 2 and obtain 2. 
Step 3) 2 is even; divide by 2 and obtain 1. 
Step 4) 1 is odd; subtract 1 and obtain 0.

Example 3:

Input: num = 123
Output: 12

Constraints:

  • 0 <= num <= 10^6

题目大意:给定一个非负整数num,返回将其变为0的步数。如果当前的数是偶数,将其除2,否则将其减去1.

思路:直接模拟,为了提高效率,除2用位运算。

C++代码1:

 1 class Solution {
 2 public:
 3     int numberOfSteps (int num) {
 4         int cnt = 0;
 5         while (num != 0) {
 6             if (num & 1) //为奇数
 7                 num--;
 8             else
 9                 num >>= 1;
10             cnt++;
11         }
12         return cnt;
13     }
14 };

C++代码二:

一般条件下,如果一个数是奇数,我们将其减去1后,下一步肯定除2,(如(5 - 1)/2=2,用了两步),而利用位运算,可以直接右移一位,会产生两步的效果:5 >> 1 = 2.

只要整数num一直减小,在变成0之前,肯定会变成1. 当数为1时,步数会多算1.

class Solution {
public:
    int numberOfSteps (int num) {
        int cnt = 0;
        while (num != 0) {
            cnt += (num & 1) ? 2 : 1;
            num >>= 1;
        }
        return cnt - 1;
    }
};

python3代码:

1 class Solution:
2     def numberOfSteps (self, num: int) -> int:
3         cnt = 0
4         while num != 0:
5             num, cnt = num - 1 if num % 2 else num // 2, cnt + 1
6         return cnt

原文地址:https://www.cnblogs.com/qinduanyinghua/p/12299781.html

时间: 2024-11-13 12:35:14

leetcode 1342. Number of Steps to Reduce a Number to Zero的相关文章

【leetcode】1342. Number of Steps to Reduce a Number to Zero

题目如下: Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. Example 1: Input: num = 14 Output: 6 Explanation:  Step 1)

1342. Number of Steps to Reduce a Number to Zero

Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. Example 1: Input: num = 14 Output: 6 Explanation:  Step 1) 14 is

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

sdut2623--The number of steps(概率dp第一弹)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

[2013山东ACM]省赛 The number of steps (可能DP,数学期望)

The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary

sdut2623——The number of steps

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点