剑指
语胡言胡
这个作者很懒,什么都没留下…
展开
-
剑指 -扑克牌顺子
题目描述现在有五张扑克牌,我们需要来判断一下是不是顺子。有如下规则:A为1,J为11,Q为12,K为13数据中的0可以看作任意牌如果给出的五张牌能组成顺子(即这五张牌是连续的)就输出true,否则就输出false。例如:给出数据[6,0,2,0,4]中间的两个0一个看作3,一个看作5 。即:[6,3,2,5,4]这样这五张牌在[2,6]区间连续,输出true数据保证每组5个数字,每组最多含有4个零class Solution {public: bool IsContinuous原创 2021-04-22 17:19:25 · 156 阅读 · 0 评论 -
左旋转字符串
class Solution {public: string LeftRotateString(string str, int n) { if(str.size()<2) return str; int len = str.length();n%=len;// `string substr(int pos = 0, int n = npos) const;`// //返回由pos开始的n个字符组成的字符串原创 2021-04-22 10:12:57 · 57 阅读 · 0 评论 -
第一个只出现一次的字符
class Solution {public: int FirstNotRepeatingChar(string str) { map<char, int> count; char res; int len = str.length(); for(int i = 0 ; i < len ; i++){ count[str[i]]++; } fo原创 2021-04-22 09:57:36 · 47 阅读 · 0 评论 -
剑指数据结构-滑动窗口最大值
class Solution {public: vector<int> maxInWindows(const vector<int>& num, unsigned int size) { vector<int> res; if(size>num.size() || size==0){ return res; } for(int i=0;i<原创 2021-04-20 19:59:26 · 68 阅读 · 0 评论 -
数组只出现一次的两个数字
class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param array int整型vector * @return int整型vector */vector<int> FindNumsAppearOnce(vector<int>& array) { unordered_map<int,int>原创 2021-04-20 19:40:44 · 53 阅读 · 0 评论 -
剑指数据结构-数据流中的中位数
class Solution {public: vector<int> v; void Insert(int num) { if (v.empty()) { v.push_back(num); } else { auto it = lower_bound(v.begin(), v.end(), num); v.insert(it, n原创 2021-04-14 19:35:00 · 54 阅读 · 0 评论 -
剑指数据结构-最小的K个数
class Solution {public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { priority_queue<int> max_heap; vector<int> results; if(input.size() < k || k<=0) return原创 2021-04-14 19:24:52 · 46 阅读 · 0 评论 -
剑指数据结构-替换空格
class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string replaceSpace(string s) { // write code here string res=""; for(char c原创 2021-04-10 21:14:02 · 51 阅读 · 0 评论 -
剑指数据结构-平衡二叉树
class Solution {public: bool IsBalanced_Solution(TreeNode* pRoot) { // 判断是否为平衡二叉树 if(!pRoot) return true; return dfs(pRoot) == -1?false:true; } int dfs(TreeNode* root){ if(!root) return 0;原创 2021-04-10 19:58:16 · 60 阅读 · 0 评论 -
剑指数据结构-二叉树的下一节点
/*struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *next; TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) { }};*/class Solution {public: Tre原创 2021-04-10 17:30:22 · 47 阅读 · 0 评论 -
剑指数据结构-对称的二叉树
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: bool isSymmetrical(TreeNode* pRoot) { if(!pRoot) r原创 2021-04-08 20:15:34 · 50 阅读 · 0 评论 -
剑指数据结构-把二叉树打印成多行
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: vector<vector<int> > Print(TreeNode* pRoot原创 2021-04-08 19:50:14 · 63 阅读 · 0 评论 -
剑指数据结构-二叉树的深度
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: int TreeDepth(TreeNode* pRoot) { int l = 0; if (!pRoot) return 0;原创 2021-04-08 08:26:35 · 49 阅读 · 0 评论 -
剑指数据结构-从上到下打印二叉树
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: vector<int> PrintFromTopToBottom(TreeNode* root) { vector<int>r原创 2021-04-07 21:30:02 · 31 阅读 · 0 评论 -
剑指数据结构-按之字形顺序打印二叉树
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: vector<vector<int> > Print(TreeNode* pRoot) {原创 2021-04-07 21:08:10 · 47 阅读 · 0 评论 -
剑指数据结构-二叉搜索树的第K个结点
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: //中序遍历的结果就是有序序列,第K个元素就是vec[K-1]存储的节点指针; TreeNode* KthN原创 2021-04-07 21:07:08 · 43 阅读 · 0 评论 -
剑指数据结构-二叉搜索树的后序遍历序列
class Solution {public: bool VerifySquenceOfBST(vector<int> sequence) { if (!sequence.size()) return false; return IsBST(sequence, 0, sequence.size()-1); } bool IsBST( vector<int>& sequence, int start, int原创 2021-04-06 17:22:59 · 48 阅读 · 0 评论 -
剑指数据结构-栈的压入、弹出序列
class Solution {public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { stack<int> stack1; int i=0,j=0; while(i < pushV.size()) { if( pushV[i]!= popV[j]) {原创 2021-04-06 16:18:29 · 45 阅读 · 0 评论 -
剑指数据结构-包含min函数的栈
class Solution {public: stack<int> stack1; stack<int> min1; void push(int value) { stack1.push(value); if(min1.empty()|| value<=min1.top()){ min1.push(value); } } void pop() {原创 2021-04-06 15:26:39 · 49 阅读 · 0 评论 -
剑指数据结构-用两个栈实现队列
class Solution{public: void push(int node) { stack1.push( node); } int pop() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.top()); stack1.pop();原创 2021-04-06 14:33:07 · 37 阅读 · 0 评论 -
剑指数据结构-复杂链表的复制
/*struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { }};*/class Solution {public: RandomListNode* Clone(RandomListNode* pHead) {原创 2021-04-02 20:21:49 · 40 阅读 · 0 评论 -
剑指数据结构-删除链表中重复的节点
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* deleteDuplication(ListNode* pHead) { ListNode *vhead = new ListNode(-1);原创 2021-04-02 16:38:37 · 45 阅读 · 0 评论 -
剑指数据结构-两个链表的第一个公共节点
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { if (!pHead1 || !pHead2 ) return NULL;原创 2021-04-01 21:36:39 · 38 阅读 · 0 评论 -
剑指数据结构-从尾到头打印链表
/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {* }* };*//**翻转单链表,打印*/class Solution {public: vector<int> printListFromTailToHead(ListNode* he原创 2021-04-01 10:11:18 · 40 阅读 · 0 评论 -
剑指数据结构-链表中倒数第k个节点
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @param原创 2021-03-31 09:11:40 · 38 阅读 · 0 评论 -
剑指数据结构-合并两个有序链表
//迭代class Solution {public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode *vhead = new ListNode(-1); ListNode *cur = vhead; while (pHead1 && pHead2) { if (pHead1->val <= pHead2-&g原创 2021-03-30 20:46:36 · 110 阅读 · 0 评论 -
剑指数据结构-机器人的运动范围
//class Solution {public: vector<int> splitnum(int n){ vector<int> ans; if(n<10){ ans.push_back(n); } else if(n<100){// 68 - 6- 8 强制整型 ans.push_back(n/10);原创 2021-03-30 20:28:58 · 49 阅读 · 0 评论 -
剑指数据结构-顺时针打印矩阵
题目描述输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10./* 思想,用左上和右下的坐标定位出一次要旋转打印的数据, 一次旋转打印结束后,往对角分别前进和后退一个单位。 需要加入条件判断,防止出现单行或者单列的情况。 */class Solution {pu原创 2021-03-29 16:17:40 · 53 阅读 · 0 评论 -
剑指数据结构-数组中的逆序对
题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007class Solution {public: int InversePairs(vector<int> data) { if(data.size() == 0){ return 0; } // 排序的辅原创 2021-03-29 15:10:53 · 96 阅读 · 0 评论 -
剑指数据结构-二维数组中的查找
**题目描述在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。class Solution {public: bool Find(int target, vector<vector<int> > array) { //判断是否符合输入数组要求 int row=array.size();原创 2021-03-28 20:57:37 · 55 阅读 · 0 评论 -
剑指数据结构-调整数组顺序使奇数位于偶数前面
题目描述输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vec原创 2021-03-28 19:48:03 · 50 阅读 · 0 评论 -
剑指数据结构-重建二叉树
** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* reConstructBinaryTree(vector&l原创 2021-03-28 19:13:22 · 56 阅读 · 0 评论 -
剑指数据结构- 数组中出现次数超过一半的数字
数组中出现次数超过一半的数字题目描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。class Solution {public: int MoreThanHalfNum_Solution(vector<int> numbers) { int len = numbers.size(); v原创 2021-03-26 11:25:22 · 50 阅读 · 0 评论 -
剑指数据结构-构建乘积数组
构建乘积数组题目描述给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)对于A长度为1的情况,B无意义,故而无法构建,因此该情况不会存在。class Solution {public: vector<int>原创 2021-03-26 09:57:10 · 39 阅读 · 0 评论 -
剑指数据结构-二叉树的镜像
//遍历节点,并交换左右节点//层序遍历 public TreeNode invertTree(TreeNode root) { if (root == null) return root; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); //交换原创 2021-03-25 16:34:17 · 42 阅读 · 0 评论 -
剑指数据结构-判断是否为完全二叉树
反转二叉树public boolean isComplete() { if (root == null) return false; Queue<Node<E>> queue = new LinkedList<>(); queue.offer(root); boolean leaf = false; while (!queue.isEmpty()) { Node<E> node = queue.poll(); if (leaf &原创 2021-03-25 14:57:45 · 44 阅读 · 0 评论 -
剑指数据结构-用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现 MyQueue 类:void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元素boolean empty() 如果队列为空,返回 true ;否则,返回 false说明:你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和原创 2021-03-24 10:55:06 · 47 阅读 · 0 评论 -
剑指数据结构-删除链表中的节点
删除链表中的节点请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。示例 1:输入:head = [4,5,1,9], node = 5输出:[4,1,9]解释:给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list著作权归领扣网络原创 2021-03-24 09:09:41 · 51 阅读 · 0 评论 -
剑指数据结构-单链表反转
反转单链表//递归private ListNode reverse(ListNode head){ //单链表为空 if (head == null || head.next == null)return head; ListNode temp = head.next;//保存下一个节点 ListNode newHead = reverse(head.next);//整体 temp.next原创 2021-03-24 08:56:35 · 63 阅读 · 0 评论