简体   繁体   中英

leetcode 1st problem called two_sum im geting error,please help me with the mistake what i have done

this is my leetcode 1st day 1st problem called two_sum am getting an error, please help me with the mistake what I have done, in vs code this, below code was getting no error and also no output, but in leetcode I getting an error :

Line 25: Char 5: error: conflicting types for 'main' [solution.c] int main(int argc, char *argv[]) { ^~~~ where my code is only 22 lines but I was getting error in the 25th line

    #include <stdio.h>
    void main()
    {
    int n, a[10], i, j, t;
    printf("enter the array size");
    scanf("%d", &n);
    printf("enter th array values");
    for (i = 0; i < n; i++)
     scanf("%d", &a[I]);
    printf("enter the targest sum");
scanf("%d", &t);
for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d]", i, j);
        }
      }
    }
  }

2nd try: I replaced with a new code in the last if statement to check in another way. so, now I got output but, not the correct one

According to the C Standard the function main without parameters shall be declared like

int main( void )

Instead of the array with the fixed number of elements

int n, a[10], i, j, t;

you could use a variable length array like for example

int n, i, j, t;
printf("enter the array size");
scanf("%d", &n);
int a[n];

Another approach is to allocated the array dynamically like

int *a = malloc( n * sizeof( int ) );

You can write either each pair of values in a new line like

printf("[%d,%d]\n", i, j);

Or after all pairs you could print the new line character like

for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d] ", i, j);
        }
    }
}

putchar( '\n' );

{ay attention to that you have a typo

scanf("%d", &a[I]);
              ^^^

you need to write

scanf("%d", &a[i]);
              ^^^

Try declaring main as int main(int argc, char * argv[]) or int main(void) . void main is usually not a valid type declaration so I presume both VS Code and Leetcode do not treat your main declaration as the entry to your program.

In leetcode, you don't need to (and should not) write your own main function. Leetcode has its own main function. Also you don't need to include libraries. What you need to do is just to implement the twoSum function.

Here's Leetcode's default code definition for the two sum problem:



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){

}

What you need to do is write code within the twoSum() function.


// don't need to #include<stdio.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    // write your code here.
}


// don't need to write main function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM