Leetcode
leetcode刷题总结,开头有解题思路。
weixin_36094222
存储行业小学生一枚~
展开
-
Leetcode-237.删除链表中的节点-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */void deleteNode(struct ListNode* node) { struct ListNode *p = node, *last=NULL; if(!p) return; while(p->next) {原创 2020-05-09 08:36:29 · 346 阅读 · 0 评论 -
LeetCode-258. 各位相加-C语言
int addDigits(int num){ if(num > 9) return num%9 ? num%9 : 9; return num;}int addDigits(int num){ int tmp, cnt; while(num >=10) { tmp = num; cnt = 0;...原创 2020-04-29 21:40:52 · 348 阅读 · 0 评论 -
LeetCode-5056-不邻接植花-C语言
/* * 算法思想: * 套用经典回溯算法框架,注意需要带返回值的回溯; * 题目要求的是返回一种染色方式,并未要求全部的染色方式,一旦符合条件直接返回即可,而不是遍历所有的可能。 * 详细见代码中注释。 * *//* check函数用于检查当前index位置染得颜色是否符合要求,,index之前的染色一定符合要求 */bool check(int *arr, int...原创 2019-05-13 16:24:10 · 304 阅读 · 0 评论 -
LeetCode-77-组合-C语言
/** * 算法思想: * 经典回溯算法,递归返回的条件为临时栈中的元素个数为k个; * * * * **/void rec(int n, int k, int index, int *stack, int *stack_index, int *ret_index, int **ret){ if(*stack_index == k){ ret[*ret_i...原创 2019-05-12 20:37:58 · 622 阅读 · 3 评论 -
LeetCode-47-全排列II-C语言
/* * 算法思想: * 经典回溯算法框架能解决的问题,其中回溯返回的条件为index到头了 * */void swap(int *a, int *b){ int tmp = *a; *a = *b; *b = tmp;}void rec(int *arr, int len, int index, int **ret, int *ret_index...原创 2019-05-12 20:12:34 · 312 阅读 · 0 评论 -
LeetCode-46-全排列-C语言
/* * 算法思想: * 经典回溯算法框架能解决的问题,其中回溯返回的条件为index到头了 * */void swap(int *a, int *b){ int tmp = *a; *a = *b; *b = tmp;}void rec(int *arr, int len, int index, int **ret, int *ret_index)...原创 2019-05-12 18:58:34 · 1748 阅读 · 1 评论 -
LeetCode-40-组合总和II-C语言
/* * 算法思想: * 回溯算法,套用回溯框架,很容易得出答案,详见代码注释。 * 与39提类似,但需要注意,1. 下次回溯需要从index+1开始; 2.每次添加前查询看该结果是否已经存在。 * */void rec(int *arr, int len, int index, int *stack, int *stack_index, int target, ...原创 2019-05-12 18:12:49 · 299 阅读 · 0 评论 -
LeetCode-39-组合总和-C语言
/* * 算法思想: * 回溯算法,套用回溯框架,很容易得出答案,详见代码注释。 * * */void rec(int *arr, int len, int index, int *stack, int *stack_index, int target, int **ret, int *ret_index, int *ret_size){ if(index...原创 2019-05-12 17:16:39 · 808 阅读 · 0 评论 -
LeetCode-98-验证二叉搜索树-C语言
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */void find_max(struct TreeNode *root, int *val){ ...原创 2019-05-12 10:24:04 · 733 阅读 · 0 评论 -
LeetCode-7-整数反转-C语言
/** 算法思想:* 先将给定的值转换为正数,反转这个正数,再根据之前的符号返回相应的结果***/int get_len(char *s){ int i=0; while(*s != '\0'){ s++; i++; } return i;}int get_num(char *s){ int j = get...原创 2019-05-11 22:43:23 · 268 阅读 · 0 评论 -
LeetCode-6-之字形变换-C语言
/** 算法思想:* 使用numRows个字符数组,分别存储每行的字符;* 按照之字形的方式分别将所给字符填进numRows个字符数组;* 最后将numRows个字符数组中的字符取出来放到s数组中,返回s即可。***/int get_len(char *s){ int i=0; while(*s != '\0'){ s++; ...原创 2019-05-11 22:13:23 · 199 阅读 · 0 评论 -
LeetCode-143-重排链表-C语言
/* * 算法思想: * 将链表分为两段,其实是构造了两个链表,第一个链表使用的节点为前(len+1)/2个节点; * 第二个链表为len-(len+1)/2个节点;第二个链表构造的同时反转该链表; * 如链表1,2,3,4,5,6,7; 构造后list1 = 1,2,3,4; list2 = 7,6,5; * 再将两个链表构造成一个链表,分别循环取list1,list2中的节点即可。构...原创 2019-05-10 22:48:10 · 1019 阅读 · 0 评论 -
LeetCode-138-复制带随机指针的链表-C语言
/* * 算法思想: * 第一遍遍历所有节点,建立新链表,先将所有random指针置为NULL; * 第二遍,为新链表建立random指针,参照旧链表的指向; * 时间复杂度O(N^2),建立random指针时每个节点都要访问所有节点查询指向哪个节点。 * **/class Solution {public: Node* copyRandomList(Node* head...原创 2019-05-10 21:47:36 · 335 阅读 · 0 评论 -
LeetCode-92-反转链表II-C语言
typedef struct ListNode Node;/* * 算法思想:* 将链表分为三段,index在M与N之间的为第二段,将第二段反转* 并且记录第一段的尾,第三段的头,用来连接三段链表*/struct ListNode* reverseBetween(struct ListNode* head, int m, int n){ Node *p = head; ...原创 2019-05-10 14:01:51 · 452 阅读 · 0 评论 -
LeetCode-160-相交链表-C语言
/** * 算法思想: * 如果二者有相交的点,则相交后面的点个数相同,因此相交的点必在后N个点,N为二者中短的长度,M为较长的长度; * 因此,对于较长的链表,前M-N肯定不为相交点; * 所以先将较长的放过M-N点后,比较两个链表的节点,一个一个比较,如果有相同则为交点,否则无交点。 * */typedef struct ListNode Node;inline int...原创 2019-05-09 22:32:50 · 446 阅读 · 1 评论 -
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-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-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-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-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-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-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-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-707-设计链表-C语言
typedef struct ListNode Node;typedef struct { /* 一个虚拟的节点,node.next才是链表的头节点 */ Node node; /* 用于记录链表的尾节点 */ Node *rear; /* 用于记录链表的长度,根据长度判断,检索插入等操作是否合法 */ int len;} ...原创 2019-05-08 21:35:36 · 415 阅读 · 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-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-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-86-分割链表-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;struct ListNode* partition(struct ListNode* head,...原创 2019-05-07 20:18:18 · 258 阅读 · 0 评论 -
LeetCode-83- 删除排序链表中的重复元素-C语言
typedef struct ListNode Node;struct ListNode* deleteDuplicates(struct ListNode* head) { Node node; node.val = INT_MAX; node.next = NULL; Node *rear = &node; Node *last = NULL;...原创 2019-05-07 19:59:23 · 376 阅读 · 1 评论 -
LeetCode-82-删除排序链表中的重复元素 II-C语言
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */typedef struct ListNode Node;/* 方法一:重新构造一个新的List */struct ListNode* deleteDup...原创 2019-05-07 19:44:04 · 415 阅读 · 0 评论 -
LeetCode-53-最大子序和-C语言
将前面累加和为0的项直接丢掉,重新计算。int maxSubArray(int* nums, int len){ int max=INT_MIN, tmp=0; int i=0; for(i=0; i<len; i++){ tmp += nums[i]; if(tmp > max) max = tmp; /* if current sum v...原创 2019-05-07 16:58:22 · 419 阅读 · 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 评论 -
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-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-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-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-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-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-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-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 评论