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;
    }
    

      

  • 相关阅读:
    anaconda 离线安装大包
    Openpose 安装问题解决
    Linux grep log文件及find命令学习
    整理了一些常用编程软件(方便自己下载)
    Docker安装es7.6和kibana7.6(并解决Unable to revive connection: http://192.168.162.139:9200/的问题)
    Jsr303分组校验和自定义注解校验
    Spring Cloud整合Oss
    Linux:vim
    Linux:挂载命令
    SpringBoot整合SpringSecurity:集中式项目
  • 原文地址:https://www.cnblogs.com/iamcdx-2017/p/8086102.html
Copyright © 2011-2022 走看看