算法特训,AB5 .点击消除BC.149简写单词牛客.除2!牛客.Fibonacci数列

简介: 算法特训,AB5 .点击消除BC.149简写单词牛客.除2!牛客.Fibonacci数列

AB5 .点击消除


点击消除,类似于括号匹配a(b[b]a){c{d,这种,利用栈去消除,这样正好可以处理,假如相同就不进栈,同时还要出栈。注意我们这么搞完他是一个逆序,我们需要一个reverse()

 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //aba,aab
        String a = scanner.nextLine();
        char[] b = a.toCharArray();
        Stack<Character> c = new Stack<>();
 
        for (int i = 0; i < b.length; i++) {
            if (c.isEmpty()) {
                c.push(b[i]);
            } else {
                if (b[i] == c.peek()) {
                    c.pop();
                } else c.push(b[i]);
            }
        }
        StringBuffer ret = new StringBuffer();
        if (c.isEmpty()){
            System.out.println(0);
        }else {
            while (!c.isEmpty()) {
                ret.append(c.pop());
            }
            ret.reverse();
            System.out.println(ret.toString());
        }
    }
}

BC.149简写单词

import java.util.Scanner;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a1=in.nextLine();
        String ret="";
        String []a=a1.split(" ");
        int b=a.length;
        for(int i=0;i<b;i++){
            if(a[i].charAt(0)>='a'){
              ret+=(char)(a[i].charAt(0)-32);
            }else{
                ret+=a[i].charAt(0);
            }
        }
        System.out.println(ret);
    }
}

牛客.除2!

我们把偶数都放在堆里面,注意⚠️也要看数字的范围10^9我们需要,

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
//逆序的大根堆
        PriorityQueue<Integer> a = new PriorityQueue<>((c,d)->{
            return d-c;
        });
        ArrayList<Integer> ret=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            long x=scanner.nextLong();
            if(x%2==0){
//偶数放到堆里面
                a.add((int)x);
            }else
//奇数放到ArrayList里面
                ret.add((int) x);
        }
        long count = 0;
        while (k > 0 && !a.isEmpty()) {
                int t = a.poll();
                if(t%2==0){
                a.add(t / 2);
                    k--;}
//有可能10/2=5 然后把5放进来之后,5并没有修改
                else{
                    ret.add(t);
                }
 
        }
        for(int i=0;i<ret.size();i++)
            count += ret.get(i);
            while (!a.isEmpty()){
                count+=a.poll();
            }
        System.out.println(count);
    }   
    }

牛客.Fibonacci数列

import java.util.*;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
//假如不用集合也可以,但是数组是没有集合好的,毕竟数组的长度你是无法确定的
        ArrayList<Integer>dp = new ArrayList<>();
        if (a == 1) {
            System.out.println(0);
        } else {
            dp.add(0);
            dp.add(1);
            int m = 0;
            int n = 1;
            int k = 1;
            int count = Integer.MAX_VALUE;
            int j = 2;
            while (dp.get(j - 1) < a) {
                dp.add(j, dp.get(j - 1) + dp.get(j - 2));
                j++;
            }
            for (int i = 2; i < dp.size(); i++) {
                m = dp.get(i - 2);
                n = dp.get(i - 1);
                k = dp.get(i);
                if (a < k && a > n) {
                    count = Math.min(count, Math.min(Math.abs(a - k), Math.abs(a - n)));
                } else if (a < n && a > m) {
                    count = Math.min(count, Math.min(Math.abs(a - m), Math.abs(a - n)));
                }
            }
            System.out.println(count);
        }
    }
}


相关文章
|
1月前
|
算法
【算法优选】 动态规划之斐波那契数列模型
【算法优选】 动态规划之斐波那契数列模型
|
1月前
|
机器学习/深度学习 算法 测试技术
【动态规划】C++算法:446等差数列划分 II - 子序列
【动态规划】C++算法:446等差数列划分 II - 子序列
|
1月前
|
机器学习/深度学习 数据采集 监控
机器学习-特征选择:如何使用递归特征消除算法自动筛选出最优特征?
机器学习-特征选择:如何使用递归特征消除算法自动筛选出最优特征?
303 0
|
15天前
|
存储 SQL 算法
LeetCode题58: 5种算法实现最后一个单词的长度【python】
LeetCode题58: 5种算法实现最后一个单词的长度【python】
|
19天前
|
算法 Java Go
【经典算法】LeetCode 58.最后一个单词的长度(Java/C/Python3/Go实现含注释说明,Easy)
【经典算法】LeetCode 58.最后一个单词的长度(Java/C/Python3/Go实现含注释说明,Easy)
13 0
|
19天前
|
存储 算法 Java
【经典算法】LeetCode 151. 反转字符串中的单词(Java/C/Python3实现含注释说明,中等)
【经典算法】LeetCode 151. 反转字符串中的单词(Java/C/Python3实现含注释说明,中等)
13 0
|
1月前
|
算法
算法沉淀 —— 动态规划篇(斐波那契数列模型)
算法沉淀 —— 动态规划篇(斐波那契数列模型)
29 0
|
1月前
|
算法
算法修炼-动态规划之斐波那契数列模型
算法修炼-动态规划之斐波那契数列模型
|
1月前
|
存储 算法 JavaScript
JS算法-反转字符串中的单词
JS算法-反转字符串中的单词
|
1月前
|
算法 JavaScript

热门文章

最新文章