分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程https://www.captainai.net/troubleshooter
package live.every.day.lseg; import org.junit.jupiter.api.Test; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; public class BinaryStrZeroOperationTest { @Test public void test01() { String s = "011100"; int expected = 7; assertEquals(expected, BinaryStrZeroOperation.solution(s)); } @Test public void test02() { String s = "111"; int expected = 5; assertEquals(expected, BinaryStrZeroOperation.solution(s)); } @Test public void test03() { String s = "1111010101111"; int expected = 22; assertEquals(expected, BinaryStrZeroOperation.solution(s)); } @Test public void test04() { int len = 400000; char c = '1'; char[] chars = new char[len]; Arrays.fill(chars, c); String s = new String(chars); int expected = 799999; assertEquals(expected, BinaryStrZeroOperation.solution(s)); } @Test public void test05() { String s = "0"; int expected = 0; assertEquals(expected, BinaryStrZeroOperation.solution(s)); } }package live.every.day.lseg; /** * You are given a string S of length N which encodes a non-negative number V in a binary form. * Two types of operations may be performed on it to modify its value: * if V is odd, subtract 1 from it; * if V is even, divide it by 2. * These operations are performed until the value of V becomes 0. * * For example, if string S= "011100", its value V initially is 28. The value of V would change as * follows: * · V = 28, which is even: divide by 2 to obtain 14; * · V = 14, which is even: divide by 2 to obtain 7; * · V = 7, which is odd: subtract 1 to obtain 6; * · V = 6, which is even: divide by 2 to obtain 3; * · V = 3, which is odd: subtract 1 to obtain 2; * · V = 2, which is even: divide by 2 to obtain 1; * · V = 1, which is odd: subtract 1 to obtain 0. * Seven operations were required to reduce the value of V to 0. * * Write a function: * class Solution { public int solution(String S); } * that, given a string S consisting of N characters containing a binary representation of the * initial value V, returns the number of operations after which its value will become 0. * Examples: * 1. Given S = "011100", the function should return 7. String S represents the number 28, which * becomes 0 after seven operations, as explained above. * 2. Given S = "111", the function should return 5. String S encodes the number V=7. Its value * will change over the following five operations: * · V = 7, which is odd: subtract 1 to obtain 6; * · V = 6, which is even: divide by 2 to obtain 3; * · V = 3, which is odd: subtract 1 to obtain 2; * · V = 2, which is even: divide by 2 to obtain 1; * · V = 1, which is odd: subtract 1 to obtain 0. * 3. Given S = "1111010101111", the function should return 22. * 4. Given string S consisting of "1" repeated 400,000 times, the function should return 799,999. * * Write an efficient algorithm for the following assumptions: * 1. String S is made only of the characters '0' and/or '1'; * 2. N, which is the length of string S, is an integer within the range [1..1,000,000]; * 3. The binary representation is big-endian, i.e. the first character of string S corresponds to * the most significant bit; * 4. The binary representation may contain leading zeros. */ public class BinaryStrZeroOperation { public static int solution(String S) { int n = S.length(); // 找到第一个 '1' 的位置 int firstOne = -1; for (int i = 0; i < n; i++) { if (S.charAt(i) == '1') { firstOne = i; break; } } // 全零的情况 if (firstOne == -1) { return 0; } // 有效长度(去掉前导零后) int len = n - firstOne; // 统计有效部分中 '1' 的个数 int ones = 0; for (int i = firstOne; i < n; i++) { if (S.charAt(i) == '1') { ones++; } } // 操作次数 = (有效长度 - 1) + 1的个数 return (len - 1) + ones; } }