剑指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(' ')))
相关推荐
生成论实验室8 分钟前
用事件关系网络重新理解AI(二):损失函数、优化器与深度学习的动力学
数据结构·人工智能·深度学习·算法·语言模型
霍霍的袁15 分钟前
【C++初阶】缺省参数(默认参数)详细讲解
开发语言·c++·算法
计算机安禾21 分钟前
【算法分析与设计】第2篇:计算模型与渐进复杂性分析
算法
生成论实验室26 分钟前
事件、信息荷与六维态势空间——每一个事件都是一次空间的弯曲
人工智能·算法·语言模型·可信计算技术·安全架构
budingxiaomoli31 分钟前
递归,搜索与回溯算法--递归
算法
风味蘑菇干32 分钟前
Stream基础题目
java·算法
KaMeidebaby43 分钟前
卡梅德生物技术快报|真核蛋白表达信号肽筛选实验全流程复盘
服务器·前端·数据库·人工智能·算法
霍霍的袁1 小时前
【C++初阶】函数重载详细讲解
开发语言·c++·算法
心中有国也有家1 小时前
CANN 算子开发完全指南——从 TBE DSL 到算子上线全流程
人工智能·经验分享·笔记·分布式·算法
阿文的代码库1 小时前
线段树入门:算法分析
数据结构·算法