码迷,mamicode.com
首页 > 其他好文 > 详细

C编程题总结

时间:2019-11-23 17:58:50      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:def   无法   str   number   两数相加   不能   dup   for   ase   

两数之和:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

int* twoSum(int* nums, int numsSize, int target, int* returnSize){//参数:原始数组,原始数组长度,比较值,返回数组长度
    int* res=(int*)malloc(sizeof(int)*2);
    *returnSize=0;
    int i,j;
    for(i=0;i<numsSize-1;i++)
    {
        for(j=i+1;j<numsSize;j++)//依次往后比较
        {
            if(nums[i]+nums[j]==target)
            {
                res[0]=i;
                res[1]=j;
                *returnSize=2;
                return res;
            }
        }
    }
    return res;
}

存在重复元素:

输入: [1,2,3,1]输出: true

输入: [1,2,3,4]输出: false

void swap(int *a, int *b)//自己编写交换函数
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
void quickSort(int arr[] ,int start, int end)//自己编写归并排序
{
    int arrBase, arrMiddle;

    int tempStart = start,
        tempEnd = end;

    //对于这种递归的函数,内部必须要有一个函数返回的条件
    if(tempStart >= tempEnd)
        return;

    //拷贝一个基准值作为后面比较的参数
    arrBase = arr[start];
    while(start < end)
    {
        while(start < end && arr[end] > arrBase)
            end--;
        if(start < end)
        {
            swap(&arr[start], &arr[end]);
            start++;
        }

        while(start < end && arr[start] < arrBase)
            start++;
        if(start < end)
        {
            swap(&arr[start], &arr[end]);
            end--;
        }
    }
    arr[start] = arrBase;
    arrMiddle = start;

    //分治方法进行递归
    quickSort(arr,tempStart,arrMiddle-1);
    quickSort(arr,arrMiddle+1,tempEnd);
}
int comp(const void *a,const void *b){//自己编写比较函数
    return (*(int*)a > *(int*)b);
}
bool containsDuplicate(int* nums, int numsSize){
    qsort(nums, numsSize, sizeof(int), comp);//把原始数组的值,按顺序排序
    for(int k=0;k<numsSize-1;k++){//判断,相邻元素有相等,则返回true。
        if(nums[k]==nums[k+1]){
            return true;
        }
    }
    return false;
}

两数相加:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)

输出:7 -> 0 -> 8

原因:342 + 465 = 807

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//链表结构
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) 
{
    //初始化空头
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next =NULL;
    struct ListNode* tail;
    tail = head;
    struct ListNode* p1 = l1;
    struct ListNode* p2 = l2;

    int carry = 0; //进位
   if(p1==NULL) return p2;
   if(p2==NULL) return p1;
//循环,直到跳出两个链表 //当两条链表一样长时只需这一次处理,但是当不一样长时,只能处理一样长的大小 while (p1 != NULL && p2 != NULL) { //当前结点的和,注意加上进位 int sum = p1->val + p2->val + carry; //当前结点和大于等于10时 if (sum >= 10) { sum -= 10; //当前结点的值-10,变为个位 carry = 1; //大于10,进位1 } else { carry = 0; } //初始化结点,尾添加, 必须先将尾指针移动到新的尾结点上再赋值,因为不这么做的话,第一个结点无法正确赋值(因为这时候尾指针还没有真正移到第一个结点上,此时指向的还是头结点) tail->next = (struct ListNode*)malloc(sizeof(struct ListNode)); tail = tail->next; tail->val = sum; p1 = p1->next; p2 = p2->next; } //当两条链表不一样长时,其中一者为NULL了,另一者还没完,这时候用p1指向没完的那一条链表,继续遍历 if (p1 == NULL) { p1 = p2; } else if (p2 = NULL) { p1 = p1; } //遍历剩余部分 while (p1 != NULL) { int sum = p1->val + carry; //带上进制计算当前结点和 if (sum >= 10) { sum -= 10; carry = 1; } else { carry = 0; } //继续朝合并的链表中添加结点 tail->next = (struct ListNode*)malloc(sizeof(struct ListNode)); tail = tail->next; tail->val = sum; p1 = p1->next; } //如果最后一位还有进制,再申请一个结点存1 if (carry == 1) { tail->next = (struct ListNode*)malloc(sizeof(struct ListNode)); tail = tail->next; tail->val = 1; } tail->next = NULL; //尾指针赋空,结尾 //因为我们不能返回头结点,所以要把头结点释放了,但是要头指针移到第一个结点 struct ListNode* pTemp = head; head = head->next; free(pTemp); return head; }

整数反转:

输入: 123
输出: 321
输入: -123
输出: -321
输入: 120
输出: 21
int reverse(int x)
{
    int temp;
    int i;
    long c=0;//定义为long型
    for(i=0;i<10;++i){
        temp=x%10;//取出最后一位包括负数情况
        x=x/10;
        c=c*10+temp;
        if(c>0x7fffffff||c<(signed int)0x80000000){//判断整型数据是否溢出
            return 0;
        }
        if(x==0) break;
    }
    return c;
}

 

C编程题总结

标签:def   无法   str   number   两数相加   不能   dup   for   ase   

原文地址:https://www.cnblogs.com/ming-4/p/11918733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!