首页 > 编程语言 >每日算法--2023.2.28

每日算法--2023.2.28

时间:2023-02-28 12:44:40浏览次数:40  
标签:nums -- res 28 queue 2023.2 int new

1.剑指offer 56 数组中数字出现的次数2

class Solution {
    public int singleNumber(int[] nums) {
        int[] cnt = new int[32];
        int n = nums.length;
        for(int i = 0;i<n;i++){
            int cur = nums[i], t = 0;
            while(cur!=0){
                if((cur&1)== 1){
                    cnt[t]++;
                }
                cur = cur>>1;
                t++;
            }
        }
        int res = 0;
        for(int i = 0;i<32;i++){
            if(cnt[i]%3 == 1){
                res += (1<<i);
            }
        }
        return res;

    }
}

2.剑指offer56 --数组中出现的次数1

class Solution {
    public int[] singleNumbers(int[] nums) {
        int n = nums.length, sum = 0;
        for(int i = 0;i<n;i++){
            sum ^=nums[i];
        }
        int cnt = 0;
        while(sum!=0){
            if((sum &1) == 1){
                break;
            }
            sum = sum>>1;
            cnt++;
        }
         int res1 = 0, res2 = 0;
         for(int i = 0;i<n;i++){
             if((nums[i] & (1<<cnt)) == 0){
                 res1 ^= nums[i]; 
             }else{
                 res2 ^= nums[i];
             }
         }  
         return new int[]{res1,res2}; 
    }
}

3.剑指offer59 -- 滑动窗口的最大值

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        Deque<Integer> queue = new LinkedList<>();
        int[] res = new int[n-k+1];
        int t = 0;
        for(int i = 0, j = 0;j<n;j++){
            int cur = nums[j];
            while(!queue.isEmpty()&&cur>queue.getLast()){
                queue.removeLast();
            }
            queue.addLast(cur);
            if(j-i+1>k){
                int temp = queue.getFirst();
                if(temp == nums[i]){
                    queue.removeFirst();
                }
                i++;
            }
            if(j-i+1 == k){
                res[t++] = queue.getFirst();
            }
        }
        return res;
    }
}

4.剑指offer64

class Solution {
    public int sumNums(int n) {
        return n == 0?0:sumNums(n-1) + n;
    }
}

5.剑指offer 66 -- 构建乘积数组

class Solution {
    //前缀积与后缀积
    public int[] constructArr(int[] a) {
        int n = a.length;
        int[] preorderNum = new int[n+2];
        int[] postorderNum = new int[n+2];
        preorderNum[0] = 1;postorderNum[n+1] = 1;
        for(int i = 1;i<=n;i++){
            preorderNum[i] = preorderNum[i-1]*a[i-1];
        }
        for(int i = n;i>=1;i--){
            postorderNum[i] = postorderNum[i+1] * a[i-1];
        }
        int[] res = new int[n];
        for(int i = 0;i<n;i++){
            res[i] = preorderNum[i] * postorderNum[i+2]; 
        } 
        return res;
    }
}

  

 

  

  

  

标签:nums,--,res,28,queue,2023.2,int,new
From: https://www.cnblogs.com/lyjps/p/17163604.html

相关文章

  • OpenStack00-虚拟化之KVM
    1、什么是虛拟化?虚拟化是一种具体技术,把实体资源进行虚拟化。虚拟化技术根据对象可以分成计算虚拟化、存储虚拟化、网络虚拟化等。虚拟化是一种具体技术,指是一种资......
  • golang实现类似PHP json_encode和json_decode 函数
    类似PHP json_encode和json_decode的用法/*data:=map[string]interface{}{"name":"Tom","age":18,"gender":"male",......
  • 网络传输三大基石
    三大基石:URL,HTTP协议,HTMLURL:在WWW上,每一信息资源都有统一的且在网上唯一的地址,该地址就叫URL(UniformResourceLocator,统一资源定位符),它是WWW的统一资源定位标志,就是指网......
  • Map集合
    Map集合packageMap_test.Map_01;importjava.util.HashMap;importjava.util.Map;publicclassMapDemo_1{publicstaticvoidmain(String[]args){......
  • 网络传输三大基石
    三大基石:URL,HTTP协议,HTMLURL:在WWW上,每一信息资源都有统一的且在网上唯一的地址,该地址就叫URL(UniformResourceLocator,统一资源定位符),它是WWW的统一资源定位标志,就是指网......
  • 逆向软件设计和开发能力
    原项目流程图  改进后的项目流程图改进之后的优点:将两个形式类似的检测模块合并成一个模块,减少了代码量,令编程更为简明便捷。......
  • POD
    一、pod介绍1、pod的基础概念Pod是kubernetes中最小的资源管理组件,Pod也是最小化运行容器化应用的资源对象,一个pod代表着集群中运行的一个进程。kubernetes中其它大多数......
  • 免登陆,用脚本提前修改微信、企业微信、QQ的聊天路径
    为什么要改聊天路径微信、企业微信、QQ的聊天记录默认都是放在登陆用户的文档目录下面的登陆用户的文档目录路径C:\Users\%USERNAME%\Documents在我的日常工作中,碰到......
  • 35互联,中国频道均无法登录?
    昨晚想登录35互联,中国频道,但均报无法登录。截止目前仅​​http://service.35.com/​​​可以看到内容,其他​​http://www.35.com​​​, ​​http://www.china-channel.......
  • Apache Maven Assembly自定义打包插件的使用
    前言本文主要记录在SpringBoot项目中使用ApacheMavenAssembly插件进行打包的相关内容;官网说明:https://maven.apache.org/plugins/maven-assembly-plugin/概述是什......