zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    找最大质因子问题
    ACM基础训练题解4302 丢失的牛
    ACM基础训练题解4301 城市地平线
    hnu 7/19 A Broken Audio Signal
    hnu7/20比赛 BUG 题解
    http://acm.hnu.cn/online/?action=problem&type=show&id=12817&courseid=267 7.19hnu/数据结构/数学 xxs.code
    SGU 解题报告
    2015 Multi-University Training Contest 1 题解&&总结
    HDU 5351 MZL's Border (规律,大数)
    SGU 239.Minesweeper
  • 原文地址:https://www.cnblogs.com/iamcdx-2017/p/8086102.html
Copyright © 2011-2022 走看看