尝试上市问题的解决这里的蟒蛇,我想我会尝试一个可爱的小正则表达式来捕获的最大"二元鸿沟"(在一个数字的二进制表示零的链).
我为这个问题写的函数如下:
def solution(N):
max_gap = 0
binary_N = format(N, 'b')
list = re.findall(r'1(0+)1', binary_N)
for element in list:
if len(element) > max_gap:
max_gap = len(element)
return max_gap
Run Code Online (Sandbox Code Playgroud)
它运作得很好.但是......由于某种原因,它与10000010000000001
(二进制表示66561
)中的第二组零不匹配.9个零没有出现在匹配列表中,因此它必须是正则表达式的问题 - 但我无法看到它在哪里,因为它匹配给定的每个其他示例!
我正在尝试创建一个程序,该程序检查给定的数组/字符串是否是回文,并且不起作用。该程序仅在每个给定的数组上打印“ 0”,即使在回文上也是如此。
int main()
{
char string[100]= {0};
char stringReverse[100]= {0};
int temp = 0;
int firstLetter = 0;
int lastLetter = 0;
printf("Please enter a word or a sentence: ");
fgets(string, 100, stdin);
strcpy(stringReverse , string); // This function copies the scanned array to a new array called "stringReverse"
firstLetter = 0;
lastLetter = strlen(string) - 1; //because in array, the last cell is NULL
// This while reverses the array and insert it to a new array called …
Run Code Online (Sandbox Code Playgroud)