java字符串各个字符计数_如何在不使用计数器的情况下计算字符串中0和1的数量...

java字符串各个字符计数

This question is often asked in interviews to test the candidate's approach to thinking about code.

面试中经常会问这个问题,以测试候选人思考代码的方法。

问题陈述 (Problem Statement)

You have a string which contains only 0's and 1's. You need to write a program which will return the number of 0's and 1's, and you are not allowed to use a counter variable by any means.

您有一个仅包含0和1的字符串。 您需要编写一个程序,该程序将返回0和1的数量,并且不允许以任何方式使用计数器变量。

Seems tricky right?

似乎很棘手吧?

Worry not - it's very simple. I am going to teach you how to do it both ways:

不用担心-这很简单。 我将教你两种方法:

  • With a counter (the classic approach)

    带柜台(经典方法)
  • Without using a counter (as required in the interview question)

    不使用计数器(根据面试问题的要求)

让我们编码 (Let's Code )

带柜台 (With a Counter)

function count(str) {
  var countForZero = 0;
  var countForOne = 0;
    
  for (var i = 0, length = str.length; i < length; i++) {
    if (str[i] === '0') {
      countForZero++;
    }
    else {
      countForOne++;
    }
  }
    
  return {
    'zero': countForZero,
    'One': countForOne
  };
}

The logic of the above code is:

上面代码的逻辑是:

  • Keep two variables for counting zeros and ones.

    保留两个变量以计算零和一。
  • Loop through the characters of the string, and when a zero is found, increment countForZero. When a one is found, increment countForOne.

    遍历字符串的字符,找到零时,递增countForZero。 找到一个后,递增countForOne。
  • In the last part, we are returning the count in an object.

    在最后一部分中,我们将返回对象中的计数。

You can try the above code using this link: https://repl.it/repls/PurpleIdolizedInstructions

您可以使用此链接尝试上述代码: https : //repl.it/repls/PurpleIdolizedInstructions

Now, let's see the solution without a counter.

现在,让我们看看没有计数器的解决方案。

没有柜台 (Without a Counter)

function count(str) {
  var sum = 0;
  
  for (var i = 0, length = str.length; i < length; i++) {
    sum += Number(str[i]);
  }
  
  return {
    'zero': str.length - sum,
    'One': sum
  };
}

As you can see in the above code, I am not using any counter, but rather a variable called sum. So what's the concept here?

如您在上面的代码中看到的,我没有使用任何计数器,而是使用了一个名为sum的变量。 那么这里的概念是什么?

The concept is:

这个概念是:

The sum of a character of a string which contains zero and one will always be equal to the number of 1's.
包含零和一的字符串的字符总和始终等于1的数目。

For example: 0011001 : 0+0+1+1+0+0+1 = 3 = number of 1's

例如:0011001:0 + 0 + 1 + 1 + 0 + 0 + 1 = 3 = 1的个数

And the number of zeroes = length of string - number of 1's
零的数目=字符串的长度-1的数目

The logic of the above code is:

上面代码的逻辑是:

  • Keep a sum variable initialized with value zero.

    保持求和变量初始化为零值。
  • Loop through the characters of string and take the sum of all the characters.

    遍历字符串的字符并取所有字符的总和。
  • The sum of all the characters of the string will be the number of 1's, and the number of zeroes will be the (length of string - number of 1's).

    字符串的所有字符的总和将为1的数目,零的数目将为(字符串的长度-1的数目)。
  • In the last part, we are returning the count in an object.

    在最后一部分中,我们将返回对象中的计数。

You can try the above code using this link: https://repl.it/repls/ComfortableOrdinaryConversions

您可以使用此链接尝试上述代码: https : //repl.it/repls/ComfortableOrdinaryConversions

I hope you are able to understand how to answer this question both with and without a counter now.

我希望您现在能够理解如何在不使用计数器的情况下回答这个问题。

If you have any questions, feel free to ask in the comment section. Or if you have any other tricks, let me know as well.

如果您有任何疑问,请随时在评论部分提问。 或者,如果您还有其他窍门,也请告诉我。

翻译自: https://www.freecodecamp.org/news/how-to-count-no-of-0-and-1-in-a-string-without-using-counter/

java字符串各个字符计数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值