leetcode:Range Sum Query - Immutable
2015-12-08 09:02
357 查看
1、问题:
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
求整数数组中某个范围的元素和。
2、My answer:
用C实现的,在跑case []时出错,提示runtime error。怀疑是在数组为空的时候返回值不符合case的要求,不确定应该返回什么,试过return 0, return -999, return。结果都一样。
Last executed input:[]
source code:
struct NumArray {
int *pnums;
int num;
};
#define ERROR -999
/** Initialize your data structure here. */
struct NumArray* NumArrayCreate(int* nums, int numsSize) {
struct NumArray *pArray;
int i;
if ((numsSize <= 0) || (NULL == nums))
{
pArray = NULL;
return pArray;
}
pArray = (struct NumArray *)malloc(sizeof(struct NumArray));
pArray->pnums = (int *)malloc(numsSize);
if ((NULL == pArray) || (NULL == pArray->pnums))
return NULL;
pArray->pnums[0] = nums[0];
pArray->num = numsSize;
for (i=1; i<numsSize; i++)
{
pArray->pnums[i] = nums[i] + pArray->pnums[i-1];
}
return pArray;
}
int sumRange(struct NumArray* numArray, int i, int j) {
int sum = 0;
if (NULL == numArray)
return;
if ((i >= numArray->num) || (j >= numArray->num) || (i < 0) || (j < 0))
return;
if (i > j)
{
i = i ^ j;
j = i ^ j;
i = i ^ j;
}
if (0 == i)
{
sum = numArray->pnums[j];
}
else
{
sum = numArray->pnums[j] - numArray->pnums[i-1];
}
return sum;
}
/** Deallocates memory previously allocated for the data structure. */
void NumArrayFree(struct NumArray* numArray) {
//free(numArray->pnums);
free(numArray);
free(numArray->pnums);
}
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
求整数数组中某个范围的元素和。
2、My answer:
用C实现的,在跑case []时出错,提示runtime error。怀疑是在数组为空的时候返回值不符合case的要求,不确定应该返回什么,试过return 0, return -999, return。结果都一样。
Status:
| |
Submitted: 3 days, 18 hours ago |
source code:
struct NumArray {
int *pnums;
int num;
};
#define ERROR -999
/** Initialize your data structure here. */
struct NumArray* NumArrayCreate(int* nums, int numsSize) {
struct NumArray *pArray;
int i;
if ((numsSize <= 0) || (NULL == nums))
{
pArray = NULL;
return pArray;
}
pArray = (struct NumArray *)malloc(sizeof(struct NumArray));
pArray->pnums = (int *)malloc(numsSize);
if ((NULL == pArray) || (NULL == pArray->pnums))
return NULL;
pArray->pnums[0] = nums[0];
pArray->num = numsSize;
for (i=1; i<numsSize; i++)
{
pArray->pnums[i] = nums[i] + pArray->pnums[i-1];
}
return pArray;
}
int sumRange(struct NumArray* numArray, int i, int j) {
int sum = 0;
if (NULL == numArray)
return;
if ((i >= numArray->num) || (j >= numArray->num) || (i < 0) || (j < 0))
return;
if (i > j)
{
i = i ^ j;
j = i ^ j;
i = i ^ j;
}
if (0 == i)
{
sum = numArray->pnums[j];
}
else
{
sum = numArray->pnums[j] - numArray->pnums[i-1];
}
return sum;
}
/** Deallocates memory previously allocated for the data structure. */
void NumArrayFree(struct NumArray* numArray) {
//free(numArray->pnums);
free(numArray);
free(numArray->pnums);
}
相关文章推荐
- iOS UILabel利用NSMutableString显示不同样式的文字(富文本)
- Xcode7(修改Bitcode) does not contain bitcode. You must rebuild it with bit code enabled. -- @ShinePug
- 10 包装类(NSValue和NSNumber)
- UESTC 1018 王之新学期 贪心法
- HDU 5146 Sequence
- UVA-11248 Frequency Hopping (最大流+最小割)
- apk更新、下载、安装(三)---DownloadManager ui方式【有卡顿bug】
- js实现卡片式项目管理界面UI设计效果
- UIPickerView改变字体大小
- UEFI
- CodeForces 601B Lipshitz Sequence (单调队列)
- Activity类的runOnUiThread更新UI
- Moqui简介
- UITableView优化技巧
- SPOJ GSS7 Can you answer these queries VII(LCT)
- UI基础-初级数据持久化
- 【android_温故知新】第 5 组 UI 组件:ProgressBar 及其子类
- 随意细解:UI -- 初级数据持久化
- UItextField 和 UIbutton
- iOS 获取设备的UUID