LeetCode数组类的题目提交记录 【1】

/***********************************************************************
              1. Two Sum

Discussion:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]

**************************************************************/

/**
* Note: The returned array must be malloced, assume caller calls free().
*/

//我的提交
int* twoSum(int* nums, int numsSize, int target) {
	//int *result = new int[2];

	int* result=NULL;
    result= (int *)malloc(2*sizeof(int));
	
	int i, j;
	for (i = 0; i<numsSize; i++) {
		for (j = i + 1; j<numsSize; j++) {
			if (target == (*(nums + i) + *(nums + j))) {
				*result = i;
				*(result + 1) = j;
				return result;
			}
		}
	}
	return NULL;
}

  

/***********************************************************************
        26. Remove Duplicates from Sorted Array

Discussion:

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.


**************************************************************/

//我的提交
int removeDuplicates(int* nums, int numsSize) {

	if (0 == numsSize) {
		return numsSize;
	}
	int countnum=1;
	int i, j;
	for (i = 0; i < numsSize - 1; ) 
	{
		for (j = i + 1; j < numsSize; j++) 
		{
			if (*(nums + i) != *(nums + j))
			{
				countnum++;
				*(nums + (countnum - 1)) = *(nums + j);
			}
			i = j;
		}

	}
	return countnum;
}

//参考答案1
int removeDuplicates1(int* nums, int numsSize) {

	//当是空数组时
	if (0 == numsSize) {
		return numsSize;
	}

	//非空数组时
	int index = 0;
	for (int i = 1; i < numsSize; i++) {
		if (*(nums + index) != *(nums + i)) 
		{
			index++;
			*(nums + index) = *(nums + i);
		}
	}
	return index+1;//因为非空数组开头有一个元素
}

//参考答案2
int removeDuplicates2(std::vector<int>&nums) {

	int numsSize = distance(nums.begin(), unique(nums.begin(), nums.end()));
	return numsSize;
}

  

/***********************************************************************

        80. Remove Duplicates from Sorted Array II

Discussion:

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example:
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.


**************************************************************/

//定义一个有序数组中允许元素最大重复次数
//方便直接更改重复次数
#define DuplicateMaxCount 2

//我的提交
int RemoveDuplicates(int* nums, int numsSize) {
	if (0 == numsSize) {
		return numsSize;
	}
	int index = 0;
	int ElementDuplicateCount = 0;

	for (int i = 1; i < numsSize; i++) 
	{
		if (*(nums + index) != *(nums + i))
		{
			ElementDuplicateCount = 0;

			index++;
			*(nums + index) = *(nums + i);
		}
		else 
		{
			if (ElementDuplicateCount < (DuplicateMaxCount-1))
			{
				index++;
				*(nums + index) = *(nums + i);
			}

			ElementDuplicateCount++;
		}
	}
	return index+1;
}

  

原文地址:https://www.cnblogs.com/iamcdx-2017/p/8086102.html