代码改变世界

随笔分类 -  leetcode

leetcode中尽量不用static变量

2020-08-25 22:59 by legend聪, 603 阅读, 收藏,
摘要: 容易产生和本地不一致的结果。可能与加载类的过程有关。leetcode每次加载然后测试,之后变量会变成脏数据,也就是保存上一次的结果。前提是用static,不用的话就没关系 阅读全文

leetcode63 不同路径

2020-03-20 22:34 by legend聪, 230 阅读, 收藏,
摘要: 这种题目大多不用搜索,首选简单数学方法和动态规划。这里用的是动态规划如果网络只有两行的话可以用数学方法。简单的dp问题,题目求什么dp数组设什么就好。转移条件也很简单。注意判断边界情况,数组设置成long long不然会爆int。 class Solution { public: int uniqu 阅读全文

leetcode1143最长公共子序列

2020-03-19 23:15 by legend聪, 232 阅读, 收藏,
摘要: 先定义dp[i][j]是第一个字符串的i位置和第二个字符串j位置之前最长的公共子序列数目。返回的时候返回dp[lenA][lenB class Solution { public: int longestCommonSubsequence(string text1, string text2) { 阅读全文

leetcode138. 复制带随机指针的链表

2020-03-17 18:14 by legend聪, 115 阅读, 收藏,
摘要: 题目的意思比较难理解,分为3步。第一步建立二重链表,第二步random指针的建立,第三步拆分二重链表。 /* // Definition for a Node. class Node { public: int val; Node* next; Node* random; Node(int _val 阅读全文

leetcode48 旋转图像

2020-03-14 11:58 by legend聪, 215 阅读, 收藏,
摘要: 这道题只需要搞明白矩阵位置跳转逻辑即可,首选是反着赋值,只花费额外变量存储第一个值即可,然后弄清跳转过程。 class Solution { public: void rotate(vector<vector<int>>& matrix) { if(matrix.empty()&&matrix[0] 阅读全文

leetcode54 螺旋矩阵

2020-03-13 01:48 by legend聪, 113 阅读, 收藏,
摘要: 题目的思路是每次确定左上顶点和右下顶点,然后进行一个框的打印,注意边界条件。然后设计一个打单行和单列的算法。一个小错误如果vector为空的话调用.size()方法会引发空指针异常,所以先判空再进行下面的操作。 class Solution { public: vector<int> spiralO 阅读全文