剑指offer第九天

1.数组中只出现一次的两个数

cpp 复制代码
#include <vector>
class Solution {
public:
    vector<int> FindNumsAppearOnce(vector<int>& nums) {
        // write code here
        int v = 0;
        for(int&x:nums)
            v^=x;
        int cnt = 0;
        while(v)
        {
            v>>=1;
            cnt++;
        }
        int a = 0,b = 0;
        for(int&x:nums)
        {
            if(x&(1<<(cnt-1)))a^=x;
            else b^=x;
        }
        return vector<int>{min(a,b),max(a,b)};
    }
};

2.求1+2+...+n

cpp 复制代码
class Solution {
public:
    int getSum(int n) {
        return 1ll*n*(n+1)/2;
    }
};

3.顺时针打印矩阵

cpp 复制代码
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> res;
        int n = matrix.size();
        //先排除特殊情况
        if(n == 0) 
            return res;
        //左边界
        int left = 0; 
        //右边界
        int right = matrix[0].size() - 1; 
        //上边界
        int up = 0; 
        //下边界
        int down = n - 1; 
        //直到边界重合
        while(left <= right && up <= down){ 
            //上边界的从左到右
            for(int i = left; i <= right; i++) 
                res.push_back(matrix[up][i]); 
            //上边界向下
            up++; 
            if(up > down)
                break;
            //右边界的从上到下
            for(int i = up; i <= down; i++) 
                res.push_back(matrix[i][right]);
            //右边界向左
            right--; 
            if(left > right)
                break;
            //下边界的从右到左
            for(int i = right; i >= left; i--) 
                res.push_back(matrix[down][i]);
            //下边界向上
            down--; 
            if(up > down)
                break; 
            //左边界的从下到上
            for(int i = down; i >= up; i--) 
                res.push_back(matrix[i][left]);
            //左边界向右
            left++; 
            if(left > right)
                break;
        }
        return res;
    }
};

4.扑克牌顺子

cpp 复制代码
class Solution {
    bool hash[14] = { 0 };
  public:
    bool IsContinuous(vector<int>& numbers) {
        int maxVal = 0, minVal = 14;
        for (int& x : numbers) {
            if (x) {
                if (hash[x]) return false;
                hash[x] = true;
                maxVal = max(maxVal, x);
                minVal = min(minVal, x);
            }
        }
        return maxVal - minVal <= 4;
    }
};

5.构建乘积数组

cpp 复制代码
class Solution {
public:
    vector<int> multiply(vector<int>& A) {
        // write code here
        //看起来数值比较小,可以直接使用暴力的做法去乘
        int n = A.size();
        vector<int>ans(n,1);
        for(int i = 0;i<n;++i)
        {
            for(int j = 0;j<n;++j)
            {
                if(j == i)continue;
                ans[i] *= A[j];
            }
        }
        return ans;
    }
};

6.字符串中第一次只出现一次的字符

cpp 复制代码
class Solution {
    int cnt[128]{};
public:
    char firstNotRepeatingChar(string s) {
        for(char&c:s)
            cnt[c]++;
        for(char&c:s)
            if(cnt[c]==1)return c;
        return '#';
    }
};

7.替换空格

python 复制代码
# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        return "%20".join(list(s.split(' ')))
相关推荐
雾喔15 小时前
算法练习7
java·数据结构·算法
可编程芯片开发16 小时前
基于模糊PID控制的六步逆变器供电无刷直流电机调速simulink仿真
算法
imperialeast17 小时前
WinAXP音乐播放器8月4日更新
windows·算法·ui
ZhouDevin17 小时前
算法论文/模型微调1——CNN架构做lora微调训练
人工智能·深度学习·算法·架构·cnn
小月土星17 小时前
LeetCode 21. 合并两个有序链表 超详细题解(虚拟头结点 + 逐行代码拆解)
算法
oier_Asad.Chen17 小时前
【OI学习笔记】Floyed-Warshall算法解决传递闭包问题
c++·笔记·学习·算法·图论·最短路·传递闭包
国科安芯17 小时前
卫星星务计算机数据管理中SEU容错机制的技术实现研究——以AS32S601存储器ECC架构为例
网络·单片机·算法·fpga开发·架构·云计算·risc-v
孙启超17 小时前
【AI应用开发】Agent 无限 loop(反复调用同一个工具)如何规避?
人工智能·python·算法·llm·agent·loop·ai应用开发
小月土星17 小时前
LeetCode 142. 环形链表 II 超详细题解(快慢指针原理+逐行代码拆解)
算法
dok1217 小时前
Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》 (11)kmalloc 动态内存分配和struct file中的私有数据
linux·驱动开发·算法