目录
力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。
第1题:二进制中1的个数
试题要求如下:
回答(C语言):
int hammingWeight(uint32_t n) {
int cou=0;
while(n>0){
if(n%2==1){ //注意是二进制
cou++;
}
n/=2;
}
return cou;
}
运行效率如下所示:
第2题:打印从 1 到最大的 n 位十进制数
试题要求如下:
回答(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;
}
运行效率如下所示:
第3题:删除链表的节点
试题要求如下:
回答(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;
}
运行效率如下所示:
第4题:调整数组顺序使奇数位于偶数前面
试题要求如下:
回答(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;
}
运行效率如下所示:
第5题:链表中倒数第K个节点
试题要求如下:
回答(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;
}
运行效率如下所示:
第6题:反转链表
试题要求如下:
回答(C语言):
/**
* 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;
}
运行效率如下所示:
第7题:二叉树的镜像
试题要求如下:
回答(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;
}
运行效率如下所示:
第8题:顺时针打印矩阵
试题要求如下:
回答(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;
}
运行效率如下所示:
第9题:数组中出现次数超过一半的数
试题要求如下:
回答(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;
}
运行效率如下所示:
第10题:最小的K个数
试题要求如下:
回答(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;
}
运行效率如下所示(逻辑简单,效率惨不忍睹!):