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/