【LC数组】移动零
题目描述: 题解:12345678910111213141516171819class Solution {public: void moveZeroes(vector<int>& nums) { int k = 0; for(int i = 0; i < nums.size(); ++i) { if(nums[i] != 0) { nums[k] = nums[i]; ++k; } } //剩余置零 for(int i = k; i < nums.size(); ++i) { nums[i] = 0; } }};
【LC】二叉树的最大深度
题目描述: 题解:123456789class Solution {public: int maxDepth(TreeNode* root) { if(root == nullptr) return 0; else return std::max(maxDepth(root->left), maxDepth(root->right)) + 1; }};
【LC】环形链表
题目描述: 题解:12345678910111213141516171819class Solution {public: bool hasCycle(ListNode *head) { if(head == nullptr) return false; ListNode* slow = head; ListNode* fast = head; while(fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; if(slow == fast) { return true; } } return false; } ...
【LC】回文链表
题目描述: 题解一:deque1234567891011121314151617181920212223242526class Solution {public: bool isPalindrome(ListNode* head) { if(head == nullptr) return false; //使用双端队列 deque<int> deq; while(head != nullptr) { deq.push_back(head->val); head = head->next; } while(!deq.empty()) { if(deq.front() != deq.back()) { return false; } deq.pop_front(); if(!deq.empty()) { ...
【LC】合并两个有序链表
题目描述: 题解一:链接+排序 直接拼接两个链表 给链表排序 输出链表头结点 123456789101112131415161718192021222324252627282930313233343536373839404142class Solution {private: int len(ListNode* head) { int count = 0; while(head != nullptr) { ++count; head = head->next; } return count; }public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { if(list1 == nullptr) return list2; if(list2 == nullptr) return l ...
【LC】反转链表
题目描述 题解一:置换数据(栈)1234567891011121314151617181920212223242526class Solution {public: ListNode* reverseList(ListNode* head) { if(head == nullptr) return nullptr; ListNode* ptr = head; ListNode* res = head; //返回值 //使用stack stack<int> st; while(head != nullptr) { st.push(head->val); head = head->next; } while(!st.empty()) { ptr-&g ...
【LC】删除链表的倒数第N个节点
题目描述 题解求得链表长度,得出正向遍历的次数,使用快慢指针,删除对应结点 123456789101112131415161718192021222324252627class Solution {public: int getlen(ListNode* head) { int count = 0; while(head != NULL) { ++count; head = head->next; } return count; } ListNode* removeNthFromEnd(ListNode* head, int n) { if(n <= 0) return nullptr; ListNode* fast = head; ListNode* slow = NULL; int forward = getlen(h ...
【LC】删除链表中的节点
题目描述解题思路: 题解12345678class Solution {public: void deleteNode(ListNode* node) { node->val = node->next->val; node->next = node->next->next; //free(node); }}; 本题心得:删除一节点不一定非要知道它的前驱,可以用后继覆盖掉要删的,再把后继删除即可!! 这里借用这位网友的故事:很有意思
【数据结构】二叉排序树
一、关于二叉排序树二叉排序树的特点:中序遍历的顺序是有序的。中序遍历结果:12 16 23 35 42 60逆中序遍历结果:60 42 35 23 16 12 二、二叉排序树结构设计Bst.h123456789101112131415#ifndef BST_H#define BST_H//二叉搜索树(二叉排序树) //三叉链式结点typedef int ElemType;typedef struct BstNode{ ElemType key; struct BstNode* leftchild; struct BstNode* rightchild; struct BstNode* parent;}BstNode, *BstTree;#endif 三、二叉排序树的使用(1)申请节点12345678//申请结点BstNode* BuyNode(){ BstNode* node = (BstNode*)malloc(sizeof(BstNode)); if (NULL == node) return NULL; memset(node, 0, sizeof ...
【数据结构】二叉树的使用
寻找双亲结点1234567891011121314151617181920//查找双亲BtNode* FindParent(BtNode* root, BtNode* pos){ if (root == NULL || pos == NULL) return NULL; if (root->leftchild == pos || root->rightchild == pos) { return root; } else { BtNode* p = FindParent(root->leftchild, pos); if (p == NULL) { p = FindParent(root->rightchild, pos); } return p; }} 查找val值1234567891011121314151617181920212223242526//二叉树中查找val是否存在BtNode* Find(BtNode* p, Element val){ ...