Find the longest continuous subsequence of 1's in binary representation of an integer.
Solution 1:
public int countConsecutive1s(int n) { if(n == 0) return 0; int max = 0; int len = 0; for(int i=0; i<32; i++) { int v = (n >>> i) & 1; if(v == 1) { len++; max = Math.max(len, max); } else { len = 0; } } return max; }
Solution 2:
To determine the length of the longest sequence of consecutive 1's, this is more efficient:
def longest_one_chain(n) c = 0 while n != 0 n &= n >> 1 c += 1 end c end
The method simply counts how many times you can "bitwise AND" the number with itself shifted 1 bit to the right until it is zero.
Example:
______ <-- longest chain
01011011100001111110011110101010 c=0
AND 0101101110000111111001111010101
1001100000111110001110000000 c=1, 1’s deleted
AND 100110000011111000111000000
100000011110000110000000 c=2, 11’s deleted
AND 10000001111000011000000
1110000010000000 c=3, 111’s deleted
AND 111000001000000
110000000000000 c=4, 1111’s deleted
AND 11000000000000
10000000000000 c=5, 11111’s deleted
AND 1000000000000
0 c=6, 111111’s deleted
Reference:
http://stackoverflow.com/questions/10911780/looping-through-bits-in-an-integer-ruby/10922528
相关推荐
python python_leetcode题解之128_Longest_Consecutive_Sequence
javascript js_leetcode题解之128-longest-consecutive-sequence.js
c语言入门 c语言_leetcode题解之1372_longest_zigzag_path_in_a_binary_tree
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
java java_leetcode题解之Longest Increasing Path in a Matrix.java
java java_leetcode题解之Longest Word in Dictionary.java
java java_leetcode题解之Longest Word in Dictionary through Deleting.java
给定exampleArray = [100, 4, 200, 1, 3, 2] 100,4,200,1,3,2 [100, 4, 200, 1, 3, 2] 。 最长的连续元素序列是[1, 2, 3, 4] 。 longestConsecutiveLength(exampleArray)返回最长序列的长度4 。 细心! 您的...
最长增长子序列(Longest Increasing Subsequence,简称LIS)是序列分析中的一个基本概念,自上世纪以来一直是研究的热点。对于长度为n的序列,可以使用O(n log n)的时间复杂度精确计算其LIS。然而,在亚线性时间下...
LMS Longest Monotonically Increasing Sequence Algorithm
s[i,j] (s[i-1,j], s[i,j-1], s[i-1,j-1]+1) else s[i,j] (s[i-1,j], s[i,j-1]) ``` - 其中,\( s[i,j] \)表示字符串\( v \)的前\( i \)个字符与字符串\( w \)的前\( j \)个字符的最长公共子序列的长度。 - \...
【Longest Common Ancestor (LCA)问题】是图论中的一个重要概念,特别是在树结构的处理中。给定树T中的两个节点u和v,LCA指的是离根节点最远的公共祖先,即同时是u和v祖先的那个节点。解决LCA问题可以转化为求解...
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。
1. "POJ2533-Longest Ordered Subsequence【O(nlogn)】.cpp":这是一个高效的解决方案,其时间复杂度为O(nlogn),很可能采用了动态规划(Dynamic Programming, DP)结合二分查找(Binary Search)的方法。...
printf("The length of the longest ordered subsequence is: %d\n", longestOrderedSubsequence(nums, numsSize)); return 0; } ``` 在上面的C程序中,我们首先定义了动态规划数组`dp`,然后通过两层循环来填充...
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. ...
1. **Python基础**: Python 是一种高级编程语言,以其简洁易读的语法而闻名。在这个项目中,你可能会遇到关于变量、数据类型(如列表、元组、字典)、控制结构(if语句、for循环、while循环)、函数、类和模块等基础...
Binary Tree Longest Consecutive Sequence等。这类题目通常要求读者使用树的各种操作来解决问题,例如树的遍历、查找、插入等操作。 4. 图类题目 图类题目是LeetCode原题中的一个复杂类题目,本文中涵盖了多个图...