当前位置:网站首页>May brush question 01 - array
May brush question 01 - array
2022-07-06 09:38:00 【A Guang chasing dreams】
May brush questions 01—— Array
Today's brush topic content : Array
Preface
- An algorithm opens the way to brush the questions of waste materials , Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- New column , Try to work out at least one question every day =v=
- Language java、python、c\c++
One 、 Today's topic
- 2016. The maximum difference between incremental elements
- 2239. Find the closest to 0 The number of
- 1475. The final price after discount
- 2248. Intersection of multiple arrays
Two 、 Their thinking
1. 2016. The maximum difference between incremental elements
- According to the requirements of the topic is to find the minimum value of the preceding subscript and the maximum value of the difference between the following subscript
- Use a variable to record the minimum value of the preceding item
- Maintain the minimum value variable and update the difference
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. Find the closest to 0 The number of
- To find the closest 0 Number of numbers , Is to find the number with the smallest absolute value
- When the absolute value is the same, take a larger number
- Use two variables to maintain absolute value and true value
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);
// The absolute value of the current number is smaller than the recorded value
if (x < absval){
absval = x;
real = n;
}
// The absolute value of the current number is the same
else if(x == absval){
real = real > n ? real: n; // The truth value takes the greater value
}
}
return real;
}
}
3. 1475. The final price after discount
- The value in front of the array subscript can be deducted by the smaller value in the back
- Just traverse the array , Get the smaller value after the array
- Just update the array
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. Intersection of multiple arrays
- Because the range of numbers is not large , So we can use
hash
Table to do questions- Count the number of all numbers in each array
- If the number of numbers is the same as the number of arrays, it means that this number is a subset of the intersection
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
边栏推荐
- Minio distributed file storage cluster for full stack development
- Compilation of libwebsocket
- [Yu Yue education] Wuhan University of science and technology securities investment reference
- [deep learning] semantic segmentation - source code summary
- QML control type: menu
- What are the models of data modeling
- Redis core configuration
- 发生OOM了,你知道是什么原因吗,又该怎么解决呢?
- Libuv thread
- Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
随机推荐
Redis之Bitmap
YARN组织架构
Global and Chinese market of bank smart cards 2022-2028: Research Report on technology, participants, trends, market size and share
Hard core! One configuration center for 8 classes!
Kratos战神微服务框架(二)
[shell script] use menu commands to build scripts for creating folders in the cluster
Sentinel mode of redis
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Lua script of redis
Basic concepts of libuv
Servlet learning diary 8 - servlet life cycle and thread safety
五月刷题01——数组
QDialog
Redis cluster
Oom happened. Do you know the reason and how to solve it?
MapReduce instance (IV): natural sorting
QML control type: menu
英雄联盟轮播图自动轮播
CSP salary calculation
Redis之Geospatial