七月集训——第三天

今天的集训内容是排序,因为之前学校课程里没有开设数据结构,所以自己在原先基础上学习了几个排序,由于时间关系,剩余的三个排序慢慢补回来。

下面是练习题的思路:

1.题目描述:912.数组排序

给你一个整数数组 nums,请你将该数组升序排列。

题目思路 这道题主要是考察自己能否熟练的把八大排序敲出来。

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 /*冒泡
int* sortArray(int* nums, int numsSize, int* returnSize){
    int i, j;
    int temp;
    for(i=0; i<numsSize-1; i++)
        for(j=0; j<numsSize-1-i; j++)
        {
            if(nums[j] > nums[j+1])
            {
                temp = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;

            }
        }

        return nums;
}
*/

int* sortArray(int* a, int numsSize, int* returnSize){
    //选择
    int i, j;
    int min;
    int temp;
    for(i=0; i<numsSize-1; i++)
    {
        for(min=i, j=i+1; j<numsSize; j++)
        {
            if(a[j] < a[min])
            {
                min = j;
            }
        }
        if(min != i)
        {
            temp = a[i];
            a[i] = a[min];
            a[min] = a[i];
        }
    }
    return a;



}



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortArray(int* a, int numsSize, int* returnSize){
    //插入
    int i, j, end, tem;
    for(i=0; i<numsSize; i++)
    {
        end = i;
        tem = a[end+1];
        while(end >= 0)
            {
            if(tem < a[end])
            {
                a[end] = a[end+1];
                end--;
            }
            else
                break;
            a[end+1] = tem;

            }


        }
    }





/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortArray(int* a, int numsSize, int* returnSize){
    //快排
    void QuickSort(int *a, int low, int high)
    {
      int pos = FindPos(a,low,high);
      QuickSort(a,low,pos-1);
      QuickSort(a,pos+1, high);
    }
    int * FindPos(int *a, int low, int high)
    {
        int i, j, temp;
        int val = a[low];
        while(low < high)
        {
          while(low< high && a[high] > val)
            high--;
          a[low] = a[high];
          while(low< high && a[low] < val)
            low++;
          a[high] = a[low];
        }
        a[low] = val;
        return high;


    }
}


//希尔排序
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortArray(int* nums, int numsSize, int* returnSize){
    int gap;
    int temp.
    for(gap=numsSize/2; gap>0; gap/=2)
    {
      for(i=0; i<gap; i++)
      {
          for(j=i+gap; j<numsSize; j++)
          {
            k = j-gap;
            temp = nums[j];
            while(k>0 && temp < nums[k])
            {
              nums[k+gap] = nums[k];
              k-=gap;
            }
            nums[k+gap] = temp;
          }


      }
    }
    return nums;
}

敲过之后,发现自己其实是不太熟练的,有时还要翻前面的代码来写,自己想到的解决方法是反复实践加深印象。


 

2.题目描述 88.合并两个有序数组

给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

问题思路:其实最容易想到的办法就是将nums2的数组元素通过for循环合并到nums1数组中,然后再对nums1进行排序。做完之后翻了一下题解,发现还有双指针等方法可以尝试。

代码:

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){

    void sort(int *a) //冒泡
    {
        int i, j, temp;
 
        for(i=0; i<nums1Size-1;i++)//
        {
            for(j=0; j<nums1Size-1-i; j++)
            {
                if(nums1[j] > nums1[j+1])
                    {
                        temp = nums1[j];
                        nums1[j] = nums1[j+1];
                        nums1[j+1] = temp;

                    }
            }

        }
    }
    for(int i=0; i<n;i++)
        {
            nums1[m] = nums2[i];  //合并元素
            m++;
            
        }
    sort(nums1);
    return nums1;
}


 

题目三:1037. 有效的回旋镖

给定一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点,如果这些点构成一个 回旋镖 则返回 true 。

回旋镖 定义为一组三个点,这些点 各不相同 且 不在一条直线上 。

 问题思路:判断是否有效题目已经给出两个条件,即

1.点各不相同

2.点都不在一条直线上

而点不相同的条件就是二维数组point中的每个元素都不相同

至于点都不在一条直线上,一开始我使用的条件是以两个点为一条直线,即三个点分为3条直线,判断它们的斜率是否相等,但是提交即出错,提示忽略了除数为0的情况,因此变换一下公式,即由原来的  k=(y2-y1)/(x2-x1)== (y3-y1)/(x3-x1) 变成(y3-y1)/(x3-x1)-(y2-y1)/(x2-x1)=0

代码:

bool isBoomerang(int** points, int pointsSize, int* pointsColSize){
    //先判断其是否相等
    int i = 0;
    if(points[0] == points[1] || points[0] ==points[2] || points[1] == points[2])
    return false;

    //判断是否在一条直线上
    if((points[2][1] - points[0][1])*(points[1][0]-points[0][0]) -(points[1][1] - points[0][1])*(points[2][0]-points[0][0]) == 0)
//对应公式(y3-y1)/(x3-x1)-(y2-y1)/(x2-x1)=0
      return false;
    else
      return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值