自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(47)
  • 收藏
  • 关注

原创 【Leetcode】 C语言 448. Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array题目分析:这也是跟前面有道题类似,用数组参数表达原数组元素。int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){ char * temp = (char *)malloc(numsSize*sizeof(char)); int * result = (int *)malloc(numsSize*sizeof(int)).

2022-03-06 20:40:21 443

原创 【Leetcode】 C语言 414. Third Maximum Number

Third Maximum Number题目分析:创建三个变量分别保存数组中从大到小的前三个数,并计数。最后判断计数即可。int thirdMax(int* nums, int numsSize){ long num_1=LONG_MIN, num_2=LONG_MIN, num_3=LONG_MIN; int count=0; for(int j=0; j<numsSize; j++){ if(nums[j] > num_1){ .

2022-03-05 21:39:20 380

原创 【Leetcode】C语言 350. Intersection of Two Arrays II

Intersection of Two Arrays II题目分析:与349的区别是相同元素的出现次数也要相同。可以用哈希表创建保存元素值和出现个数,但是对于C语言来说哈希表很麻烦,所以直接用qsort先对两个数组排序,再使用双指针进行比较。int cmpfunc(const void * a, const void * b){ return (*(int*)a - *(int*)b);}int* intersect(int* nums1, int nums1Size, int* nums.

2022-03-04 20:27:55 387

原创 【Leetcode】 C语言 349. Intersection of Two Arrays

Intersection of Two Arrays题目分析:取小的那个数组创建空间,创建一个数组可以记录个数小的那个数组中的各个值标记为1,遍历第二个数组比较是否有相同元素的数组并放入res空间中输出。int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){ int i, j = 0; int size = nums1Size < nums2Si.

2022-03-03 22:14:29 351

原创 【Leetcode】 C语言 303. Range Sum Query - Immutable

Range Sum Query - Immutable题目分析:创建指针数组并分配合适的空间,计算好数组里元素的和,比如2-5之间的值用0-5的和减0-2的和。typedef struct { int *sum; int size;} NumArray;NumArray* numArrayCreate(int* nums, int numsSize) { NumArray *na = calloc(1, sizeof(NumArray)); na->su.

2022-03-02 21:42:38 205

原创 【Leetcode】C语言 283. Move Zeroes

Move Zeroes题目分析:将数组中所有的0元素移动到数组尾部。直接想法先判断是否为后移位,再根据位置再补0。void moveZeroes(int* nums, int numsSize){ if(numsSize < 2) return nums; int j = 0; for(int i = 0; i < numsSize; i++) { if(nums[i] != 0) { .

2022-03-01 21:33:21 295

原创 【Leetcode】C语言 268. Missing Number

Missing Number题目分析:直接想法,因为数组肯定会少一个数字,所以直接数组累加求和被加和减去就是缺少的数字int missingNumber(int *arr,int arrSize) { int nSum=0; int sum=0; int total=0; for (int i=0;i<arrSize+1;i++) { nSum+=i; } for (int i=0;i<arrSize;i++) { su.

2022-02-28 21:13:15 75

原创 【Leetcode】 C语言 228. Summary Ranges

博主日常leetcode学习记录分享,仅供学习参考

2022-02-28 20:51:31 83

原创 【Leetcode】 C语言 217. Contains Duplicate

Contains Duplicate题目要求查找数组中是否有两个相同的元素。首先想到的是双遍历比较,不过这样时间和空间复杂度都太大。可以先对数组进行排序再逐次比较相邻的两个元素。int cmp(const int* a, const int* b){ return *a - *b;}bool containsDuplicate(int* nums, int numsSize) { qsort(nums, numsSize, sizeof(int), cmp); if (n.

2022-02-26 22:01:22 386

原创 【Leetcode】C语言 171. Excel Sheet Column Number

Excel Sheet Column Number分别计算每一位后*26再加上最后一位,简单粗暴。效果也确实一般般。int titleToNumber(char * columnTitle){ if(columnTitle == 0)return NULL; int len = strlen(columnTitle); int sum = 0; int i,j,k; for(i=0;i<len;i++) { k=1; .

2022-01-11 22:25:11 255

原创 【Leetcode】C语言 169. Majority Element

Majority Element用一个状态变量记录顺序记录相同元素个数并用作判断。但是好像并没有判断出是否是大于一半的数量。int majorityElement(int* nums, int numsSize){ int major = -1; int cnt = 0; for (int i = 0; i < numsSize; i++) { if (major == -1 && cnt == 0) .

2022-01-10 22:24:10 161

原创 【Leetcode】 C语言 168. Excel Sheet Column Title

Excel Sheet Column Titlechar *convertToTitle(int n) { int i = 0; char *ch = malloc(255); char *ch1 = malloc(255); memset(ch, 0, sizeof(ch)); memset(ch1, 0, sizeof(ch1)); while(n > 0){ n--; *(ch + i) = (n%26) +.

2021-12-27 21:35:54 334

原创 【Leetcode】 C语言 167. Two Sum II - Input Array Is Sorted

Two Sum II - Input Array Is Sorted分别从两头向中间比较。/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) { int i;.

2021-12-27 21:27:13 441

原创 【Leetcode】 C语言 155. Min Stack

Min Stacktypedef struct Stack{ int data; int min; struct Stack *next;} MinStack;MinStack* minStackCreate() { MinStack *ptr = malloc(sizeof(MinStack)); ptr->next = NULL; ptr->min = INT_MAX; return ptr;}void minSt.

2021-12-27 21:11:33 104

原创 【Leetcode】C语言 160. Intersection of Two Linked Lists

Min Stacktypedef struct Stack{ int data; int min; struct Stack *next;} MinStack;MinStack* minStackCreate() { MinStack *ptr = malloc(sizeof(MinStack)); ptr->next = NULL; ptr->min = INT_MAX; return ptr;}void minSta.

2021-12-02 21:44:02 160

原创 【Leetcode】C语言 145. Binary Tree Postorder Traversal

void func(struct TreeNode* root, int* res, int* index){ if(NULL == root) return; func(root->left, res, index); func(root->right, res, index); res[(*index)++] = root->val;}int* postorderTraversal(struct TreeNode* root, int* retu.

2021-12-02 21:18:08 703

原创 【Leetcode】C语言 144. Binary Tree Preorder Traversal

递归。void func(struct TreeNode* root, int* res, int* index){ if(NULL == root) return; res[(*index)] = root->val; (*index)++; func(root->left, res, index); func(root->right, res, index);}int* preorderTraversal(struct TreeNode.

2021-12-02 20:53:50 98

原创 【Leetcode】C语言 141. Linked List Cycle

环形链表,结尾可能指向任意。bool hasCycle(struct ListNode *head) { struct ListNode *p,*q; p=head; q=head; while(p!=NULL&&q!=NULL&&q->next!=NULL) { p=p->next; q=q->next->next; if(p==q)return true;

2021-11-28 22:24:27 447

原创 【Leetcode】C语言 136. Single Number

简简单单异或一下结果就有了。int singleNumber(int* nums, int numsSize) { int i,result=0; for(i =0;i < numsSize;i++) result = nums[i]^result; return result;}

2021-11-28 22:19:25 82

原创 【Leetcode】C语言 125. Valid Palindrome

主要是加了一个tolower把大写变小写,剩下判断就是中间对换比较了。bool isPalindrome(char * s){ if(s == ' ')return 1; int len = strlen(s); char *str = (char *)malloc(len*sizeof(char)); int i; int j = 0; for(i = 0;i < len;i++){ if((s[i]>='0'&am.

2021-11-28 22:14:54 346

原创 【Leetcode】C语言 121. Best Time to Buy and Sell Stock

一开始用的双循环写判断。后来发现写一个循环遍历判断就行。int maxProfit(int* prices, int pricesSize) { int i; int price=prices[0]; int profit=0; for(i=1;i<pricesSize;i++) { if(prices[i]<price) price=prices[i]; if(profit<prices[i

2021-11-28 21:52:42 149

原创 【Leetcode】C语言 118. Pascal‘s Triangle

杨辉三角,主要在int[i][j]=int[i-1[j-1]+int[i][j-1]。int** generate(int numRows, int* returnSize, int** returnColumnSizes){ int i; *returnSize=numRows; *returnColumnSizes=(int*)malloc(numRows*sizeof(int)); int **res=(int**)malloc((*returnSize)*size

2021-11-26 20:45:52 67

原创 【Leetcode】C语言 112. Path Sum

Path SumGiven the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.A leaf is a node with no children.bool findPath(struct TreeNode* root.

2021-11-26 20:30:57 268

原创 【Leetcode】C语言 111. Minimum Depth of Binary Tree

异曲同工之妙。int findDepth(struct TreeNode* root,int step){ if(root->left == NULL&&root->right == NULL) return step+1; int leftStep = 0; int rightStep = 0; if(root->left) leftStep = findDepth(root->left,step+1

2021-11-26 20:18:32 207

原创 【Leetcode】C语言 110. Balanced Binary Tree

递归读取深度,再判断。int getDepth(struct TreeNode *root) { int left, right = 0; if (root == NULL) return 0; else{ left = getDepth(root->left)+1; right = getDepth(root->right)+1; } return left > right ? left : right;}bool isB

2021-11-26 16:46:27 72

原创 【Leetcode】C语言 108. Convert Sorted Array to Binary Search Tree

递归调用。struct TreeNode* sortedArrayToBST(int* nums, int numsSize){ if(numsSize == 0) return NULL; int mid = numsSize/2; struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = nums[mid]; root->left =

2021-11-26 16:15:45 194

原创 【Leetcode】C语言 104. Maximum Depth of Binary Tree

int maxDepth(struct TreeNode* root){ if(!root) return 0; int left = maxDepth(root->left); int right = maxDepth(root->right); return (left > right ? left : right) + 1; }

2021-11-26 11:14:47 87

原创 【Leetcode】C语言 101. Symmetric Tree

与之前相同只不过是判断左右节点对应是否相等。bool MyisSymmetric(struct TreeNode* Left,struct TreeNode* Right) { if(!Left&&!Right) return true; if(!Left||!Right) return false; if(Right->val!=Left->val) return false; return M

2021-11-26 10:55:16 238

原创 【Leetcode】C语言 100. Same Tree

不断判断是否相等直到结束。bool isSameTree(struct TreeNode* p, struct TreeNode* q){ int left, right; if(p == NULL && q == NULL){ return 1; } if(p == NULL || q== NULL){ return 0; } if(p->val != q->val){ retur

2021-11-26 10:36:21 75

原创 【Leetcode】C语言 94. Binary Tree Inorder Traversal

函数自调用遍历。void travel(struct TreeNode* root,int*ret,int*size){ if(root == NULL) return 0 ; travel(root->left,ret,size); ret[(*size)++] = root->val; travel(root->right,ret,size);}int* inorderTraversal(struct TreeNode* root.

2021-11-26 10:23:33 446

原创 【Leetcode】C语言 88. Merge Sorted Array

直接逆序判断比较,结果送到nums1数组。void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){ while(m > 0 && n > 0){ if(nums1[m-1] > nums2[n-1]){ nums1[m+n-1] = nums1[m-1]; m--; }

2021-11-25 21:37:28 83

原创 【Leetcode】C语言 83. Remove Duplicates from Sorted List

定义两个链表指向当前和下一个进行对比,保存当前的链表为输出。struct ListNode* deleteDuplicates(struct ListNode* head){ if(head == NULL || head->next == NULL)return head; struct ListNode *slow = head; struct ListNode *fast = head->next; while(fast != NULL){

2021-11-25 21:01:47 75

原创 【Leetcode】C语言 70. Climbing Stairs

本质是斐波那契数列的问题,可用迭代实现。int climbStairs(int n) { int *sum = (int *)malloc((n+1)*sizeof(int)); int i; sum[0] = 1; sum[1] = 1; for(i = 2; i <= n; i++) { sum[i] = sum[i-1]+sum[i-2]; } return sum[n];}...

2021-11-25 20:47:31 87

原创 【Leetcode】C语言 69. Sqrt(x)

简单省事调函数。int mySqrt(int x) { return sqrt(x);}

2021-11-25 20:19:52 169

原创 【Leetcode】C语言 67. Add Binary

逆序遍历相加和,保存进位到下一位的相加,flag为1则进位。最后一位为1则要拓展空间。最后将数组以中间反转输出。char* addBinary(char* a, char* b) { int flag = 0; int tempa, tempb; int lena = strlen(a); int lenb = strlen(b); int max = (lena>lenb) ? lena : lenb; char* sum= (char*)malloc(1+max*sizeof(cha

2021-11-25 20:17:50 134

原创 【Leetcode】C语言 66. Plus One

以最后一个元素开始,若有进位则保存到curr上并对原数据上加1,并定义一个新的数组。若没有进位则正常返回。int* plusOne(int* digits, int digitsSize, int* returnSize){ *returnSize = digitsSize; int i = 0; int curr = 1; for(i = digitsSize-1; i >= 0; i--){ curr = (digits[i]+curr)/10;.

2021-11-24 21:44:44 291

原创 【Leetcode】C语言 53. Maximum Subarray

定义max留下最大加和。#define max(a,b) ((a>b)?a:b)int maxSubArray(int* nums, int numsSize){ int ret = 0, g = INT_MIN; for(int i = 0; i < numsSize; ++i){ ret = max(ret + nums[i],nums[i]); g = max(ret, g); } return g;}...

2021-11-24 21:03:56 91

原创 【Leetcode】C语言 35. Search Insert Position

直接用遍历寻找,遍历结束说明目标大于数组的所有数,返回数组大小。或者用二分法应该会更快。int searchInsert(int* nums, int numsSize, int target){ if(nums == NULL)return NULL; for(int i=0;i<numsSize;i++){ if(target<=nums[i])return i; } return numsSize;}...

2021-11-24 20:45:56 181

原创 【Leetcode】C语言 28. Implement strStr()

偷了个懒,直接用strstr函数了,本质上双循环不用遍历全部可以得到结果。int strStr(char * haystack, char * needle){ char *loc = NULL; if ((NULL == haystack || '\0' == *haystack) && (NULL == needle || '\0' == *needle)) { return 0; } return (NULL == (l

2021-11-24 17:31:13 110

原创 【Leetcode】C语言 27. Remove Element

这个和26意思差不多。int removeElement(int* nums, int numsSize, int val){ if(nums == NULL) return NULL; int j=0; for(int i=0;i<numsSize;i++){ if(val!=nums[i]){ nums[j]=nums[i]; j++; } } ..

2021-11-24 16:56:51 58

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除