当前位置:网站首页>Algorithm --- flip digit (kotlin)
Algorithm --- flip digit (kotlin)
2022-07-25 08:51:00 【Xiaomi technology Android research and development caoxinyu】
subject
Given a 32 An integer num, You can take a digit from 0 Turn into 1. Please write a program , Find the longest string you can get 1 The length of .
Example 1:
Input : num = 1775(110111011112)
Output : 8
Example 2:
Input : num = 7(01112)
Output : 4
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-bits-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solutions
It is easy to solve this problem with dynamic programming : Use two dynamic programming arrays current[] reverse[]
current[i] Means to include the i The subordinate of the position num Binary low order to i Bitwise continuous 1 The longest length of
reverse[i] Means to include the i Bit from low to i Bits can be flipped at most 1 individual 0->1 Continuity of 1 The longest length of
use num[i] Represents an integer num The first i The value of a
When num[i]=1 when ,current[i] = current[i-1]+1, because current[i-1] It must contain i-1 position , That is to say, he and di i Bitwise continuous , So before i-1 The maximum length of is connected with the i The length of the bit is equal to current[i], Empathy reverse[i] = reverse[i-1]+1;
num[i]=0 when , Continuous interrupt ,current[i]=0, and reverse[i] Allow flipping 1 Time , however reverse[i] It must also include the second i position , In other words, you can only flip the second i position , So there can be no flipping in front , It must be all 1, This length is exactly current[i-1], therefore reverse[i] = current[i-1]+1
Traverse num All digits , That is to say 32 Behind you ,reverse The maximum value in the array is the answer .
Equation of state :
current[i] = num[i]==1?current[i-1]+1:0
reverse[i] = num[i]==1?reverse[i-1]+1:current[i-1]+1
Reference link :https://leetcode.cn/problems/reverse-bits-lcci/solution/dong-tai-gui-hua-you-hua-0ms-by-dralamog-egzo/
resolvent
class Solution {
fun reverseBits(num: Int): Int {
var backUp = num
val dp = Array(33) {
0 }
val reverse = Array(33) {
0 }
var max = 0
for (i in 1..32) {
val isNo1 = backUp and 1 == 1
dp[i] = if (isNo1) dp[i - 1] + 1 else 0
reverse[i] = if (isNo1) reverse[i - 1] + 1 else dp[i - 1] + 1
backUp = backUp.shr(1)
max = max.coerceAtLeast(reverse[i])
}
return max
}
}
summary
1. This uses two dp Array One is used to store the previous current 0 The longest length before Easy to handle problems
I feel more and more why I can't think of it ? Not clear thinking
2. This question is not simple
边栏推荐
- Read and write models and organize notes
- 智能运维场景解析:如何通过异常检测发现业务系统状态异常
- Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)
- 机器人跳跃问题
- OpenGL es to realize the visualization of real-time audio
- Redis学习笔记
- js触屏小游戏源码冰雪之旅
- 超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张
- 防抖与节流
- Solutions to ten questions of leetcode database
猜你喜欢

js小游戏源码魔塔闯关下载

How to do the game plug-in?

The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference

Arcgis10.2 installation tutorial

Graduation project of wechat small program ordering system of small program completion works (7) Interim inspection report

51 single chip microcomputer controls nixie tube display

本周大新闻|FCC曝光Pico 4 VR一体机,雷朋母公司建立智能眼镜实验室

Foundation 32: page element positioning method XPath --- axis positioning method

@Implementation principle of Autowired annotation

Table table expansion internal row switching effect
随机推荐
How to choose a low code software development platform?
Keep your Eyes on the Lane: Real-time Attention-guided Lane Detection
25 Ph.D. degrees revoked
Wechat reservation applet graduation design of applet completion works (3) background function
PHP reports an error: classes\phpexcel\cell php Line(594) Invalid cell coordinate ESIGN1
Wechat reservation applet graduation design of applet completion works (1) development outline
51单片机外设篇:电机
Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)
51 MCU internal peripherals: serial port communication
聊下自己转型测试开发的历程
C语言基础
Wechat sports ground reservation applet graduation project of applet completion works (1) development outline
艺术 NFT 的发展之路
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
@Differences between requestparam, @pathparam, @pathvariable and other annotations (use of some annotations)
Does the server operation and maintenance need to be online 24 hours? Do you need to work overtime on weekends?
Initial knowledge of WebService (generate jar packages and call methods in remote services)
@Autowired的使用
音乐人的 NFT 指南
Additional: SQL statement area / county in the middle half (data table)