笔试算法总结

文章目录

题目1

使用 StringBuilder 模拟栈的行为,通过判断相邻2个字符是否相同,如果相同就进行删除

java 复制代码
public class Main {
    public static String fun(String s) {
        if (s == null || s.length() <= 1) return s;

        StringBuilder builder = new StringBuilder(); // 使用StringBuilder模拟栈
        for (char c : s.toCharArray()) {
            if (builder.length() > 0 && builder.charAt(builder.length() - 1) == c) {
                builder.deleteCharAt(builder.length() - 1);
            } else {
                builder.append(c);
            }
        }

        return builder.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        System.out.println(fun(s));
    }
}

题目2

java 复制代码
public class Main {

    public static int climbStairs(int n) {
        if (n <= 2) {
            return n;
        }

        int[] dp = new int[n + 1]; // 动态规划数组

        dp[1] = 1;
        dp[2] = 2;

        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2]; // 状态转移方程
        }

        return dp[n];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(climbStairs(n));
    }
}

题目3


java 复制代码
 public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int n = N;

        ArrayList<Integer> list = new ArrayList<>(N);
        while (n > 0) {
            list.add(scanner.nextInt());
            n -= 1;
        }

        ArrayList<Integer> list1 = new ArrayList<>(N);
        for (int i = 0; i < N; i++) {
            list1.add(0);
        }
        int res = 0;
        // 每次从每个口袋中取列表中最小值个糖果(0不管)
        while (!list1.equals(list)) {
            int min = Integer.MAX_VALUE;
            for (Integer i : list) {
                if (i == 0) continue;
                if (i < min) min = i;
            }

            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == 0) continue;
                list.set(i, list.get(i) - min);
            }

            res+=1;
        }
        System.out.println(res);
    }
}

题目4

输入示例:

txt 复制代码
5
1 -2 3 -4 5

输出示例:

txt 复制代码
15

思路很简单,但是当时做题提交的时候,通过率总是18%。不知道为啥,后面我改成了Long类型,然后就通过了全部用例。(易错1:第一次提交没考虑0的情况)

java 复制代码
// 这个是笔试通过的代码 当然优化之处很多
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int n = N;
        ArrayList<Long> list = new ArrayList<>(N);
        while (n > 0) {
            list.add(scanner.nextLong());
            n -= 1;
        }

        if (list.size() == 1) {
            System.out.println(list.get(0));
            return;
        }
        int cnt = 0;// 统计负数个数(0也算入)
        int zero = 0;
        for (Long i : list) {
            if (i == 0) zero = 1;
            if (i <= 0) {
                cnt += 1;
            }
        }

        Long res = 0L;
        Long min = Long.MAX_VALUE;
        for (Long i : list) {
            res += Math.abs(i);
            if (min > Math.abs(i)) min = Math.abs(i);
        }

        if (zero != 0) {
            System.out.println(res);
            return;
        }

        if ((cnt & 1) == 1) {
            // 奇数个负数
            res = res - min - min;
        }

        System.out.println(res);
    }
}
相关推荐
再见晴天*_*5 分钟前
logback 日志不打印
java·服务器·logback
闪电麦坤955 分钟前
数据结构:二维数组(2D Arrays)
数据结构·算法
mrbone119 分钟前
Git-git worktree的使用
开发语言·c++·git·cmake·worktree·gitab
幽络源小助理13 分钟前
SpringBoot基于JavaWeb的城乡居民基本医疗信息管理系统
java·spring boot·学习
欧阳有财16 分钟前
[java八股文][Mysql面试篇]日志
java·mysql·面试
凌肖战17 分钟前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode
浪裡遊20 分钟前
Sass详解:功能特性、常用方法与最佳实践
开发语言·前端·javascript·css·vue.js·rust·sass
TDengine (老段)25 分钟前
使用 StatsD 向 TDengine 写入
java·大数据·数据库·时序数据库·iot·tdengine·涛思数据
真实的菜27 分钟前
JVM类加载系统详解:深入理解Java类的生命周期
java·开发语言·jvm
埃菲尔铁塔_CV算法38 分钟前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉