当前位置:网站首页>五月刷题01——数组
五月刷题01——数组
2022-07-06 09:02:00 【追逐梦想的阿光】
今日刷题内容: 数组
前言
- 一个算法废材的刷题之路开更了, 更新每天刷题的题解内容
- 注重个人理解,看难度更新题目数量
- 题目来源于力扣
- 新开专栏,争取每日都能做出至少一题=v=
- 语言java、python、c\c++
一、今日题目
二、解题思路
1. 2016. 增量元素之间的最大差值
- 按照题目要求是求前项下标的最小值于后项下标的差最大值
- 用一个变量来记录前项的最小值
- 维护最小值变量和更新差值即可
class Solution {
public int maximumDifference(int[] nums) {
int i = 0;
int j = 1;
int n = nums.length;
int ret = - 1;
int pre = nums[0];
while(j < n){
if(nums[j] > pre){
ret = Math.max(ret, nums[j] - pre);
}else{
pre = nums[j];
}
j++;
}
return ret;
}
}
2. 2239. 找到最接近 0 的数字
- 要找最接近0的数,就是找绝对值最小的数
- 当绝对值相同时要取更大的数
- 用两个变量来维护绝对值和真值
class Solution {
public int findClosestNumber(int[] nums) {
int real = nums[0];
int absval = Math.abs(nums[0]);
for (int n: nums){
int x = Math.abs(n);
// 当前数绝对值比记录值更小时
if (x < absval){
absval = x;
real = n;
}
// 当前数绝对值相同时
else if(x == absval){
real = real > n ? real: n; // 真值取更大值
}
}
return real;
}
}
3. 1475. 商品折扣后的最终价格
- 数组下标前面的值可以用后面更小的值来抵扣
- 只需遍历数组,获取数组后面的更小值
- 更新数组即可
class Solution {
public int[] finalPrices(int[] prices) {
int i, j;
int n = prices.length;
for(i = 0; i < n; i++){
for (j = i+1; j < n; j++){
if (prices[j] <= prices[i]){
prices[i] -= prices[j];
break;
}
}
}
return prices;
}
}
4. 2248. 多个数组求交集
- 因为数的范围并不大,所以可以采用
hash表来做题- 统计每个数组中的所有数的个数
- 如果数的个数和数组的个数相同说明这个数是一个交集的子集
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
length = len(nums)
ret = [];
hash_tb = [0] * 1001
for num in nums:
for n in num:
hash_tb[n] += 1
for idx, val in enumerate(hash_tb):
if val == length:
ret.append(idx)
return ret
边栏推荐
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- Publish and subscribe to redis
- Le modèle sentinelle de redis
- Lua script of redis
- 基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
- CSP salary calculation
- Libuv thread
- Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
- 【shell脚本】——归档文件脚本
- Pytest parameterization some tips you don't know / pytest you don't know
猜你喜欢

Servlet learning diary 8 - servlet life cycle and thread safety

发生OOM了,你知道是什么原因吗,又该怎么解决呢?

基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)

Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)

LeetCode41——First Missing Positive——hashing in place & swap

DCDC power ripple test

Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
![[oc]- < getting started with UI> -- learning common controls](/img/2c/d317166e90e1efb142b11d4ed9acb7.png)
[oc]- < getting started with UI> -- learning common controls
![[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology](/img/01/8c0b8ea397c601311ae45acaaf00da.jpg)
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology

Redis之Lua脚本
随机推荐
Different data-driven code executes the same test scenario
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
Mathematical modeling 2004b question (transmission problem)
Redis之cluster集群
Redis' bitmap
Redis之连接redis服务命令
Vs All comments and uncomments
Redis之发布订阅
Kratos战神微服务框架(一)
Redis core configuration
Minio distributed file storage cluster for full stack development
Kratos ares microservice framework (I)
Improved deep embedded clustering with local structure preservation (Idec)
美团二面:为什么 Redis 会有哨兵?
Leetcode:608 树节点
Scoped in webrtc_ refptr
QML type: overlay
Parameterization of postman
CSP salary calculation
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share