📌  相关文章
📜  给定二进制字符串中最左侧设置位的位置,其中所有 1 都出现在末尾

📅  最后修改于: 2022-05-13 01:56:05.804000             🧑  作者: Mango

给定二进制字符串中最左侧设置位的位置,其中所有 1 都出现在末尾

给定一个长度为N的二进制字符串S ,使得所有 1 都出现在右侧。任务是返回从左侧找到的第一个设置位的索引,否则返回-1。

例子:

方法:这个问题可以使用二分搜索来解决。

  • 在 [1, N] 范围内应用二分查找来查找第一个设置位,如下所示:
  • mid更新为 (l+r)/2
  • 如果设置了 s[mid] 位,则将ans更新为 mid 并将r更新为mid-1
  • 否则将l更新为 mid + 1
  • 返回ans中最后存储的值。

下面是上述方法的实现:

C++
// C++ Program to find Position
// Of leftmost set bit
#include 
using namespace std;
 
// Function to find
// A bit is set or not
bool isSetBit(string& s, int i)
{
    return s[i] == '1';
}
 
// Function to find
// First set bit
int firstSetBit(string& s, int n)
{
    long l = 0, r = n, m, ans = -1;
 
    // Applying binary search
    while (l <= r) {
 
        m = (l + r) / 2;
        if (isSetBit(s, m)) {
 
            // store the current
            // state of m in ans
            ans = m;
            r = m - 1;
        }
        else {
            l = m + 1;
        }
    }
 
    // Returning the position
    // of first set bit
    return ans;
}
 
// Driver code
int main()
{
 
    string s = "111";
    int n = s.length();
    cout << firstSetBit(s, n);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
  // Function to find
  // A bit is set or not
  static boolean isSetBit(String s, int i)
  {
    return s.charAt(i) == '1' ? true : false;
  }
 
  // Function to find
  // First set bit
  static int firstSetBit(String s, int n)
  {
    int l = 0, r = n, m, ans = -1;
 
    // Applying binary search
    while (l <= r) {
 
      m = (l + r) / 2;
      if (isSetBit(s, m)) {
 
        // store the current
        // state of m in ans
        ans = m;
        r = m - 1;
      }
      else {
        l = m + 1;
      }
    }
 
    // Returning the position
    // of first set bit
    return ans;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    String s = "111";
    int n = s.length();
    System.out.print(firstSetBit(s, n));
  }
}
 
// This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find
  // A bit is set or not
  static bool isSetBit(string s, int i)
  {
    return s[i] == '1';
  }
 
  // Function to find
  // First set bit
  static int firstSetBit(string s, int n)
  {
    int l = 0, r = n, m, ans = -1;
 
    // Applying binary search
    while (l <= r) {
 
      m = (l + r) / 2;
      if (isSetBit(s, m)) {
 
        // store the current
        // state of m in ans
        ans = m;
        r = m - 1;
      }
      else {
        l = m + 1;
      }
    }
 
    // Returning the position
    // of first set bit
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    string s = "111";
    int n = s.Length;
    Console.Write(firstSetBit(s, n));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


Python
# Python Program to find Position
# Of leftmost set bit
 
# Function to find
# A bit is set or not
def isSetBit(s, i):
     
    return s[i] == '1'
 
# Function to find
# First set bit
def firstSetBit(s, n):
 
    l = 0
    r = n
    m = 0
    ans = -1
 
    # Applying binary search
    while (l <= r):
 
        m = (l + r) // 2
        if (isSetBit(s, m)):
 
            # store the current
            # state of m in ans
            ans = m
            r = m - 1
 
        else:
            l = m + 1
 
    # Returning the position
    # of first set bit
    return ans
 
# Driver code
 
s = "111"
n = len(s)
print(firstSetBit(s, n))
 
# This code is contributed by Samim Hossain Mondal.



输出:
0

时间复杂度: O(LogN)
辅助空间: o(1)