(警告)是的,这是我正在处理的任务的一部分,但此时我完全绝望,不,我不是在寻找你们来为我解决它,但任何提示都将不胜感激!(/警告)
我几乎正在尝试制作一个交互式菜单,用户打算输入一个表达式(例如“5 3 +”)并且程序应该检测到它是后缀表示法,不幸的是我遇到了分段错误错误,我怀疑它们与strlen函数的使用有关。
编辑:我能够让它工作,首先这
char expression[25] = {NULL};条线
变成char expression[25] = {'\0'};在调用
determine_notation函数时,我[25]从数组中删除了我传递的数组,如下所示:determine_notation(expression, expr_length);还有
input[length]我改成的部分,input[length-2]就像之前评论中提到的那样,input[length] == '\0'和input[length--] == '\n'.总之感谢大家的帮助!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int determine_notation(char input[25], int length);
int main(void)
{
char expression[25] = {NULL}; // Initializing character array to NULL
int notation;
int expr_length;
printf("Please enter your expression to detect and convert it's notation: ");
fgets( expression, 25, …Run Code Online (Sandbox Code Playgroud) 给定一个数组,其中每个元素出现 3 次,只有一个元素只出现一次。找出出现一次的元素。预期的时间复杂度为 O(n) 和 O(1) 额外空间。
输入:arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}
输出:2
在给定的数组中,除了 2 出现一次之外,所有元素都出现了 3 次。
我的解决方案是使用哈希图来尝试解决这个问题。不过,我遇到了一个数学解决方案,并想知道为什么会这样:
def singleNumber(nums):
# applying the formula.
return (3 * sum(set(nums)) - sum(nums)) / 2
Run Code Online (Sandbox Code Playgroud)
如果有人能帮助我理解为什么会这样,那就太好了:)