随笔分类 - 剑指offer
算法学习
摘要:58. II左旋转字符串 题目链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/ class Solution { public: //块交换问题: "abcdefg"; 先交换前2个:"bacdefg"; 再交换
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/ 堆排序 class Solution { public: vector<int> getLeastNumbers(vector<int>& arr, int k) { in
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/ 动态规划 状态表示:f[i]表示以nums[i]结尾的数组的连续子数组的最大和; 状态计算: f[i - 1] > 0, f[i] = f[i - 1
阅读全文
摘要:题目链接: https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/ 动态规划 状态表示: f[i][j]表示从左上角走到[i,j]处拿到的礼物的最大价值; 状态计算: f[i][j] = max(f[i - 1][j], f[i]
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/ 二分 时间复杂度:O(logn) 空间复杂度:O(1) class Solution { public: int missingNumber(vector<int>& nums
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/ 二分 时间复杂度:O(logn) 空间复杂度:O(1) class Solution { public: int search(ve
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof/ 朴素版 状态表示:相当于枚举在哪天卖, f[i]表示以prices[i]结尾的子数组的最大利润; 状态计算:f[i]的最大利润等于min(f[i - 1], pr
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/ 朴素版 状态表示:f[i][j]表示投掷wani枚骰子,j点数的出现次数; 状态计算:f[i][j] = f[i - 1][j - 1 ] + f[i - 1][j
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof/ 应该先去除字符串首部的空格,然后再判断正负。 难点在于处理溢出。 INT_MAX 2147483647 INT_MIN -214
阅读全文
摘要:题目链接:https://www.acwing.com/problem/content/description/86/ 双指针 快慢指针,快指针一次走两步,慢指针一次走一步,如果快指针无法继续前进,没有环; 否则,当快指针与慢指针第一次相遇以后,令快指针等于头结点,快慢指针各自每次走一步,再次相遇的
阅读全文
摘要:题目链接:https://leetcode cn.com/problems/jian sheng zi ii lcof/ 贪心
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/jian-sheng-zi-lcof/ 贪心 class Solution { public: int cuttingRope(int n) { if(n <= 3) return 1 * (n - 1); int res
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/ 递归 直接用二叉搜索树的后序遍历构建二叉树(利用二叉搜索树的性质,根节点的左子树都比他小,右子树都比他大),如果构建成功,返
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/ 思路 问题简化 考虑一个简单问题:数组中所有数组只有一个数出现了一次,其它数字都出现了两次? 利用异或的性质,两数相同,异或结果为0,
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/ 递归 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/ 递归 时间复杂度:O(n) 空间复杂度:O(n) /** * Definition for a binary tree
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/ 归并排序 class Solution { public: int merge(vector<int>& nums, int l, int r) { if(l
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/ 搜索与回溯 class Solution { public: int get_single_sum(int x){ int s = 0; while(x
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/ dfs class Solution { public: bool exist(vector<vector<char>>& board, string word)
阅读全文
摘要:题目链接:https://leetcode cn.com/problems/biao shi shu zhi de zi fu chuan lcof/
阅读全文