- 博客(20)
- 资源 (5)
- 收藏
- 关注
原创 字节跳动后端面试题 字符串s1中包含s2所有字符的最短子串
这是2022年3.8日字节跳动后端面试的一个coding题面试的时候我没有完全写出来,只是有思路,但debug不对现在写出来仅作记录题目:给定两个字符串s1,s2,默认s1长度大于s2。求出字符串s1中包含s2所有字符的最短子串思路:滑动窗口,具体见代码与注释from collections import defaultdicts1 = "akljbkcjalaklklbliopc"s2 = "abc"dct1 = defaultdict(int) # 存储s1的字符出现次数(
2022-03-09 15:32:41
7334
1
原创 剑指offer JZ16 合并两个排序的链表
代码class Solution {public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode* list = new ListNode(-1); ListNode* p = list; while (pHead1 && pHead2) { if (pHead1->val <= pHead2->val) {
2021-07-16 18:04:04
103
原创 剑指offer JZ15 反转链表
代码class Solution {public: ListNode* ReverseList(ListNode* pHead) { ListNode *pre = nullptr; ListNode *cur = pHead; ListNode *nex = nullptr; while (cur) { nex = cur->next; cur->next = pre;
2021-07-15 19:23:14
105
原创 剑指offer JZ14 链表中倒数最后k个结点
代码/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @par
2021-07-14 16:56:41
117
原创 剑指offer JZ13 调整数组顺序使奇数位于偶数前面
代码class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vector<int> reOrderArray(vector<int>& array) { // write code here
2021-07-13 19:52:13
73
原创 剑指offer JZ12 数值的整数次方
代码class Solution {public: double Power(double base, int exponent) { if (exponent < 0) { exponent = -exponent; base = 1/base; }//处理幂为负数的情况 double x = base; double res = 1.0;// return
2021-07-12 17:46:48
80
原创 剑指offer JZ11 二进制中1的个数
代码class Solution {public: int NumberOf1(int n) { int res=0; while (n!=0){ res++; n=n & (n-1); } return res; }};思路今天这题我的理解不深,就不献丑了,这里给出官方解答。谢谢各位。如果我以后理解了,会跟上我的解答的。...
2021-07-11 20:10:12
75
原创 剑指offer JZ10 矩形覆盖
代码class Solution {public: int rectCover(int number) { if (number == 0) return 0; if (number == 1) return 1; int dp[number+1]; dp[0]=1; dp[1]=1; for (int i=2;i<=number+1;i++){ dp[i] = dp[i
2021-07-10 22:10:48
124
1
原创 剑指offer JZ9 跳台阶扩展问题
代码class Solution {public: int jumpFloorII(int number) { return pow(2,number-1); }};思路非常简单易懂,拜拜~
2021-07-09 22:13:58
186
4
原创 剑指offer JZ8 跳台阶
代码class Solution {public: int jumpFloor(int number) { if (number == 1) return 1; if (number == 2) return 2; int dp[number]; dp[0]=1; dp[1]=2; for (int i=2;i<number;i++){ dp[i]=dp[i-1]+dp[
2021-07-08 09:26:43
78
原创 剑指offer JZ7 斐波那契数列
代码class Solution {public: int Fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; int i=2; int a=0; int b=1; while (i<=n) { int temp = b; b=a+b; a=temp;
2021-07-07 11:29:30
100
原创 剑指offer JZ6 旋转数组的最小数字
代码class Solution {public: int minNumberInRotateArray(vector<int> rotateArray) { if (rotateArray.empty()){ return 0; } int left = 0; int right = rotateArray.size()-1; while (left < right){
2021-07-06 11:00:43
47
原创 剑指offer JZ5 用两个栈实现队列
代码class Solution{public: void push(int node) { while (!stack1.empty()){ stack2.push(stack1.top()); stack1.pop(); } stack1.push(node); while (!stack2.empty()){ stack1.push(stack2.top
2021-07-05 22:07:35
87
原创 剑指offer JZ4 重建二叉树
代码/** * 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* rebuild(vector<int>
2021-07-04 11:38:45
75
原创 剑指offer JZ3 从尾到头打印链表
代码class Solution {public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> res; while (head){ res.push_back(head->val); head = head->next; } std::reverse(res.b
2021-07-03 10:06:19
95
原创 剑指offer JZ2 替换空格
代码class Solution {public: string replaceSpace(string s) { string ans=""; for(char c:s){ if(c==' ') ans+="%20"; else ans+=c; } return ans; }};思路使用临时变量存储,遍历原字符串替换。...
2021-07-02 16:47:17
73
原创 剑指offer JZ1 二维数组中的查找
代码class Solution {public: bool Find(int target, vector<vector<int> > array) { int m = array.size(); int n = array[0].size(); if (m==0) return false; if (n==0) return false; int r=0,c=n-1; wh
2021-07-01 19:50:48
57
原创 pat A1022 python
输入环节 使用字典,将所有出现的关键词分组存储。title={}author={}keyword={}publisher={}year={}n=int(input())for i in range(n): id=input() temp=input() try: title[temp].append(id) except: title[temp]=[id] temp=input() try:
2021-03-31 14:22:22
71
原创 python 使用列表管理同类型实例
python 使用列表管理同类型实例如何创建交换指向的实例灵感来源如何创建这样列表内的每个元素就指向了一个独特的实例交换指向的实例灵感来源最近使用python刷PAT甲级的题目时,我的代码中有对一串实例进行管理的需求,于是便尝试了许久,想出了这个办法~...
2020-12-26 14:49:32
145
1
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人