两数之和

 1 /*
 2  * Note: The returned array must be malloced, assume caller calls free().
 3  */
 4 int* twoSum(int* nums, int numsSize, int target) {
 5     static int a[2] = {0};    
 6     for (int i = 0; i < numsSize - 1; i++) {
 7         for (int j = i + 1; j < numsSize; j++) {
 8             if (nums[i] == target - nums[j]) {
 9                 a[0] = i;
10                 a[1] = j;
11                 return a;
12             }
13         }
14     }
15     
16     return 0;
17 }

注意:static局部变量的存储区为静态存储区

           如果不用静态的话,数组的生命周期就是从定义的地方到函数结束,函数运行结束,这个内存也就释放掉了。
           返回的是数组的首地址,一旦函数运行结束,这个地址里的东西就变成空了,所以要用静态数组延长数组的生命周期。

posted @ 2019-04-14 09:25  两块腹肌的yu先生  阅读(162)  评论(0编辑  收藏  举报