小工具知识网

当前位置: 首页 > 行业知识 > 剑指offer-第二版

行业知识

剑指offer-第二版

2026-11-18 15:53:43 来源:互联网转载

【剑指offer-第二版】习题感悟(一刷)

    • 1. 面试流程
    • 2. 面试的基础知识
      • 2.1 C++基础知识
        • 面试题1:赋值运算符函数
        • 面试题2:实现单例模式
        • 2.2 数据结构
          • 面试题3:数组中重复的数字
          • 面试题4:二维数组中查找
          • 面试题5:替换字符串中的空格
          • 面试题6:从尾到头打印链表
          • 面试题7:重建二叉树
          • 面试题8:二叉树的下一个节点
          • 面试题9:两个栈实现队列or两个队列实现栈
          • 2.3 算法和数据操作
            • 面试题10:斐波那契数列
            • 快速排序算法
            • 面试题11:旋转数组的最小数字
            • 面试题12:矩阵中的路径
            • 面试题13:机器人的运动范围
            • 面试题14:剪绳子
            • 面试题15:二进制中1的个数
            • 3. 高质量的代码
                • 面试题16:数值的整数次方
                • 面试题17:删除链表的结点
                • 面试题18:调整数组顺序使奇数位于偶数前面
                • 面试题19:链表中倒数第k个节点
                • 面试题23:链表中环的入口节点
                • 面试题24:反转链表
                • 面试题25:合并两个排序的链表
                • 面试题25.1:合并k个已排序的链表
                • 面试题26:树的子结构
                • 面试题27:二叉树的镜像
                • 面试题28:对称的二叉树
                • 面试题29:顺时针打印矩阵
                • 面试题30:包含min函数的栈
                • 面试题31:栈的压入、弹出序列
                • 面试题32:从上到下打印二叉树
                • 面试题33:二叉搜索树的后序遍历序列
                • 面试题34:二叉树中和为某一值的路径
                • 面试题35:复杂链表的复制
                • 面试题36:二叉搜索树与双向链表
                • 面试题37:在排序数组中查找数字
                • 面试题38: 0~n-1中缺失的数字
                • 面试题39:二叉搜索树的第k大节点
                • 面试题40:二叉树的深度
                • 面试题41:判断平衡二叉树
                • 面试题42:数组中数字出现的次数
                • 面试题43:数组中唯一出现一次的数字
                • 面试题44:和为s的两个数
                • 面试题45:和为s的连续正整数序列
                • 面试题46:翻转单词顺序
                • 面试题47:左旋转字符串
                • 面试题48:滑动窗口的最大值
                • 面试题49:队列中的最大值
                • 面试题60:n个骰子的点数
                • 面试题61:扑克牌中的顺子
                • 面试题62:股票的最大利润
                • 面试题63:求解1+2+...+n
                • 面试题64:不用加减乘除做加法
                • 面试题65:树中两个节点的最低公共祖先
                • 面试题66:二叉树的公共祖先
                • 面试题67:删掉一个元素以后全为 1 的最长子数组
                • 面试题68:分割回���串(阿里实习笔试)

                  1. 面试流程

                  一般分为三轮面试:2-3轮技术面,1轮HR面

                  ()

                  每一轮技术面试分为3个环节:行为面试,技术面试,提问环节

                  • 行为面试:自我介绍,按照简历上内容进行项目的提问;
                  • 技术面试:编程语言,数据结构与算法等基础知识,手撕代码的能力等;
                  • 提问环节:推荐提问和应聘职位相关的问题,或者项目团队氛围怎么样等等。

                    2. 面试的基础知识

                    2.1 C++基础知识

                    面试题1:赋值运算符函数
                    class CMyString{private:char* m_pData;public:CMyString(const char* pData = nullptr);CMyString(const CMyString& str);~CMyString(void);CMyString& operator=(const CMyString& str){if(this != &str){// 调用拷贝构造函数,作用域之外直接析构释放内存CMyString strTemp(str);// 交换临时变量char* pTemp = strTemp.m_pData;strTemp.m_pData = m_pData;m_pData = pTemp;}return *this; }};CMyString::CMyString(const char* pData){if(pData == nullptr){m_pData = new char[1];m_pData ='
                    面试题2:实现单例模式
                    ';}else{m_pData = new char[strlen(pData)+1];strcpy_s(m_pData, strlen(pData)+1, pData);}}
                    #include #include using namespace std;class CSingleton{private:CSingleton(){}static CSingleton* m_pInstance;static mutex mux_;public:static CSingleton *GetInstance(){if(m_pInstance == nullptr){lock_guard lock(mux_);m_pInatance = new CSingleton();}return m_pInstance;}};CSingleton* CSingleton::m_pInstance = nullptr;

                    单例模式:设计一个类,只能生成该类的一个实例。

                    ()

                    2.2 数据结构

                    面试题3:数组中重复的数字
                    // 解法1:使用hash表bool duplicate(int number[], int length, int* duplication){if(number == nullptr || length if(hashTable[number[i]]){*duplication = number[i];return true;}else{hashTable[number[i]] = 1;}}delete[] hashTable;return false;}if(number == nullptr || lengthif(hashTable[number[i]])return i;elsehashTable[number[i]] = 1;}delete[] hashTable;return -1;}if(matrix != nullptr && cols0 && rows0){int row = 0;int col = cols-1;while(rowif(matrix[row*cols+col] == number)return true;else if(matrix[row*cols+col]  number)col--;elserow++;}}return false;}if (string == nullptr || length origenLength++;if (string[i] == ' ')blankLength++;++i;}int newLength = origenLength + blankLength * 2;if (newLength  length)return;while (newLength  origenLength || origenLength >= 0){if (string[origenLength] == ' '){string[newLength--] = '0';string[newLength--] = '2';string[newLength--] = '%';}elsestring[newLength--] = string[origenLength];origenLength--;}}// 使用stringstring ReplaceBlank(string s){string ss;for(int i=0; is[i]==' ' ? ss+="%20" : ss+=s[i];}return ss;}stackstk.push(node);node = node-next;}while(!stk.empty()){coutpublic:int m_value;TreeNode* left;TreeNode* right;TreeNode(int x) : m_value(x), left(nullptr), right(nullptr) { }};TreeNode* BuildTree1(vectorif (preorder.size() != inorder.size() || preorder.size() == 0)return nullptr;int root = preorder[0];TreeNode* node = new TreeNode(root);int root_idx = -1;for (int i = 0; i right;whiel(pRight->left != nullptr)pRight = pRight->left;pNext = pRight;}else{TreeLinkNode* pParent = pNode;while(pParent->parent != nullptr){if(pParent->parent->left == pParent){pNext = pParent->parent;break;}pParent = pParent->parent;}}return pNext;}

                    题目1:找出数组中重复的数字

                    面试题9:两个栈实现队列or两个队列实现栈
                    // 思路:利用一个栈专门用于出队列,另一个栈用于入队列class cQueue{public:cQueue() {}~cQueue() {}// 实现入队列void appendTail(const int& node){stack1.push(node);}// 实现出队列int deleteHead(){//if(stack.empty() && stack2.empty())//return ;if(stack2.empty()){while(!stack1.empty()){stack2.push(stack1.top());stack1.pop();}}int n = stack2.top();stack2.pop();return n;}private:stack stack1;stack stack2;};// 两个队列实现栈// 思路:保证其中一个栈为空class cStack{public:cStack() {}~cStack() {}void appendHead(const int& node){if (!que1.empty()){que1.push(node);}else{que2.push(node);}}int deteleNode(){//if (que1.empty() && que2.empty())//return ;int node = 0;if (!que1.empty()){int num = que1.size();while (num > 1){que2.push(que1.front());que1.pop();--num;}node = que1.front();que1.pop();}else{int num = que2.size();while (num > 1){que1.push(que2.front());que2.pop();--num;}node = que2.front();que2.pop();}return node;}private:queue que1;queue que2;};

                    2.3 算法和数据操作

                    面试题10:斐波那契数列
                    // 思路:采用递归的方式调用效率低,原因在于存在重复的调用// 变种,小青蛙跳台阶(根据跳的方式累加)int Fn(int n){if(n// 计算f(n-1)+f(n-2)的结果result = pre + cur;pre = cur; // 向下传递cur = result; // 向下传递}return result;}if(start next = cur->next; // 删除重复的结点}else  // 如果没遇到重复结点{pre = cur;  // 移动前驱结点}cur = cur->next; // 不管有没有重复节点,都移动cur}return dyNode->next; //返回链表的下一个节点(不包括头结点)}
                    面试题18:调整数组顺序使奇数位于偶数前面
                    // 思路:使用双指针遍历,左边指针如果是奇数,则叠加;右边指针是偶数,则叠加vector recorder(vector& vec){int left = 0;int right = vec.size()-1;while(leftif((vec[left]&1) == 1){left++;continue;}if((vec[right]&1) == 0){right--;continue;}swap(vec[left++], vec[right--]);}return vec;}if(head == nullptr || kreturn nullptr;}ListNode* pre = head;ListNode* cur = head;for(int i=0; iif(cur!=nullptr){cur = cur-next;}else{return nullptr;  // 说明k大于链表长度}}while(cur!=nullptr){cur = cur-next;pre = pre-next;}return pre;}
                    // 快慢指针遍历ListNode* midNode(ListNode* head){if(head == nullptr) return nullptr;ListNode* fast = head;ListNode* slow= head;while(fast && fast->next){fast = fast->next->next;slow= slow->next;} return sold;}
                    面试题23:链表中环的入口节点
                    // 思路:快指针路径是慢指针路径的2倍,使用快慢指针找到相遇节点,然后从头开始遍历链表,直到和slow相遇,返回慢指针节点ListNode* EntryLoop(ListNode* head){if(head == nullptr) return nullptr;ListNode* fast=head;ListNode* slow=head;while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(fast==slow){ListNode* node = head;while(node != slow)  // 两者路径长度相同  (a+b+c+b) = 2(a+b)  => a = c{node = node->next;slow = slow->next;}return slow;  // 找到环入口即返回}}return nullptr;   // 表示没有环}
                    面试题24:反转链表
                    // 思路:使用三个指针进行保存,分别为当前指针、前一个指针、后一个指针ListNode* reverseList(ListNode* head){ListNode* cur = head;ListNode* pre = nullptr;ListNode* next = nullptr;while(cur != nullptr){next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
                    面试题25:合并两个排序的链表
                    // 思路:采用非递归的方式,按照大小顺序进行递增ListNode* mergeTwoLists(ListNode* l1, ListNode* l2){ListNode* node = new ListNode(-1);ListNode* retNode = node;while(l1 && l2){if(l1->val > l2->val){node->next = l2;l2 = l2->next;}else{node->next = l1;l1 = l1->next;}node = node->next;}if(l2 == nullptr) node->next = l1;if(l1 == nullptr) node->next = l2;return retNode->next;}
                    面试题25.1:合并k个已排序的链表
                    // 思路:属于上一个的变种。只需要遍历即可ListNode* mergeKLists(vector &lists){int len = lists.size();if(len == 0) return nullptr;ListNode* head = lists[0];for(int i=1; i}}ListNode* twoLists(ListNode* l1, ListNode* l2){ListNode* node = new ListNode(-1);ListNode* n = node;while(l1 && l2){       if(l1-val > l2->val)       {           node->next = l2;           l2 = l2->next;       }       else{           node->next = l1;           l1 = l1->next;       }       node = node->next;     }     node->next = (l1==nullptr)? l2 : l1;     return n->next;}
                    面试题26:树的子结构
                    // 思路:使用递归的方式,两种递归,1是递归遍历寻找与子树根结点相同;2是找到与子树根结点相同后,开始遍历比较两个子树,递归遍历class TreeNode{int val;TreeNode* left;TreeNode* right;TreeNode(int v) : val(v), left(nullptr), right(nullptr) { }};// 主函数,用于递归查找与根结点是否相同bool SubTree(TreeNode* A, TreeNode* B){if(A==nullptr || B==nullptr) return false;bool SubFlag = false;if(A->val == B->val) SubFlag = AhaveB(A, B);if(!SubFlag) SubFlag = SubTree(A->left, B);if(!SubFlag) SubFlag = SubTree(A->right, B);return SubFlag;}// 子函数:用于递归比较与子树是否相同bool AhaveB(TreeNode* A, TreeNode* B){if(B==nullptr) return true;if(A==nullptr) return false;if(A->val != B->val) return false;return AhaveB( A->left,  B->left) && AhaveB( A->right,  B->right);}
                    面试题27:二叉树的镜像
                    // 思路:递归遍历,先交换左右子树的结点,然后进行左右子树递归TreeNode* mirroeTree(TreeNode* root){if(root == nullptr) return nullptr;   // 递归结束条件TreeNode* temp = root->left;   // 交换左右子树root->left = root->right;root->right = temp;mirrorTree(root->left);  // 递归左子树mirrorTree(root->right); // 递归左子树return root;}
                    面试题28:对称的二叉树
                    // 思路:判断二叉树是否为对称的,即左子树的左 == 右子树的右, 左子树的右 == 右子树的左// 主函数bool isSymmetric(TreeNode* root){if(root == nullptr) return true;return Mirror(root->left, root->right);}// 子函数bool Mirror(TreeNode* left, TreeNode* right){// 先判断结点为空的情况if(left==nullptr && right==nullptr) return true;if(left==nullptr || right==nullptr) return false;// 判断结点不为空时,结点值不同if(left->val != right->val) return false;bool outSide = Mirror(left->left, right->right);bool inSide = Mirror(left->right, right->left);return outSide && inSide;}
                    面试题29:顺时针打印矩阵
                    // 思路:首先判断矩阵是否为空,然后在开始打印,按照:从左到右,从上到下,从右到左,从下到上的顺序    vector spiralOrder(vector& matrix) {                if(matrix.size()==0 || matrix[0].size()==0) return {};        int rows = matrix.size();        int cols = matrix[0].size();        vector result;        int row = 0, col = -1;        while(rows>0 && cols>0)        {            // 从左到右            for(int i=0; i            col++;                result.push_back(matrix[row][col]);            }            rows--;            if(rows==0 || cols==0) break;  // 一旦存在行或列为空,则需要跳出来            // 从上到下            for(int i=0; i            row++;                result.push_back(matrix[row][col]);            }            cols--;            if(rows==0 || cols==0) break;            // 从右到左            for(int i=0; i            col--;                result.push_back(matrix[row][col]);            }            rows--;            if(rows==0 || cols==0) break;            // 从下到上            for(int i=0; i            row--;                result.push_back(matrix[row][col]);            }            cols--;            if(rows==0 || cols==0) break;        }        return result;    }public:    /** initialize your data structure here. */    stack   }        void push(int x) {        m_data.push(x);        if(m_min.empty())        {            m_min.push(x);        }         else        {            int temp = m_min.top();            if(temp  x)                 m_min.push(x);            else                 m_min.push(temp);        }    }        void pop() {        m_data.pop();        m_min.pop();    }        int top() {        return m_data.top();    }        int min() {        return m_min.top();    }};        bool result = false;        if(pushed.size() != popped.size()) return result;        stack            m_data.push(vec);            while(!m_data.empty() && m_data.top() == popped[idx])            {                m_data.pop();                idx++;            }        }        if(m_data.empty())  result = true;                return result;     }if(root == nullptr) return {};queueTreeNode* node = que.front();que.pop();result.push_back(node-val);if(node-left != nullptr) que.push(node-left);if(node->right != nullptr) que.push(node->right);}return result;}// 思路:打印二叉树,每层打印到一行vector levelOrder(TreeNode* root){if(root == nullptr) return {};queue que;que.push(root);vector result;while(!que.empty()){vector temp;int sz = que.size();while(sz--){TreeNode* node = que.front();temp.push_back(node->val);que.pop();if(node->left != nullptr) que.push(node->left);if(node->right != nullptr) que.push(node->right);}result.push_back(temp);}return result;}// 思路:之字型打印二叉树; 需要注意偶数层需要反转,奇数层不变vector levelOrder(TreeNode* root) {    if(root == nullptr) return {};    queue que;    que.push(root);    int level = 0;  // 用于记录层数    vector result;    while(!que.empty())    {        int sz = que.size();        vector temp;        level++;        while(sz--)        {            TreeNode* node = que.front();            temp.push_back(node->val);            que.pop();            if(node->left != nullptr) que.push(node->left);            if(node->right != nullptr) que.push(node->right);        }                        if((level & 1) == 0) reserve(temp); // 偶数需要反转        result.push_back(temp);    }    return result;}void reserve(vector& vec){     int sz = vec.size();     for(int i=0; i         int temp = vec[i];         vec[i] = vec[sz-i-1];         vec[sz-i-1] = temp;     }}if(postorder.size() == 0) return true;return dfsOrder(postorder, 0, postorder.size()-1);}bool dfsOrder(vectorif(left = right) return true;int root = vec[right];int i = left;for(; iif(vec[i]  root)break;}for(int j=i; jif(vec[j] val == sum && root->left==nullptr && root->right==nullptr){res.push_back(path);}// 3. 不满足和条件,就开始遍历左子树和右子树(需要注意,此时sum改变,需要减掉当前节点的值)if(root->left) findPath(root->left, sum-root->val, path, res);if(root->right) findPath(root->left, sum-root->val, path, res);path.pop_back();}
                    面试题35:复杂链表的复制
                    // 思路:可以使用哈希表进行拷贝复制class Node{public:    int val;    Node* next;    Node* random;        Node(int _val) {val = _val; next = NULL; random = NULL;}};Node* copyRandomList(Node* head){if(head == nullptr) return nullptr;// 1. 创建哈希表,并遍历链表unordered_map u_map;Node* cur = head;while(curr != nullptr){u_map[cur] = new Node(cur->val);cur = cur->next;}// 2. map映射复制cur = head;while(cur != nullptr){u_map[cur]->next = u_map[cur->next];u_map[cur]->random = u_map[cur->random];cur = cur->next;}return u_map[head];}
                    面试题36:二叉搜索树与双向链表
                    // 思路:由于是二叉搜索树,因此需要采用中序遍历。class Solution{public:Node* treeToDoublyList(Node* root){if(root == nullptr) return nullptr;inOrder(root);head->left = pre;pre->right = head;return head;}private:Node* pre;Node* head;void inOrder(Node* cur){if(cur == nullptr) return ;// 中序遍历inOrder(cur->left);if(pre != nullptr) pre->right = cur;else head = cur;cur->left = pre;pre = cur;inOrder(cur->right); }};
                    面试题37:在排序数组中查找数字
                    面试题38: 0~n-1中缺失的数字
                    // 思路:可用哈希表,或者 二分法查找。 其中哈希表较为占用内存int search(vector& nums, int target){return BinaryLeft(nums, target+1)-BinaryLeft(nums, target);}int BinaryLeft(vector& nums, int target){int left = 0;int right = nums.size()-1;int mid = 0;while(leftmid = (left+right)>1;if(nums[mid]  
                    // 思路:使用二分法查找,如果相等则在右边,否则在左边void missingNumber(vector& nums){return BinaryNum(nums);}int BinaryNum(vector nums){int left = 0;int right = nums.size()-1;int mid = 0;while(leftmid = (left+right)>1;if(nums[mid] == mid)left = mid + 1;elseright = mid - 1;}return left;}
                    面试题39:二叉搜索树的第k大节点
                    // 思路: 使用中序遍历可以得到二叉搜索树按照从小到大的排序,而我们需要从大到小的排序,因此需要按照从右到左int kthLargest(TreeNode* root, int k){if(root == nullptr || k==0) return 0;vector result;mid_dfs(root, result);return result[k-1];}void mid_dfs(TreeNode* root, vector& vec){if(root == nullptr) return ;mid_dfs(root->right, vec);vec.push_back(root->val);mid_dfs(root->left, vec);}
                    面试题40:二叉树的深度
                    // 思路: 采用层序遍历,借助queueint maxDepth(TreeNode* root){if(root == nullptr) return 0;queue que;que.push(root);int depth=0;while(!que.empty()){++depth;int size = que.size();while(size--){TreeNode* node = que.front();que.pop();if(node->left) que.push(node->left);if(node->right) que.push(node->right);}}return depth;}
                    面试题41:判断平衡二叉树
                    // 思路:如果时平衡二叉树,则说明任意节点的左、右子树的高度差不超过1.可以使用后序遍历。bool res = true;bool idBalance(TreeNode* root){dfs(root);return res;}int dfs(TreeNode* root){if(root == nullptr) return 0;int left = dfs(root->left);int right = dfs(root->right);if(abs(left-right)>1) res = false;return max(left, right)+1;}
                    面试题42:数组中数字出现的次数
                    面试题45:和为s的连续正整数序列
                    // 思路:可以使用异或实现检测数组中出现不重复的数字,如果一个数组中只有一个不重复的数字,仅需要遍历一遍即可。vector singleNumbers(vector& nums){int first=0, second=0, mid=1;for(auto &n : nums) // 遍历一遍数组可以找到两个不重复数字的组合{first ^= n;}while( (first&mid) == 0) // 找到两个数字第一个不同的位,利用该位将数组分为两份,然后分别查找mid if(n&mid) first ^= n;else second ^= n; }return {first, second};}int res = 0;unordered_mapif(map_n[n] == 1){res = n;break;}}return res;}int begin = 0;int end = nums.size()-1;while(end  begin){int res = nums[begin]+nums[end];if(res  target) end--;else if(res  
                    面试题49:队列中的最大值
                    // 思路:可以使用滑动窗口算法,左闭右开。主要判断条件时 左边界小于目标值的一半vector findContinuousSequence(int target){int left = 1; // 左边界int right = 1; // 右边界int sum = 0; // 中间值vector result;while(left if(sum >1;while(begin char c = s[begin];s[begin] = s[end];s[end] = c;begin++;end--;}}if(s.size() int mid = (begin+end)1;while(begin char c = s[begin];s[begin] = s[end];s[end] = c;begin++;end--;}}// 2. 拼接方法string reserveLeftWords(string s, int n){int len = s.size();s += s;  // 拼接字符串return s.substr(n, len); // 取出(n-len)字符串}vector// 查找滑窗内的最大值,放到队头while( !que.empty() && nums[i]  num[que.back()])que.pop_back();// 判断队头的索引是否超出窗口左边界if( !que.empty() && que.front()  
                    面试题60:n个骰子的点数
                    // 思路:使用单调不增序列,同时使用队列和双向队列class MyQueue{public:queue que;deque deq;MyQueue(){}int MaxValue(){if(deq.empty()) return -1;return deq.front();}void push_back(int value){que.push(value);while(!deq.empty() && deq.back()  
                    // 思路:主要利用动态规划进行求解,其状态转移方程需要构建,还有初始化边界条件vector dicesProbability(int n){// 首先定义:二维数组res, 表示第n个骰子和为j出现的次数vector res(n+1, vector(6*n+1, 0));// 其次定义初始化条件for(int i=1; iif(jresult.push_back(res[n][i]*1.0/allSum);}return result;}sort(nums.begin(), nums.end());int zero_size = 0;int space_sum = 0;for(int i=0; iif(nums[i] == 0){zero_size++;continue;}int temp = nums[i+1]-num[i];  // 计算数字间隔if(temp  1)space_sum += (temp-1); // 统计相邻数字间的间隔,正常间隔为1if(temp == 0)return false;  // 存在重复数字,直接返回}return (zero_size = space_sum);}if(prices.size() val val && node->val val)node = node->right;elsebreak;}return node;}
                    面试题66:二叉树的公共祖先
                    // 思路:由于是普通二叉树, 需要遍历查找。采用后序遍历TreeNode* lowestCommon(TreeNode* root, TreeNode* p, TreeNode* q){if(!root || !p || !q || root==p || root=q) // 表示当前当前节点就是两个节点中一个return root;TreeNode* left = lowestCommonAncestor(root->left, p, q);TreeNode* right = lowestCommonAncestor(root->right, p, q); // 后序遍历    if(!left && !right) return nullptr;  // 表示节点两边都没有    else if(left && right) return root;  // 表示在当前节点两边    else if(!left && right) return right;  // 表示在右节点这边    else return left;   // 表示在左节点这边}
                    面试题67:删掉一个元素以后全为 1 的最长子数组
                    // 思路:可以使用滑动窗口进行统计,主要统计滑动窗口内0的个数。如果0个数大于1则需要移动左边界,将多余的0移除出去;如果0个数小于等于1则需要移动右边界int longestSubarray(vector& nums){// 首先定义左右边界int left=0, right=0;int size=nums.size(); // 统计数组的大小int count=0;  // 表示窗口内0的个数int res=0; // 表示最后的滑窗内最大连续1值while(rightcount += nums[right]==0;  // 统计滑窗内0的个数while(count 1){count -= nums[left]==0; // 将移除掉的0剪掉left++;}res = max(res, right-left+1);  // 保存最大窗口数right++;  // 说明滑窗内0个数小于等于1}return res-1;  // 由于需要减掉0,因此需要为窗口最大值减1}// 包含输入输入输出的笔试#include#include#includeusing namespace std;// 滑动窗口算法int maxSlideWindow(vector nums){int left = 0, right = 0;int count = 0;int size = nums.size();int res = 0;while (right  1){count -= nums[left] == 0;left++;}res = max(res, right - left + 1);right++;}return res - 1;}// 主函数int main(){int count = 0;cin >> count;vector nums;while (count--){int signlCount = 0;cin >> signlCount;int temp = 0;vector nums1;while (signlCount--){cin >> temp;nums1.push_back(temp);}nums.push_back(nums1);}int length = nums.size();for (auto & num : nums){int res = maxSlideWindow(num);cout int res = 0;for(int i=l, j=r; iif(s[i] != s[j])res++;}return res;}// 2. 动态规划返回d[n][k] int reslotion(string& s, int k){int n=s.size(); // 字符个数// dp[i][j]表示前i个字符分割为j个子串需要的最少字符数,其中i=jvectorfor(int j=1; jif(j==1)  // 表示前i个不分割,直接统计{dp[i][j] = cost(s, 0, i-1);}else{for(int t=j-1; tdp[i][j] = min(dp[i][j], dp[t][j-1]+cost(s, t, i-1));  // 状态转移方程,使用最小的}}}}return dp[n][k];}int main(){int size = 0;cinsize;vectorstring temp_str;cintemp_str;str.push_back(temp_str);cintemp;count.push_back(temp);}for(int i=0; icout

上一篇:云服务器怎么使用代理ip

下一篇:揭秘下载页面代码秘籍,轻松掌握高效编写技巧!