当前位置:网站首页>Day31 LeetCode
Day31 LeetCode
2022-07-30 19:21:00 【太阳在坠落】
1. 数组序号转换
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
- 序号从 1 开始编号。
- 一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
- 每个数字的序号都应该尽可能地小。
分析:
先对arr进行复制并排序得到arr1,创建一个哈希表并对arr1进行遍历得到每个数字排序后的位置,然后创建一个数组res大小与arr相同,遍历arr,查找当前数字在哈希表中的value并把它value写进res的当前位置中。
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] sortedArr = new int[arr.length];
System.arraycopy(arr, 0, sortedArr, 0, arr.length);
Arrays.sort(sortedArr);
Map<Integer, Integer> ranks = new HashMap<Integer, Integer>();
int[] ans = new int[arr.length];
for (int a : sortedArr) {
if (!ranks.containsKey(a)) {
ranks.put(a, ranks.size() + 1);
}
}
for (int i = 0; i < arr.length; i++) {
ans[i] = ranks.get(arr[i]);
}
return ans;
}
}
2. 完全平方数
给你一个整数 n ,返回和为 n 的完全平方数的最少数量 。完全平方数是一个整数,其值等于另一个整数的平方;换句话说,其值等于一个整数自乘的积。例如,1、4、9 和 16 都是完全平方数,而 3 和 11 不是。
示例:
输入:n = 12
输出:3
解释:12 = 4 + 4 + 4
分析:
动态规划:
- 首先初始化长度为 n+1 的数组 dp,每个位置都为 0
- 如果 n 为 0,则结果为 0
- 对数组进行遍历,下标为 i,每次都将当前数字先更新为最大的结果,即 dp[i]=i,比如 i=4,最坏结果为 4=1+1+1+1 即为 4 个数字
- 动态转移方程为:dp[i] = MIN(dp[i], dp[i - j * j] + 1),i 表示当前数字,j*j 表示平方数
class Solution {
public int numSquares(int n) {
int[] dp = new int[n+1];
dp[0] = 0;
for (int i = 1; i < dp.length; i++) {
dp[i] = i;
for (int j = 1; i-j*j>=0; j++) {
dp[i] = Math.min(dp[i], dp[i - j*j]+1);
}
}
return dp[n];
}
}
3. 有效的正方形
给定2D空间中四个点的坐标 p1, p2, p3 和 p4,如果这四个点构成一个正方形,则返回 true 。点的坐标 pi 表示为 [xi, yi] 。输入不是按任何顺序给出的。一个有效的正方形有四条等边和四个等角(90度角)。
分析:
求出6条边,四条边长和两条对角线。边长相等的只有正方形和菱形,对角线又相等的只有正方形
所以排序判断边长以及对角线是否相等即可。
class Solution {
public long len(int[] x,int[] y){
return 1L * (x[0] - y[0]) * (x[0] - y[0]) + 1L * (x[1] - y[1]) * (x[1] - y[1]);
}
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
if(p1[0] == p2[0] && p1[1] == p2[1]) return false;
long[] l = new long[6];
l[0] = len(p1,p2);
l[1] = len(p1,p3);
l[2] = len(p1,p4);
l[3] = len(p2,p3);
l[4] = len(p2,p4);
l[5] = len(p3,p4);
Arrays.sort(l);
return l[0] == l[1] && l[0] == l[2] && l[0] == l[3] && l[4] == l[5];
}
}
边栏推荐
- MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
- nlohmann json 使用指南【visual studio 2022】
- Read the "Language Model" in one article
- - daily a LeetCode 】 【 191. A number of 1
- OneFlow source code analysis: Op, Kernel and interpreter
- Scala学习:类和对象
- DM8: Single database and single instance to build a local data guard service
- VS Code 连接SQL Server
- Encapsulates a console file selector based on inquirer
- redis
猜你喜欢

Spark学习:用spark实现ETL

Node encapsulates a console progress bar plugin

redis

Basic use of scrapy

电脑死机的时候,发生了什么?

防抖和节流有什么区别,分别用于什么场景?

Swiper rotates pictures and plays background music

解决终极bug,项目最终能顺利部署上线。

VBA batch import Excel data into Access database

The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
随机推荐
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
Read the "Language Model" in one article
How do radio waves transmit information?
JS提升:Promise中reject与then之间的关系
Multiple instances of mysql
Vulkan开启特征(feature)的正确姿势
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
Correct pose of Vulkan open feature
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
来了!东方甄选为龙江农产品直播带货
AWS console
Go 系统收集
Witness the magical awakening of the mini world in HUAWEI CLOUD
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
【科普】无线电波怎样传送信息?
MongoDB打破了原则引入SQL?
MySQL数据库————视图和索引
SimpleOSS third-party library libcurl and engine libcurl error solution
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
什么是 RESTful API?