力扣(LeetCode)刷题,简单题(第5期)

简介: 力扣(LeetCode)刷题,简单题(第5期)

目录

第1题:二进制中1的个数


第2题:打印从 1 到最大的 n 位十进制数


第3题:删除链表的节点


第4题:调整数组顺序使奇数位于偶数前面


第5题:链表中倒数第K个节点


第6题:反转链表


第7题:二叉树的镜像


第8题:顺时针打印矩阵


第9题:数组中出现次数超过一半的数


第10题:最小的K个数


力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。


第1题:二进制中1的个数

试题要求如下:


image.png


回答(C语言):


int hammingWeight(uint32_t n) {
    int cou=0;
    while(n>0){
        if(n%2==1){ //注意是二进制
            cou++;
        }
        n/=2;
    }
    return cou;
}

运行效率如下所示:


image.png


第2题:打印从 1 到最大的 n 位十进制数

试题要求如下:


image.png


回答(C语言):


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* printNumbers(int n, int* returnSize){
    int cou=(int)pow(10,n)-1;
    int* data_buf=(int*)malloc(sizeof(int)*(cou));
    for(int i=0;i<cou;i++)
        data_buf[i]=i+1;
    *returnSize=cou;
    return data_buf;
}

运行效率如下所示:


image.png


第3题:删除链表的节点

试题要求如下:


image.png


回答(C语言):


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteNode(struct ListNode* head, int val){
    struct ListNode *p=head,*q=p->next;
    if(head->val==val) 
        return head->next;
    while(q){
        if(q->val==val){
            p->next=q->next;
            free(q);
            return head;
        }else{
            p=q;
            q=q->next;
        }
    }
    return head;
}

运行效率如下所示:


image.png


第4题:调整数组顺序使奇数位于偶数前面

试题要求如下:


image.png


回答(C语言):


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* exchange(int* nums, int numsSize, int* returnSize){
    int i=0,j=numsSize-1;
    int num=0;
    while(i<j){
        if(nums[i]%2==0 && nums[j]%2!=0){
            num=nums[i];
            nums[i]=nums[j];
            nums[j]=num;
        }
        if(nums[i]%2!=0)
            i++;
        if(nums[j]%2==0)
            j--;  
    }
    *returnSize=numsSize;
    return nums;
}

运行效率如下所示:


image.png


第5题:链表中倒数第K个节点

试题要求如下:


image.png


回答(C语言):


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* getKthFromEnd(struct ListNode* head, int k){
    struct ListNode* p=head,*q=p;
    int i=0;
    while(q!=NULL){
        q=q->next;
        i++;
    }
    i=i-k;
    while(i--){
       p=p->next; 
    }
    return p;
}

运行效率如下所示:


image.png


第6题:反转链表

试题要求如下:

image.png



回答(C语言):


image.png


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head){
    struct ListNode *cur = NULL,*pre = head,*t;
    while (pre != NULL) {
        t = pre->next;
        pre->next = cur;
        cur = pre;
        pre = t;
    }
    return cur;
}

运行效率如下所示:


image.png


第7题:二叉树的镜像

试题要求如下:


image.png


回答(C语言):


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* mirrorTree(struct TreeNode* root){
    if (root==NULL) {
        return NULL;
    }
    struct TreeNode* right = mirrorTree(root->right);
    struct TreeNode* left = mirrorTree(root->left);
    root->left = right;
    root->right = left;
    return root;
}

运行效率如下所示:


image.png


第8题:顺时针打印矩阵

试题要求如下:


image.png


回答(C语言):


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
    if(matrixSize==0){
        *returnSize=0;
        return 0;
    }
    int cycle=0,row=0,column=0,k=0;
    *returnSize=matrixSize*(*matrixColSize);
    int *res=malloc(*returnSize * sizeof(int));
    while(k<*returnSize){
        res[k++]=matrix[row][column];
        if(row==cycle&&(column<*matrixColSize-cycle-1)) column++;
        else if((column==*matrixColSize-cycle-1)&&(row<matrixSize-cycle-1)) row++;
        else if((row==matrixSize-cycle-1)&&column>cycle) column--;
        else if(column==cycle&&(row>cycle+1)) row--;
        else{
            cycle++;
            column++;
        }
    }
    return res;
}

运行效率如下所示:

image.png


第9题:数组中出现次数超过一半的数

试题要求如下:


image.png


回答(C语言):


int majorityElement(int* nums, int numsSize){
    int key = nums[0];
    int count = 0;
    for (int i = 0; i < numsSize; i++)
    {
        if(nums[i] == key)
            count++;
        else
            count--;
        if(count <= 0)
        {
            key = nums[i+1];
        }
    }
    return key;
}

运行效率如下所示:


image.png


第10题:最小的K个数

试题要求如下:


image.png


回答(C语言):


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){
    int num=0;
    for(int i = 0; i < arrSize - 1; i++)
    {
        for(int j = i+1; j < arrSize; j++)
        {
            if(arr[i] > arr[j])
            {
                num = arr[i];
                arr[i] = arr[j];
                arr[j] = num;
            }
        }
    }
    returnSize[0]=k;
    return arr;
}

运行效率如下所示(逻辑简单,效率惨不忍睹!):


image.png

相关文章
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2
|
5天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
39 3
|
2月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
17 3
|
2月前
|
Python
【Leetcode刷题Python】50. Pow(x, n)
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
18 1
|
2月前
|
Python
【Leetcode刷题Python】LeetCode 478. 在圆内随机生成点
本文介绍了LeetCode 478题的解法,题目要求在给定圆的半径和圆心位置的情况下实现在圆内均匀随机生成点的功能,并提供了Python的实现代码。
20 1
|
2月前
|
算法 Python
【Leetcode刷题Python】295. 数据流的中位数
本文介绍了一种使用Python实现的数据结构,用以支持数据流中添加整数并返回当前所有元素的中位数,通过排序列表来计算中位数。
16 1
|
2月前
|
算法 Python
【Leetcode刷题Python】73. 矩阵置零
本文介绍了LeetCode第73题的解法,题目要求在给定矩阵中将所有值为0的元素所在的行和列全部置为0,并提供了一种原地算法的Python实现。
19 0
【Leetcode刷题Python】73. 矩阵置零
|
2月前
|
Python
【Leetcode刷题Python】1467. 两个盒子中球的颜色数相同的概率
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
24 0