当前位置:网站首页>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
边栏推荐
- Graduation design of wechat small program ordering system of small program completion works (3) background function
- 为什么要使用MQ消息中间件?这几个问题必须拿下!
- Wechat reservation of small program completion works (5) assignment book of small program graduation project
- Solving a random number problem
- 富文本样式文字图片处理
- Wechat sports field reservation of the finished works of the applet graduation project (4) opening report
- Network solutions for Alibaba cloud services
- Wechat reservation applet graduation design of applet completion works (2) applet function
- 提高代码可续性的小技巧,以connectTo方法为例。
- How can hospitals achieve efficient and low-cost operation and maintenance? Is there any software that can meet it?
猜你喜欢

Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)

Django4.0 + Web + MySQL5.7 实现简单登录操作

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

51单片机内部外设:定时器和计数器

Redis learning notes

Record the process of two multi terminal troubleshooting

Wechat applet ordering system graduation design of applet completion works (2) applet function

NVIDIA programmable reasoning accelerator tensorrt learning notes (II) - practical operation

Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)

酷炫canvas动画冲击波js特效
随机推荐
Foundation 31: Selenium positioning dynamic ID element
51 MCU peripherals: Motor
附加:下半部分sql语句 区/县(数据表)
Wechat reservation applet graduation design of applet completion works (3) background function
智能运维场景解析:如何通过异常检测发现业务系统状态异常
@Differences between requestparam, @pathparam, @pathvariable and other annotations (use of some annotations)
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
游戏外挂怎么做?
Graduation design of wechat small program ordering system of small program completion works (3) background function
51单片机外设篇:蜂鸣器
BGP border gateway protocol basic knowledge points
英特尔就产品延误向Xe HPG寻宝游戏获奖者道歉并公布奖品样貌
Centernet network structure construction
QA robot sequencing model
Leetcode · 83 biweekly race · 6129. Number of all 0 subarrays · mathematics
Wechat applet ordering system graduation design of applet completion works (8) graduation design thesis template
Sina Weibo client (4) - set navigation bar theme
Unity HTC vive use
How does Flink SQL persist?
JDBC的API解析