首页 > 其他分享 >LeetCode 2595. Number of Even and Odd Bits

LeetCode 2595. Number of Even and Odd Bits

时间:2024-05-16 11:20:44浏览次数:7  
标签:Even 2595 binary int Odd even indices representation odd

原题链接在这里:https://leetcode.com/problems/number-of-even-and-odd-bits/description/

题目:

You are given a positive integer n.

Let even denote the number of even indices in the binary representation of n (0-indexed) with value 1.

Let odd denote the number of odd indices in the binary representation of n (0-indexed) with value 1.

Return an integer array answer where answer = [even, odd].

Example 1:

Input: n = 17
Output: [2,0]
Explanation: The binary representation of 17 is 10001. 
It contains 1 on the 0th and 4th indices. 
There are 2 even and 0 odd indices.

Example 2:

Input: n = 2
Output: [0,1]
Explanation: The binary representation of 2 is 10.
It contains 1 on the 1st index. 
There are 0 even and 1 odd indices.

Constraints:

  • 1 <= n <= 1000

题解:

Have even and odd count, when n & cur != 0, accumulate the corresponding count.

Time Complexity: O(1). Since it is interger, it can't be more than 32 bits.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] evenOddBit(int n) {
 3         int even = 0;
 4         int odd = 0;
 5         int cur = 1;
 6         boolean isEven = true;
 7         while(cur <= n){
 8             if((n & cur) != 0){
 9                 if(isEven){
10                     even++;
11                 }else{
12                     odd++;
13                 }
14             }
15 
16             cur = cur << 1;
17             isEven = !isEven;
18         }
19 
20         return new int[]{even, odd};
21     }
22 }

 

标签:Even,2595,binary,int,Odd,even,indices,representation,odd
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18195600

相关文章

  • 事件循环(event loop)原理,并类比理解qt的信号(Signal)和槽(Slot)机制)
    背景:实际项目中要使用pyqt/pyside技术,涉及到qt和asyncio的事件循环,并需要使用到qt的信号(Signal)和槽(Slot)机制,从底层了解事件循环的原理使得后续工作更好入手。事件循环是什么?事件循环(EventLoop)是一种用于处理和调度异步任务的机制。它通常用于编写异步编程,特别是在处理IO密......
  • Snow White and the Seven Dwarfs
    Alongtimeago,therelivedaSnowWhitewhoseparentsweredeadinafarawayland.Herstepmotherwasavenomouswitch.ShewasafraidthatSnowWhitewouldbemorebeautifulthanherwhenshegrewup,soshesenthertobeamaidinthecastle.Asthe......
  • Levenshtein:计算字符串的编辑距离
    https://github.com/ztane/python-Levenshtein/在处理文本数据时,我们经常需要比较两个字符串的相似度,无论是在自然语言处理、数据清洗还是用户输入验证中。这时,Levenshtein距离(又称编辑距离)就显得尤为重要。它衡量的是,将一个字符串转换成另一个字符串所需的最少编辑操作次数,包括......
  • Camunda 流程执行错误处理ERROR BOUNDARY EVENT
     ERRORBOUNDARYEVENT:在任务发生异常时候会触发走,在代码中必须显式抛出thrownewBpmnError("error.....");publicvoidexecute(DelegateExecutiondelegateExecution)throwsException{System.out.println("进来了>>>>>>>>>>>>&......
  • WPF Behavior Interaction Triggers EventTrigger EventName CallMethodAction Target
    //xaml<Windowx:Class="WpfApp92.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 【Azure Event Hub】Schema Registry 在China Azure门户上不能创建的替代方案
    问题描述创建EventHub服务后,标准版的定价层功能中有SchemaRegistry的功能,但是根据官方文档,在门户中确无法创建。 问题解答什么是Azure架构注册表?Azure架构注册表是事件中心的一项功能,它为事件驱动的应用程序和以消息为中心的应用程序的架构提供一个中心存储库。它......
  • EventSphere项目典型用户及主要风险
    典型用户:主要风险:人本风险出现概率:中对项目影响度:中客户、用户、利益相关者:他们的需求和期望可能会与项目的实际交付产生偏差,可能导致沟通问题或不满意的结果。用户付费意愿不高。团队成员:人员变动、技能缺乏或冲突可能影响项目进展和质量。流程风险出现概率:中对项目......
  • RocketMQLog:WARN No appenders could be found for logger (io.netty.channel.nio.Ni
    springBoot集成rocketMq启动的时候报RocketMQLog:WARNNoappenderscouldbefoundforlogger(io.netty.channel.nio.NioEventLoop). RocketMQLog:WARNPleaseinitializetheloggersystemproperly. 原因是pom中的rocket的依赖版本太高了。<dependency><groupI......