LeetCode數組類的題目提交記錄 【1】

/***********************************************************************
              1. Two Sum數組

Discussion:app

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

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

Example:blog

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

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

**************************************************************/rem

/**
* 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 Arrayget

Discussion:input

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;
}
相關文章
相關標籤/搜索