首页 > 编程语言 >算法--2023.1.20

算法--2023.1.20

时间:2023-01-20 11:55:22浏览次数:34  
标签:20 nums -- void int 2023.1 static public

1.acwing842--排列数字

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static boolean[] flag;
    public static Deque<Integer> queue = new LinkedList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        flag = new boolean[n+1];
        dfs(n);
    }
    public static void dfs(int n){
        if(queue.size() == n){
            for(int t : queue){
                System.out.print(t + " ");
            }
            System.out.println();
        }
        for(int i = 1;i<=n;i++){
            if(flag[i] == false){
                flag[i] = true;
                queue.addLast(i);
                dfs(n);
                queue.removeLast();
                flag[i] = false;
            }
        }

    }
}

2.力扣240--搜索二维矩阵2

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length, n = matrix[0].length;
        int i = n-1, j = 0;
        while(i>=0&&j<m){
            if(matrix[j][i]>target){
                i--;
            }else if(matrix[j][i]<target){
                j++;
            }else{
                return true;
            }
        }
        return false;
    }
}

3.力扣283--移动零

class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        for(int i = 0;i<n;i++){
            boolean flag = false;
            for(int j = 1;j<n;j++){
                if(nums[j-1] == 0){
                    flag = true;
                    int temp = nums[j-1];
                    nums[j-1] = nums[j];
                    nums[j] = temp;
                }
            }
            if(flag == false){
                return;
            }
        }
        return;
    }
}
class Solution {
    public void moveZeroes(int[] nums) {
        //快慢指针算法:一次遍历,快指针在前面找到不是0的元素,然后放到慢指针这里,可以保证所有非零元素按照原来顺序相邻
        int n = nums.length;
        int i = 0, j = 0;
        for(;j<n;j++){
            if(nums[j] !=0){
                nums[i++] = nums[j];
            }
        }
        for(int k = i;k<n;k++){
            nums[k] = 0;
        }

    }
}

  

  

 

标签:20,nums,--,void,int,2023.1,static,public
From: https://www.cnblogs.com/lyjps/p/17062633.html

相关文章

  • CentOS Stream/Rocky Linux/AlmaLinux 8和9旧内核删除
    一、查看当前使用的内核版本uname-a二、查询当前已安装的所有内核rpm-qa|grepkernel三、删除未使用的内核dnfremove--oldinstallonly--setoptinstall......
  • Harbor企业级镜像仓库的安装
    1、概述Harbor是VMware公司开源的一个企业级DockerRegistry项目,项目地址:https://github.com/goharbor/harborHarbor作为一个企业级私有Registry服务器,提供......
  • 推荐ssh工具
    介绍一些我常用的ssh工具1、Xshell​ Xshell应该是一款家喻户晓的ssh连接工具,本人有幸也在很长一段时间都在使用Xshell,但是Xshell他是收费的!而且每次关闭后都会有一个提......
  • 前端面试题合集-第一篇
    前端面试题合集-第一篇......
  • excel 用byte数组存储 pgsql数据库的 导入 导出
      这次需求是用数据库来存储excel,不用任何操作,只记录excel文件。 这次的灵感来源于这句话     pom文件: <dependency><groupId>......
  • (转)Delphi编译器版本对照表及工具链
    目录Delphi编译器版本对照表及工具链1、Delphi编译器版本历史对照表2、Delphi工具链(Delphi编译器)3、Delphi条件编译3.1、预定义条件符号:3.2、对编译器版本的条件......
  • IBM免费服务器试用
    IBM公司的免费资源,仅需一个邮箱就可以注册申请,注册可以有60天试用期。1、注册点击注册进入IBM教育资源官网进行注册,注册时仅需要一个有效邮箱,QQ,Gmail,outlook均可。......
  • 308. 最廉价的回文串 Cheapest Palindrome(挑战程序设计竞赛)
    地址https://www.papamelon.com/problem/308给定一个长度为m(m≤2000)的小写字母字符串,在给定组成该字符串的n(n≤26)个字符的添加和删除费用,求使原字符串变为......
  • 数据结构:树状数组 学习笔记
    树状数组基本思想树状数组是一种基于二进制拆分的思想,用来动态维护序列的前缀和的树形数据结构。在全国青少年信息学奥林匹克竞赛大纲内难度评级为6,是提高级中开始学习......
  • 1538 迎春舞会之数字舞蹈 题解
    #include<iostream>intmain(){/**#Seven-segmentDisplay**Thewayhowtheprogramprintsdecimalnumericstotheconsoleworks......