【LeetCode】397. Integer Replacement 解题报告(Python)
【LeetCode】397. Integer Replacement 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/integer-replacement/description/
题目描述:
Given a positive integer n and you can do operations as follow:
- If n is even, replace n with
n/2. - If n is odd, you can replace n with either
n + 1orn - 1.
What is the minimum number of replacements needed for n to become 1?
Example 1:
Input:
8
Output:
3
Explanation:
8 -> 4 -> 2 -> 1
Example 2:
Input:
7
Output:
4
Explanation:
7 -> 8 -> 4 -> 2 -> 1
or
7 -> 6 -> 3 -> 2 -> 1
题目大意
给了n,如果是是偶数对其/2,如果是奇数可以对其+1或-1操作,求最后将其化为1需要多少步。
解题方法
方法一:
递归。不过速度不快。
class Solution(object):
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1: return 0
if n % 2 == 0:
return 1 + self.integerReplacement(n / 2)
else:
return 1 + min(self.integerReplacement(n + 1), self.integerReplacement(n - 1))
方法二:
位运算。
当n是奇数的时候,如何决定应该加1还是减1?我们可以看这个数字的二进制。奇数的二进制一定是01或11结尾。同时,发现如果把一个奇数化为4的倍数,变成1的步骤会更少(3除外)。
15 -> 16 -> 8 -> 4 -> 2 -> 1
15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
那么,如果结尾是01,那么应该对其-1;如果结尾是11,那么应该对其+1;
如果这个数字是3,需要对其-1。
直接迭代求解,速度很快。
代码:
class Solution(object):
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
count = 0
while n > 1:
count += 1
if n & 1:#奇数
if n & 2 and n != 3:#尾号是11
n += 1
else:#尾号是01
n -= 1
else:#偶数
n >>= 1
return count
方法二:
日期
2018 年 3 月 9 日
【LeetCode】397. Integer Replacement 解题报告(Python)的更多相关文章
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- Linux服务器I/O性能分析-1
一.IOSTAT误区 1.1 误区-svctm Linux上的svctm是重要的I/O指标(I/O平均服务时间-单位毫秒),这个值直接反映了硬件的性能(I/O请求从SCSI层发出--->I/O完 ...
- IO流中的字符输入输出流及try...catch处理流处理中的异常
使用字节流读取中文的问题 import java.io.FileInputStream; import java.io.IOException; /* 使用字节流读取中文文件 1个中文 GBK:占用两 ...
- 从零构建Java项目(Maven+SpringBoot+Git) #02 奥斯丁项目
前两天我说要写个项目来持续迭代,有好多小伙伴都表示支持和鼓励,项目的第一篇这不就来了么~我给项目取了个名字,英文名叫做:austin,中文名叫做:奥斯丁 名字倒没有什么特别的含义,我单纯觉得这个名字好 ...
- linux 实用指令文件目录类
目录 linux实用指令文件目录类 路径 pwd指令 cd指令 操作文件夹/文件 ls指令 mkdir rmdir touch cp(重要) rm mv 操作内容 cat more less > ...
- React 16.13.1触发两次render
一段很普通的代码,出发了两次render import React, { useState, useEffect } from 'react' const MouseTracker: React.FC ...
- Spark(十)【RDD的读取和保存】
目录 一.文件类型 1.Text文件 2.Json文件 3.对象文件 4.Sequence文件 二.文件系统 1. MySQL 2. Hbase 一.文件类型 1.Text文件 读写 读取 scala ...
- 单元测试(Jest 和 Mocha)
Vue CLI 拥有通过 Jest 或 Mocha 进行单元测试的内置选项. Jest 是功能最全的测试运行器.它所需的配置是最少的,默认安装了 JSDOM,内置断言且命令行的用户体验非常好.不过你需 ...
- 【Linux】【Shell】【Basic】条件测试和变量
bash脚本编程 脚本文件格式: 第一行,顶格:#!/bin/bash 注释信息:# 代码注释: 缩进,适度添加空白行: ...
- SpringMVC中@RestController和@Controller的区别
在使用SpringMVC时,对于Controller中的注解@RestController和@Controller需要我们区分清楚 @RestController注解相当于@ResponseBody和 ...
- MySQL安装详细教程(数据库免安装版)
MySQL安装详细教程(数据库免安装版)mysql-5.7.33-winx64.zip 一.软件下载 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产 ...