news 2026/4/23 1:29:05

程序设计-二进制字符串操作计数

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
程序设计-二进制字符串操作计数

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程​​​​​​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; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 1:23:26

【收藏备用】2026年AI人才市场需求爆发,企业更看重实践能力而非学历(小白/程序员必看大模型学习指南)

2026年&#xff0c;AI行业迎来新一轮爆发式增长&#xff0c;大模型技术的普及的落地&#xff0c;让AI人才成为企业争抢的核心资源。不同于以往“唯学历论”的招聘导向&#xff0c;今年多数企业在AI人才招聘中&#xff0c;更看重求职者的实践能力、项目经验和技术落地能力&#…

作者头像 李华
网站建设 2026/4/23 1:23:00

胜山集成:定义2026年临时建筑集装箱房新范式

【导语】 基建工程提速、商业业态迭代、应急响应升级——传统建筑模式面临成本、效率、灵活性与合规性的多重考验。市场呼唤一种兼顾快速部署、坚固耐用、灵活迁移且符合国际标准的新型建筑解决方案。本文将系统阐述胜山集成在模块化快装集装箱房领域的技术路径、核心优势与价值…

作者头像 李华
网站建设 2026/4/23 1:16:42

Keras与scikit-learn整合:深度学习与传统机器学习的完美结合

1. Keras与scikit-learn的强强联合在Python的机器学习生态系统中&#xff0c;Keras和scikit-learn无疑是两颗最耀眼的明星。Keras以其简洁直观的API设计&#xff0c;成为深度学习研究和开发的首选工具&#xff1b;而scikit-learn则是传统机器学习领域的瑞士军刀&#xff0c;提供…

作者头像 李华
网站建设 2026/4/23 1:16:34

数据分析怎么做?数据分析框架是什么?

我经常听到刚接触数据分析的人的抱怨&#xff1a;”SQL要学&#xff0c;Python要学&#xff0c;报表要做&#xff0c;汇报还要写&#xff0c;感觉门槛高得吓人。”但真相是&#xff0c;SQL和Python更多是数据挖掘工具&#xff0c;PPT、BI不过是数据展示手段。学会了当然好&…

作者头像 李华