LeetCode
weixin_36094222
存储行业小学生一枚~
展开
-
LeetCode-202. 快乐数-C语言
bool ishappy(int n, int *cnt){ (*cnt)++;/* cnt to control the depth of callstack, max is 40 */ if((*cnt) >= 40) { return false; } if(n == 1) return true; int tmp=0; ...原创 2020-04-30 08:52:44 · 586 阅读 · 0 评论 -
LeetCode-5040-边框着色-C语言
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assum...原创 2019-04-29 17:14:52 · 248 阅读 · 0 评论 -
LeetCode-148-排序链表-C语言
使用常数存储空间,使用归并排序,分治的思想,不断将链表等分,只到单节点为止,再对链表进行合并排序。对链表等分时间复杂度O(lg2N),对两个排序链表进行合并,时间复杂度O(N),因此时间复杂度为O(Nlg2N)。代码如下。/** * Definition for singly-linked list. * struct ListNode { * int val; * s...原创 2019-05-05 19:57:26 · 396 阅读 · 0 评论 -
LeetCode-5050-从二叉搜索树到更大和树-C语言
类似于二叉树的中序遍历,只是首先访问右子树,另外在递归过程中,要传一个总的值记录当前已访问节点的和。代码如下。void set(struct TreeNode *node, int *val){ if(!node){ return; } set(node->right, val); node->val += *val; ...原创 2019-05-05 20:01:17 · 315 阅读 · 0 评论 -
LeetCode-141-环形链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;/* 使用快慢指针,快指针每次走两步,慢指针每次一步* 如果两指针相遇表示是环形,否则表示非环形...原创 2019-05-08 17:21:21 · 310 阅读 · 0 评论 -
LeetCode-1019-链表中的下一个更大节点-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//** * Note: The returned array must be malloced, assume caller calls free(). ...原创 2019-05-08 19:04:57 · 895 阅读 · 0 评论 -
LeetCode-876-链表的中间节点-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;struct ListNode* middleNode(struct ListNode* head...原创 2019-05-08 19:15:41 · 216 阅读 · 0 评论 -
LeetCode-2-两数相加-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;struct ListNode* addTwoNumbers(struct ListNode* l1...原创 2019-05-06 11:18:21 · 383 阅读 · 0 评论 -
LeetCode-19- 删除链表的倒数第N个节点-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//* Description: * 使用快慢指针的方法, 快指针比慢指针先跑n个节点, *等快指针到头的时候,慢指针所指向的就是待删除的节点。 */t...原创 2019-05-06 11:40:19 · 283 阅读 · 0 评论 -
LeetCode-21-合并两个有序链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { st...原创 2019-05-06 11:49:50 · 199 阅读 · 0 评论 -
LeetCode-707-设计链表-C语言
typedef struct ListNode Node;typedef struct { /* 一个虚拟的节点,node.next才是链表的头节点 */ Node node; /* 用于记录链表的尾节点 */ Node *rear; /* 用于记录链表的长度,根据长度判断,检索插入等操作是否合法 */ int len;} ...原创 2019-05-08 21:35:36 · 416 阅读 · 0 评论 -
LeetCode-817-链表组件-C语言
typedef struct ListNode Node;int numComponents(struct ListNode* head, int* G, int GSize){ int arr[10000]; int i; Node *p = head; int cnt = 0; int last_flag = 0;/* 用于标志上一个节点是否存在于G...原创 2019-05-08 22:09:11 · 169 阅读 · 0 评论 -
LeetCode-89-格雷编码-C语言
/* * 算法思想: * 使用递归的思想,求N位grey码,将其转化为N-1位的grey码。 * 一位:0 两位: 01 三位:001 * 1 00 000 * 10 010 * 11 011 * ...原创 2019-05-13 20:22:26 · 337 阅读 · 0 评论 -
LeetCode-23-合并K个排序链表-C语言
方法一:递归分治使用递归的方法,将数组不断分下去,一分为二,递归截至条件为,数组中元素为0个,或者1个。对分治后的两个链表进行合并排序。分的时间复杂度为O(lgN),合并的时间复杂度为O(N),因此时间复杂度为O(NlgN);typedef struct ListNode Node;struct ListNode* mergeTwoLists(struct ListNode* l1,...原创 2019-05-06 14:08:05 · 808 阅读 · 0 评论 -
LeetCode-24-两两交换链表中的节点-C语言
使用递归的方法递归的截至条件为链表小于等于一个节点,每次仅处理头节点head以及head->next节点,使二者交换。/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct L...原创 2019-05-06 14:35:10 · 512 阅读 · 0 评论 -
LeetCode-430-扁平化多级双向链表-C语言
class Solution {public: Node* get(Node *head, Node **rear){ Node *last = NULL; Node *p = head; while(head){ last = head; head = h...原创 2019-05-09 06:36:52 · 224 阅读 · 0 评论 -
LeetCode-725-分割链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//** * Note: The returned array must be malloced, assume caller calls free(). ...原创 2019-05-09 07:37:41 · 290 阅读 · 0 评论 -
LeetCode-206-反转链表-C语言
类似于滑动窗口,用三个指针组成一个窗口,每次移动一个,将中间指针的mid->next = rear,相当于反转了一个节点,不断的滑动到最后,完成一个链表的反转。typedef struct ListNode Node;Node *reverseList(Node *head){ Node *front = head; Node *mid = NULL; Node ...原创 2019-05-06 16:24:39 · 470 阅读 · 0 评论 -
LeetCode-93-复原IP地址-C语言
/* 算法思想: * 套用回溯算法的框架可以解决问题,注意排除以0开头,以及超过255时的。 * * * *//* set one result string */void get_ret(char **ret, int *ret_index, int *stack, int len, char *s){ int index = 0; int index_s = 0...原创 2019-05-13 22:52:52 · 1781 阅读 · 0 评论 -
LeetCode-25-K个一组反转链表-C语言
使用递归的方式,递归截至的条件为链表为NULL。每次递归,反转K个节点完成后,将将链表尾指向下次递归的返回值。如果某一次递归反转后,未达到K个节点,表示节点数小于K,则将链表恢复原状后(重新反转)返回即可。代码 如下。/** * Definition for singly-linked list. * struct ListNode { * int val; * s...原创 2019-05-06 17:00:41 · 351 阅读 · 0 评论 -
LeetCode-784-字母大小写全排列-C语言
/* * 算法思想: * 使用递归的算法思想,将求len的问题转化为求len-1的问题,当len==0时,实际是有1个NULL字符串作为结果的。 * 当len==1时,如果s[0]为数字,则返回结果数ret_index不变,否则变为2倍,并分配相应的内存处理。 * */int get_len(char *s){ int i=0; while(*s++ !=...原创 2019-05-14 11:36:17 · 989 阅读 · 0 评论 -
LeetCode-1-两数之和-C语言
构造一个哈希表,依次遍历每个元素,遍历当前元素时,查找已存在的哈希表,目的值target-nums[i],如果找到,则直接返回当前和target-nums[i]对应的index即可。#define LEN 1000typedef struct hash_node{ int val; int i; struct hash_node *next;} Node;in...原创 2019-05-06 20:34:16 · 986 阅读 · 0 评论 -
LeetCode-207-课程表-C语言
/* * 算法思想: * 转化为有向图判环问题, * 1.使用二维矩阵存储节点关系, * 2. 使用回溯法; * 3. 该算法时间复杂度过高,对于num==2000的测试用例,会超时,因此对num==2000的时候,进行撞大运。 * */bool find(int num, int **parr, int pm, int pn, int vst[num], int index) ...原创 2019-05-19 10:06:02 · 947 阅读 · 0 评论 -
Leetcode-925-长按键入-C语言
bool isLongPressedName(char * name, char * typed){ int i=0, j=0; while(name[i] != '\0' && typed[j] != '\0'){ if(name[i] == typed[j]){ i++;j++; cont...原创 2019-05-06 22:29:33 · 179 阅读 · 0 评论 -
LeetCode-191-位1的个数-C语言
int hammingWeight(uint32_t n) { int ret=0; while(n){ if(n & 1) ret++; n >>= 1; } return ret;}原创 2019-05-14 14:51:30 · 165 阅读 · 0 评论 -
LeetCode-401-二进制手表-C语言
/** * 算法思想: * 套用回溯算法框架。 * */int get_num(char *s, int len){ int ret=0; int lenth = len; while(len--){ ret *= 2; ret += *s++ - '0'; } /* 4位为小时,6位为分钟,合法才返回整...原创 2019-05-14 14:57:14 · 635 阅读 · 0 评论 -
LeetCode-216-组合总和III-C语言
/** * 算法思想: * 套用回溯算法框架 * * */void get_one_ret(int *stack, int len, int target, int **ret, int *ret_index) { int cnt = 0; int i=0; while(i<len){ cnt += stack[i]; ...原创 2019-05-14 15:32:08 · 562 阅读 · 0 评论 -
LeetCode-5063-最后一个石头重量-C语言
int cmp(int *a, int *b){ return *a - *b;}void print_arr(int *arr, int len){ int i; for(i=0; i<len; i++) printf("%d,", arr[i]); printf("\n");}int lastStoneWeight(in...原创 2019-05-19 14:43:57 · 315 阅读 · 0 评论 -
LeetCode-5064. 删除字符串中的所有相邻重复项-C语言
#define LEN 0xffffchar * removeDuplicates(char * S){ char *ret = (char *)malloc(sizeof(char) * LEN); int index = 0; int i=0; if(*S == '\0') return NULL; for(i=0; S[i]!...原创 2019-05-19 14:44:52 · 549 阅读 · 0 评论 -
LeeCode-1048-最长字符串链-C语言
/* check two words can be word chain by hash table */bool check(int num, int start, int end, int arr[num][26]){ int i;int flag = 0; for(i=0; i<26; i++){ if(arr[end][i] == ar...原创 2019-05-19 14:46:56 · 390 阅读 · 0 评论 -
LeetCode-208-实现前缀树-C语言
typedef struct Node { struct Node *letter[128];} Trie;Trie* trieCreate() { Trie *node = (Trie *)malloc(sizeof(Trie)); memset(node, 0, sizeof(Trie)); return node;}/** In...原创 2019-05-19 15:38:50 · 672 阅读 · 0 评论 -
LeetCode-131-分割回文串-C语言
/** * 算法思想: * 套用回溯算法框架 *//* check current string is valid or not */bool check(char *s, int len){ int i=0, j=len-1; while(i<j){ if(s[i] != s[j]) return false; i++;j--...原创 2019-05-14 17:02:17 · 763 阅读 · 0 评论 -
LeetCode-605-种花问题-C语言
bool canPlaceFlowers(int* arr, int len, int n){ int i; for(i=0; i<len; i++){ if(arr[i]) continue; /* 当前位置不在边缘 */ if(i>=1 && i<len...原创 2019-05-19 16:06:26 · 609 阅读 · 0 评论 -
LeetCode-61-旋转链表-C语言
typedef struct ListNode Node;inline Node *rotateOne(Node *head){ Node *p = head, *last_p = NULL; if(!p){ return NULL; } while(p->next){ last_p = p; ...原创 2019-05-07 10:25:15 · 168 阅读 · 0 评论 -
LeetCode-445-两数相加II-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;/* 反转链表 */Node *reverse(Node *head){ Node no...原创 2019-05-09 18:41:08 · 585 阅读 · 0 评论 -
LeetCode-237-删除链表中的节点-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;void deleteNode(struct ListNode* node) { Node ...原创 2019-05-09 18:49:29 · 533 阅读 · 0 评论 -
LeetCode-328-奇偶链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;/* 算法思想: * 生成两个临时链表,节点以奇偶分别添加到两个临时链表上; * 最后将两个临...原创 2019-05-09 19:01:05 · 321 阅读 · 0 评论 -
LeetCode-234-回文链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//* * 算法思想: * 将list后半段反转,与前半段相比较,如果相同则为回文 * 否则返回false */typedef struct Li...原创 2019-05-09 19:27:30 · 405 阅读 · 0 评论 -
LeetCode-203-移除链表元素-C语言
/* * 算法思想: * 将符合条件的节点,添加到一个新链表中,返回新链表 * */struct ListNode* removeElements(struct ListNode* head, int val){ Node node; node.next = NULL; Node *rear = &node; Node *last = NULL;...原创 2019-05-09 20:13:10 · 267 阅读 · 0 评论 -
LeetCode-3-无重复字符的最长子串-C语言
int lengthOfLongestSubstring(char* s) { int start=0,maxLen=0,index=0; char c; int arr[256]; int len ; memset(arr, 0, sizeof(arr)); for(; s[index] !='\0';index++){ ...原创 2019-05-07 14:04:38 · 736 阅读 · 0 评论