Java每日一练(20230414) Pow(x, n) 、旋转图像、买卖股票的最佳时机 IV

简介: Java每日一练(20230414) Pow(x, n) 、旋转图像、买卖股票的最佳时机 IV

1. Pow(x, n)  

实现 pow(x, n),即计算 x 的 n 次幂函数(即,xn)。

示例 1:

输入:x = 2.00000, n = 10

输出:1024.00000

示例 2:

输入:x = 2.10000, n = 3

输出:9.26100

示例 3:

输入:x = 2.00000, n = -2

输出:0.25000

解释:2^-2 = 1/2^2 = 1/4 = 0.25


提示:

  • -100.0 < x < 100.0
  • -2^31 <= n <= 2^31-1
  • -10^4 <= x^n <= 10^4
public class myPow {
    public static class Solution {
        public double myPow(double x, int n) {
            if (n < 0) {
                return 1 / pow(x, -n);
            } else {
                return pow(x, n);
            }
        }
        private double pow(double x, int n) {
            if (n == 0) {
                return 1.0;
            }
            if (n == 1) {
                return x;
            }
            double val = pow(x, n / 2);
            if (n % 2 == 0) {
                return val * val;
            } else {
                return val * val * x;
            }
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.myPow(2.0, 10));
        System.out.println(s.myPow(2.1, 3));
        System.out.println(s.myPow(2.0, -2));
    }
}

输出:

1024.0

9.261000000000001

0.25


2. 旋转图像

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]

输出:[[7,4,1],[8,5,2],[9,6,3]]


示例 2:

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]


示例 3:

输入:matrix = [[1]]

输出:[[1]]

示例 4:

输入:matrix = [[1,2],[3,4]]

输出:[[3,1],[4,2]]


提示:

  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
import java.util.*;
public class rotate {
    public static class Solution {
        public void rotate(int[][] matrix) {
            int len = matrix.length;
            int le = len - 1;
            for (int i = 0; i < len / 2; i++) {
                for (int j = 0; j < (len + 1) / 2; j++) {
                    int temp = matrix[i][j];
                    matrix[i][j] = matrix[le - j][i];
                    matrix[le - j][i] = matrix[le - i][le - j];
                    matrix[le - i][le - j] = matrix[j][le - i];
                    matrix[j][le - i] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        s.rotate(matrix);
        System.out.println(Arrays.deepToString(matrix));
        int[][] matrix2 = {{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
        s.rotate(matrix2);
        System.out.println(Arrays.deepToString(matrix2));
        int[][] matrix3 = {{1}};
        s.rotate(matrix3);
        System.out.println(Arrays.deepToString(matrix3));
        int[][] matrix4 = {{1,2},{3,4}};
        s.rotate(matrix4);
        System.out.println(Arrays.deepToString(matrix4));
    }
}

输出:

[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

[[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]

[[1]]

[[3, 1], [4, 2]]

代码中可以用 di, dj 两个变量来美化一下原题代码中的数组下标:

class Solution {
    public void rotate(int[][] matrix) {
        int len = matrix.length;
        for (int i = 0; i < len / 2; i++) {
          int di = len - i - 1;
            for (int j = 0; j < (len + 1) / 2; j++) {
                int temp = matrix[i][j];
                int dj = len - j - 1;
                matrix[i][j] = matrix[dj][i];
                matrix[dj][i] = matrix[di][dj];
                matrix[di][dj] = matrix[j][di];
                matrix[j][di] = temp;
            }
        }
    }
}

3. 买卖股票的最佳时机 IV

给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:k = 2, prices = [2,4,1]

输出:2

解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

示例 2:

输入:k = 2, prices = [3,2,6,5,0,3]

输出:7

解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。

随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。


提示:

  • 0 <= k <= 100
  • 0 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000

代码:

import java.util.*;
public class maxProfit {
    public static class Solution {
        public int maxProfit(int k, int[] prices) {
            if (k < 1)
                return 0;
            if (k >= prices.length / 2)
                return greedy(prices);
            int[][] t = new int[k][2];
            for (int i = 0; i < k; ++i)
                t[i][0] = Integer.MIN_VALUE;
            for (int p : prices) {
                t[0][0] = Math.max(t[0][0], -p);
                t[0][1] = Math.max(t[0][1], t[0][0] + p);
                for (int i = 1; i < k; ++i) {
                    t[i][0] = Math.max(t[i][0], t[i - 1][1] - p);
                    t[i][1] = Math.max(t[i][1], t[i][0] + p);
                }
            }
            return t[k - 1][1];
        }
        private int greedy(int[] prices) {
            int max = 0;
            for (int i = 1; i < prices.length; ++i) {
                if (prices[i] > prices[i - 1])
                    max += prices[i] - prices[i - 1];
            }
            return max;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] prices = {2,4,1};
        System.out.println(s.maxProfit(2, prices));
        int[] prices2 = {3,2,6,5,0,3};
        System.out.println(s.maxProfit(2, prices2));
    }
}

输出:

2

7


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力!

🌟 收藏,你的青睐是我努力的方向!

评论,你的意见是我进步的财富!  

主页:https://hannyang.blog.csdn.net/


目录
相关文章
|
1月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
165 3
|
1月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
381 3
|
1月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
214 0
|
1月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
247 3
|
1月前
|
人工智能 缓存 自然语言处理
Java与多模态AI:构建支持文本、图像和音频的智能应用
随着大模型从单一文本处理向多模态能力演进,现代AI应用需要同时处理文本、图像、音频等多种信息形式。本文深入探讨如何在Java生态中构建支持多模态AI能力的智能应用。我们将完整展示集成视觉模型、语音模型和语言模型的实践方案,涵盖从文件预处理、多模态推理到结果融合的全流程,为Java开发者打开通往下一代多模态AI应用的大门。
305 41
|
2月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
270 102
|
2月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
299 104
|
2月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
250 103
|
2月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
189 82
|
2月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
76 0

推荐镜像

更多