金典——最接近的数__
来源:互联网 发布:sql查询多表相同列名 编辑:程序博客网 时间:2024/05/16 17:58
题目描述
有一个正整数,请找出其二进制表示中1的个数相同、且大小最接近的那两个数。(一个略大,一个略小)
给定正整数int x,请返回一个vector,代表所求的两个数(小的在前)。保证答案存在。
测试样例:
2
返回:[1,4]
思路:直接暴力。
import java.util.*;public class CloseNumber { public int[] getCloseNumber(int x) { // write code here int[] res=new int[2]; int num=getOneNum(x); int left=x-1; int right=x+1; while(left>0){ if(num==getOneNum(left)) break; left--; } while(true){ if(num==getOneNum(right)) break; right++; } res[0]=left; res[1]=right; return res; } public int getOneNum(int x){ int count=0; while(x!=0){ if((x & 1)==1){ count++; } x>>=1; } return count; }}
阅读全文
0 0
- 金典——最接近的数__
- 最接近的数
- 最接近的数
- 最接近的数
- 最接近的三数之和
- 最接近的三数之和
- Leetcode刷题记——16. 3Sum Closest(最接近的三个数的和)
- 算法爱好者——最接近的三数之和 ? 待解决
- [LeetCode]—3Sum Closest 求数组中三个数之和最接近给定target的组合
- 数组中最接近N的数
- LintCode:最接近的三数之和
- 最接近数的完整程序源码
- LintCode 52-最接近的三数之和
- 最接近目标的三数和
- 1.11.1二分查找最接近的数
- LintCode 59 最接近的三数之和
- 最接近的三数之和 _LintCode
- Python 数据结构与算法——从某个列表中找出两个彼此最接近但不相等的数
- 飞行器设计大作业
- leetcode 623 Add One Row to Tree
- imageNamed和imageWithContentsOfFile
- 对于Servlet的get请求和post请求的两种数据请求的编码格式
- python:打印直角三角形
- 金典——最接近的数__
- websocket的日常问题
- Hadoop日记——安装、配置、启动HDFS
- 葵花宝典第二天
- 3 Hibernate:本地 API 单元测试优化
- FreeRTOS queue usage
- where条件
- WebRTC音频模块
- CRC校验