`

Longest Consecutive 1s in Binary

 
阅读更多

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

http://www.quora.com/How-can-we-find-the-longest-continuous-subsequence-of-0s-in-binary-representation-of-an-integer-in-O-log-n-time-complexity

分享到:
评论

相关推荐

    python-leetcode题解之128-Longest-Consecutive-Sequence

    print(longest_consecutive([100, 4, 200, 1, 3, 2])) # 输出应为4 ``` 以上代码就完整地解决并展示了如何找到一个无序数组中的最长连续序列问题。通过巧妙的算法设计与数据结构应用,我们成功将时间复杂度降低到O...

    js-leetcode题解之128-longest-consecutive-sequence.js

    其中,问题编号128的“最长连续序列”(Longest Consecutive Sequence)是其中的一个经典问题。该问题要求编写一个函数,找出数组中未排序的最长连续元素序列的长度。针对这一问题,使用JavaScript编写一个高效的解法...

    c语言-leetcode题解之1372-longest-zigzag-path-in-a-binary-tree

    c语言入门 c语言_leetcode题解之1372_longest_zigzag_path_in_a_binary_tree

    LeetCode5 Longest Palindromic Substring

    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-leetcode题解之Longest Word in Dictionary.java

    本文档详细介绍了使用Java语言解决LeetCode上的一个特定问题——“Longest Word in Dictionary”(字典中最长的单词)。 在解决这个问题时,开发者需要编写一个函数,该函数从一组单词列表中找到并返回字典序最小的...

    longest-consecutive-sequence

    给定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 。 细心! 您的...

    Estimating the Longest Increasing Subsequence in Nearly Optimal

    最长增长子序列(Longest Increasing Subsequence,简称LIS)是序列分析中的一个基本概念,自上世纪以来一直是研究的热点。对于长度为n的序列,可以使用O(n log n)的时间复杂度精确计算其LIS。然而,在亚线性时间下...

    最长公共子序列Longest Monotonically Increasing Sequence Algorithm

    LMS Longest Monotonically Increasing Sequence Algorithm

    java-leetcode题解之Longest Increasing Path in a Matrix.java

    Java实现LeetCode题解之矩阵中的最长递增路径,是对算法问题“Longest Increasing Path in a Matrix”的解答。在该问题中,给定一个m x n整数矩阵,需要编写一个函数来找到矩阵中的最长递增路径长度。一个路径在矩阵...

    Longest Common Subsequences For Dummies

    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

    【Longest Common Ancestor (LCA)问题】是图论中的一个重要概念,特别是在树结构的处理中。给定树T中的两个节点u和v,LCA指的是离根节点最远的公共祖先,即同时是u和v祖先的那个节点。解决LCA问题可以转化为求解...

    java-leetcode题解之Longest Word in Dictionary through Deleting.java

    题目“Longest Word in Dictionary through Deleting”要求编写一个Java程序来寻找通过删除字典中的某些字符可以得到的最长单词。 为了完成这个任务,我们需要先熟悉Java编程语言,了解它的语法结构、面向对象的...

    北大POJ2533-Longest Ordered Subsequence【O(nlogn)】

    北大POJ2533-Longest Ordered Subsequence【O(nlogn)】

    longest-common-string.rar_LONGEST COMMON STRI_longest_longest co

    这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。

    POJ2533-Longest Ordered Subsequence

    1. "POJ2533-Longest Ordered Subsequence【O(nlogn)】.cpp":这是一个高效的解决方案,其时间复杂度为O(nlogn),很可能采用了动态规划(Dynamic Programming, DP)结合二分查找(Binary Search)的方法。...

    Longest Ordered Subsequence

    printf("The length of the longest ordered subsequence is: %d\n", longestOrderedSubsequence(nums, numsSize)); return 0; } ``` 在上面的C程序中,我们首先定义了动态规划数组`dp`,然后通过两层循环来填充...

    longest-palindromic-substring

    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. ...

    CodeWars-Practice:我用Python或Javascript解决Codewars问题的代码解决方案

    1. **Python基础**: Python 是一种高级编程语言,以其简洁易读的语法而闻名。在这个项目中,你可能会遇到关于变量、数据类型(如列表、元组、字典)、控制结构(if语句、for循环、while循环)、函数、类和模块等基础...

    2018年最新Google Onsite面经总结1

    Binary Tree Longest Consecutive Sequence等。这类题目通常要求读者使用树的各种操作来解决问题,例如树的遍历、查找、插入等操作。 4. 图类题目 图类题目是LeetCode原题中的一个复杂类题目,本文中涵盖了多个图...

Global site tag (gtag.js) - Google Analytics