LeetCode 397. Integer Replacement
397. Integer Replacement
- Total Accepted: 5878
- Total Submissions: 21519
- Difficulty: Easy
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
Subscribe to see which companies asked this question
这个题目没思路,参考了别人的想法 网址:https://discuss.leetcode.com/topic/58334/a-couple-of-java-solutions-with-explanations
class Solution {
public:
int integerReplacement(int n) {
if(INT_MAX==n)return ; //INT_MAX是个特殊值,如果加1就越界了
//我们也可以用long long 类型的数令它等于n,如 long long N=n;然后把下面的n替换成N即可
int c = ;
while (n != ) {
if ((n & ) == ) {
n /= ;
}
else if (n == || ((n) & (<<)) == ) { //留意后两位 奇数是最后一位是1 如果倒数第二位是0那么减1 使得1的位数更少
--n;
}
else {
++n;
}
++c;
}
return c;
}
};
LeetCode 397. Integer Replacement的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- 397 Integer Replacement 整数替换
给定一个正整数 n,你可以做如下操作:1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是多少?示例 ...
- 397. Integer Replacement
先正统做法. public class Solution { public int integerReplacement(int n) { if(n == 1) return 0; int res = ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- Leetcode Integer Replacement
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- LeetCode——Integer Replacement
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
随机推荐
- 基于云计算Iaas平台的ZStack
2015年4月,一家全新的基础架构即服务的软件产品ZStack面世.ZStack的主创人员是自在海外云计算公司的中国人.ZStack是基于Java语言,结合了OpenStack和CloudStack上 ...
- CentOS 7 安装 MySQL Database
CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...
- DSL 或者说是抽象 或者说是沉淀 ,我看到的不错的一篇文章
作者:张浩斌 链接:https://www.zhihu.com/question/45552115/answer/99388265 来源:知乎 著作权归作者张浩斌和知乎所有. ---------- ...
- CentOS 系统安装
1.安装时分区建立 建立/boot分区 500M /swap 10000M 剩余空间建立PV PV加入VG 建立LV 全部挂在到/ 2安装模式选择:基本安装 开发工具全部装,SERVER 全部不选,中 ...
- java数组引用
public class Arriy { public static void main(String args[]){ int data[]=new int[3]; data[0]=10; data ...
- 统计Apache或Nginx访问日志里的独立IP访问数量的Shell
1.把IP数量直接输出显示: cat access_log_2011_06_26.log |awk '{print $1}'|uniq -c|wc -l 2.把IP数量输出到文本显示: cat acc ...
- 【python】jiraAPI使用教程 自动创建jira问题单并置状态为OPEN
环境依赖 : python库 redis jira 安装命令:pip install redis pip install jira redis服务安装命令: $sudo apt-get update ...
- 将jar包直接Buldpath所引起的问题
今天在学习jasperReports时遇到个很郁闷的问题,发现自己的jar包通过Buildpath导进去后,一运行,出现了一些错误. 找不到类 这就很尴尬了,我已经按要求都导进去了,竟然提示我找不到类 ...
- windows 下 webstorm 使用SVN
1.安装了webstorm之后,用了很久都没有配置SVN 现在想配置svn,结果发现一般的svn程序不好用. 经指导,发现需要安装一个专用于webstorm的SVN 2.在file->setti ...
- BZOJ 1801中国象棋 DP
1801: [Ahoi2009]chess 中国象棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1426 Solved: 826[Submit][ ...