c语言怎么讲英文字符串中的英语单词按长度由大到小排序,由空格分隔 - 爱问答

(爱问答)

c语言怎么讲英文字符串中的英语单词按长度由大到小排序,由空格分隔

如char s1[]="This is a c programming ttest"

英文描述

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].

中文描述

这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。

这里题目的要求是,将 N 为一个整数类型的数据,转换为一个 2 进制的字符串,然后在返回的字符串中返回最大的 0 的间隔数字。

例如 529 转换为 2 进制的字符串为:1000010001,在这里,将会存在以 1 为分割的字符串  0000 和 000,这 2 个字符串的长度分别为 4 和 3。

我们的算法需要返回的值诶 4。

思路和点评

这个题目的思路其实比较简单,你需要首先将 N 这个整数,转换为 0 和 1 的字符串。然后在转换成功的字符串中返回以 1 分分割的 0 的长度。

这里可能需要考虑下面的几种况。

况结果11这个情况应该返回的长度为 010这个情况因为没有被 1 这个字符串封闭,因此应该返回长度为 0

传统的思路应该是采取字符串分割的方式,进行遍历后获得结果。

我们在这里采取一种相对不是非常常规的方式,例如在 10000010001 字符串中插入 #,将字符串变为 #1#00000#1#000#1#。

然后将字符串按照 1 进行分割,那么分割后的数组应该分别存储的数据为:#,#0000#,#000#,#

这里我们只需要找到 #...# 中值最大的连续 0 字符串就可以了。基本上可以使用 1 个字符串替换函数和一个字符串分割函数就可以了,并不需要多次存储和遍历。

源代码

源代码和有关代码的更新请访问 GitHub:

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/codility/CodilityBinaryGapTest.java

代码思路请参考:

 

Binary Gap(二进制空白)

  1. 云栖社区>
  2. 博客列表>
  3. 正文

Binary Gap(二进制空白)

honeymoose 2018-12-24 06:03:58 浏览53 评论0
  • java
  •  
  • logger
  •  
  • string

摘要: 中文标题【二进制空白】 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

中文标题【二进制空白】

英文描述

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].

中文描述

这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。

这里题目的要求是,将 N 为一个整数类型的数据,转换为一个 2 进制的字符串,然后在返回的字符串中返回最大的 0 的间隔数字。

例如 529 转换为 2 进制的字符串为:1000010001,在这里,将会存在以 1 为分割的字符串  0000 和 000,这 2 个字符串的长度分别为 4 和 3。

我们的算法需要返回的值诶 4。

思路和点评

这个题目的思路其实比较简单,你需要首先将 N 这个整数,转换为 0 和 1 的字符串。然后在转换成功的字符串中返回以 1 分分割的 0 的长度。

这里可能需要考虑下面的几种情况。

情况结果11这个情况应该返回的长度为 010这个情况因为没有被 1 这个字符串封闭,因此应该返回长度为 0

传统的思路应该是采取字符串分割的方式,进行遍历后获得结果。

我们在这里采取一种相对不是非常常规的方式,例如在 10000010001 字符串中插入 #,将字符串变为 #1#00000#1#000#1#。

然后将字符串按照 1 进行分割,那么分割后的数组应该分别存储的数据为:#,#0000#,#000#,#

这里我们只需要找到 #...# 中值最大的连续 0 字符串就可以了。基本上可以使用 1 个字符串替换函数和一个字符串分割函数就可以了,并不需要多次存储和遍历。

源代码

源代码和有关代码的更新请访问 GitHub:

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/codility/CodilityBinaryGapTest.java


相关标签:英语c语言

下一篇:想资询一下,考体系资格证,需哪些个人资料

上一篇:Therearemanyshopsoneachsideofthestreet

热门标签:
英语 谜语 作文 数学 公式 语文 物理 化学 工艺 java c语言 实验 方程 金属 分子 数据库 硫酸 酒精 运算 石油 vc 世界大战 php 化合物 mysql
最新更新:
电学的一个小问题 为什么打点计时器只能粗略瞬时速度 lookdownupon用法 中专都考不上大学有必要复读一年吗? 如图,已知∠B=∠DEF,AB=DE,请添加一个条件使△ABC≌△DEF,则需添加的条件是__________. 求曲线y=2x^2和直线y=2的所围图形的面积 夜上受降城闻笛是哪句 这个怎么填数字? 小明家下五层楼是5楼,那么小明家上五层楼是几层楼? 填空题,这个题目是怎么算的呢…… 22335577()143中括号里填什么数字。 懂得人帮我看一下这个英文是啥意思??? 最小的物质单位是什么 怎么估算根号52000000 about的重读字母是哪里