
leetcode
leetcode
manguoyu
这个作者很懒,什么都没留下…
展开
-
leetcode: 3 无重复字符的最长子串
int lengthOfLongestSubstring(char * s) { int len = strlen(s); if (len <= 1) { return len; } int hash[1024] = {0}; int left = 0; int right = 0; int max = 0; while (s[right] != '\0') { if (hash[s[right]] &.原创 2021-12-15 11:17:47 · 287 阅读 · 0 评论 -
leetcode: 1011. 在 D 天内送达包裹的能力
int getDays(int* weights, int weightsSize, int mid){ int days = 1; int count = 0; for (int i = 0; i < weightsSize; i++) { if ((count + weights[i]) > mid) { days++; count = 0; } count += we.原创 2021-10-30 08:57:47 · 121 阅读 · 0 评论 -
leetcode:62. 不同路径
https://leetcode-cn.com/problems/unique-paths/// 三步走// 1. 确定数组意义:dp[i][j] :从 (0,0) 到 (i,j)有多少条路径// 2. 找出关系式: dp[i][j] = dp[i-1][j] + dp[i][j-1]// 3. 初始值: dp[0][0] = 1// dp[i][0] = 1// dp[0][j] = 1int uniqu原创 2021-09-08 19:05:43 · 94 阅读 · 0 评论 -
leetcode: 237. 删除链表中的节点
// node就是要删除的节点, 且不是末尾结点// 直接将node->next的值赋给node,同时node->next = node->next->next;void deleteNode(struct ListNode* node) { struct ListNode *next = node->next; node->val = node->next->val; node->next = node->next.原创 2021-08-15 10:54:19 · 74 阅读 · 0 评论 -
leetcode: 283. 移动零
// 记录当前0的个数, 当遍历到非0时, 根据前面有几个0, 既能得知要往前挪几位void moveZeroes(int* nums, int numsSize){ if (NULL == nums || numsSize <= 1) return; int i; int zero_num = 0; for (i = 0; i < numsSize; i++) { if (nums[i] == 0) { zero_n.原创 2021-08-15 10:36:40 · 65 阅读 · 0 评论 -
leetcode: 11. 盛最多水的容器
利用双指针, 加一个临时最大值。临时最大值 = MIN(height[left] , height[right])* (right - left), 同时根据哪个矮就往前或者往后挪一位int maxArea(int* height, int heightSize){ if (NULL == height) return 0; if (heightSize < 2) return 0; int left = 0; int right = heightSi.原创 2021-08-15 10:24:39 · 75 阅读 · 0 评论 -
leetcode: 53. 最大子序和
#define MAX(a,b) (a) > (b) ? (a) : (b)int maxSubArray(int* nums, int numsSize){ int max = nums[0]; int sum = nums[0]; for (int i = 1; i < numsSize; i++) { if (sum < 0) { // 当前sum值小于0, 那么不管怎么加都会把后续的值变小, 所以直接置为0, 重新计算sum值 .原创 2021-08-06 11:04:00 · 71 阅读 · 0 评论 -
leetcode(动态规划):198. 打家劫舍
//1. 定义 dp[i]: 为到第i间房最多偷多少//2. 关系: 有两种偷法: 不偷:dp[i] = dp[i-1]// 偷:dp[i] = nums[i] + dp[i-2] // 前面一个房子不能偷// dp[i] = MAX(dp[i-1], nums[i] + dp[i-2])//3. 初始值// 只有一个房间,偷之: dp[0] = num[0]// 只有两个房间,偷其中金额较高的那个房间:.原创 2021-08-06 10:36:44 · 79 阅读 · 0 评论 -
leetcode(动态规划):64. 最小路径和
// 三步走// 1. 确定数组意义:dp[i][j] 当前走到dp[i][j]的最小路径// 2. 找出关系式: dp[i][j] = MIN(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]// 3. 初始值: dp[0][0] = grid[0][0]// dp[i][0] = grid[i][0] + dp[i - 1][0];// dp[0][j] = grid[0][j] .原创 2021-08-05 15:33:07 · 81 阅读 · 0 评论 -
leetcode(动态规划):62. 不同路径
https://leetcode-cn.com/problems/unique-paths/// 动态规划三步走:// 1. 定义数组a[i][j]: 到a[i][j] 总共有多少条路径// 2. 找出数组元素关系:a[i][j] = a[i-1][j] + a[i][j - 1]// 3. 初始条件: a[0][0] = 0, a[0][j] = 1, a[i][0] = 1int uniquePaths(int m, int n) { if (m == 1 &原创 2021-08-05 11:07:54 · 99 阅读 · 0 评论 -
leetcode(回溯法):46. 全排列
/** * 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, assume caller calls free(). */ /*回溯法框架解题:result = []def.原创 2021-08-03 11:23:27 · 124 阅读 · 0 评论 -
leetcode(回溯法):17. 电话号码的字母组合
/** * Note: The returned array must be malloced, assume caller calls free(). */ /*回溯法框架解题:result = []def backtrack(路径, 选择列表): if 满足结束条件: result.add(路径) return for 选择 in 选择列表: 做选择 backtrack(路径, 选择列表) .原创 2021-08-02 16:35:24 · 86 阅读 · 0 评论 -
leetcode:98. 验证二叉搜索树
// 引入min和max// 左子树结点的最大值一定小于root->val// 右子树结点的最小值一定大于root->valbool isValidBSTSub(struct TreeNode *node, int min, int max) { if (NULL == node) return true; if (node->val < min || node->val > max) return false; if (node-&g.原创 2021-07-27 11:17:16 · 71 阅读 · 0 评论 -
leetcode:20. 有效的括号 (C语言)
利用栈, 注意边界// 将左括号入栈,一旦遇到右括号就出栈,出栈的元素与左括号不匹配,直接返错bool isValid(char * s){ char stack[10000] = {0}; int top = -1; while (*s != '\0') { if (*s == '(' || *s == '{' || *s == '[') { stack[++top] = *s; } if (*s =原创 2021-07-26 20:21:19 · 135 阅读 · 0 评论 -
leetcode: 86. 分隔链表
https://leetcode-cn.com/problems/partition-list//** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x){ if (NULL == head) retu原创 2021-07-23 15:30:03 · 72 阅读 · 0 评论 -
leetcode: 24. 两两交换链表中的节点
借用临时接节点/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* swapPairs(struct ListNode* head){ if (head == NULL || head->next == NULL) return head; struct Li原创 2021-07-23 11:03:22 · 76 阅读 · 0 评论