03 2021 档案
摘要:杨辉三角题,利用动态规划逆推 class Solution { public: vector<int> getRow(int rowIndex) { vector<int> row(rowIndex + 1); row[0] = 1; for (int i = 1; i <= rowIndex; +
阅读全文
摘要:动态规划题,根据动态规划,选择最短的路径 class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int n = triangle.size(); vector<int> f(n); f[0] = tria
阅读全文
摘要:滑动窗口问题,找出最大的不重复的字符串 class Solution { public: int lengthOfLongestSubstring(string s) { unordered_set<char> occ; int n = s.size(); int rk = -1, ans = 0;
阅读全文
摘要:一个典型的递归算法 class Solution { public: bool hasPathSum(TreeNode *root, int sum) { if (root == nullptr) { return false; } if (root->left == nullptr && root
阅读全文
摘要:运用一个递归进行二叉树的翻转,很基本的题目。、 class Solution { public: TreeNode* invertTree(TreeNode* root) { if (root == nullptr) { return nullptr; } TreeNode*left=invertT
阅读全文
摘要:直接判断,循环遍历,只要是前面是空格并且自己不为空,就是个新单词。 class Solution { public: int countSegments(string s) { int flag=1; int count=0; for(int i=0; i<s.length();i++) { if(
阅读全文
摘要:很简单的题,一个个遍历,将当前节点的next设成上一个节点,依次类推。 class Solution { public: ListNode* reverseList(ListNode* head) { ListNode*prev=NULL; ListNode*curr=head; while(cur
阅读全文
摘要:基本的层序遍历 class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector <vector <int>> ret; if (!root) { return ret; } queue <TreeNod
阅读全文
摘要:很简单的题,会用哈希表即可,将哈希表存入出现过的元素,再一个个遍历未出现的元素,如果不用哈希表的话复杂度会增高。 class Solution { public: int missingNumber(vector<int>& nums) { int n=nums.size(); unordered_
阅读全文
摘要:先对间隔数组每一个数组的第一个数子进行排序,之后再进行合并。 class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { if (intervals.size() == 0) { retur
阅读全文
摘要:加强版的快慢指针题,需要得出在哪里环的位置,就需要一个从头指针,一个slow,相碰的地方为环所在地,这是一个数学题。 这是快慢指针的方法 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *slow =
阅读全文
摘要:一个快慢指针的题,slow指针一次一格,fast指针一次两格,当相碰便是有环。减少了时间复杂度,如果使用哈希表解答的话要注意,哈希表存储的必须是地址,是地址匹配才可以,数值匹配是不行的。 class Solution { public: bool hasCycle(ListNode *head) {
阅读全文
摘要:很简单的题目,一个容器平 class Solution { public: vector<int> sortedSquares(vector<int>& nums) { int n=nums.size(); vector<int>num2(n); for(int i=0;i<=nums.size()
阅读全文
摘要:建造一个hash表,pos指针遍历链表,有不同的就存入hash表,相同的则跳过。题目思路比较简单,活用容器即可。 class Solution { public: ListNode* removeDuplicateNodes(ListNode* head) { if(head==NULL) { re
阅读全文

浙公网安备 33010602011771号