当前位置:网站首页>力扣刷题日记/day5/2022.6.27
力扣刷题日记/day5/2022.6.27
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
求一个数组中的最大值,可以使用Math.max()函数
首先去MDN官网查看使用说明
想要直接获取一个数组中的最大值
可以使用以下拓展方法
var arr = [1, 2, 3];
var max = Math.max(...arr);
Math.max的使用方法已经了解
例题
解题思路:
整数网络以二维数组的形式存储,共有m行n列,共有m个客户,n个银行。
首先定义一个ans,返回最大资产ans
遍历m个客户,m=accounts.length
定义一个数值money,用来存储每个客户所有银行的资产
遍历n个银行,n=accounts[0].length
把每个银行的资产相加,money+=accounts[i][j];
再把客户资产最大值求出来,ans=Math.max[ans,money]
class Solution {
public int maximumWealth(int[][] accounts) {
int ans=0;
for(int i=0;i<accounts.length;i++)
{
int money=0;
for(int j=0;j<accounts[0].length;j++)
{
money +=accounts[i][j];
}
ans = Math.max(ans, money);
}
return ans;
}
}
求数组中最大值并且求出其下标
假设法
var arr = [1, 20, 30, 60, 80, 90, 99, 89, 100];
var max = arr[0]; //假设第一个数最大
int index = 0; // 定义的一个下标
for (var i = 0; i < arr.length; i++){ //让数组中其他数和我们假定的最大的数比较,遍历
if (max < arr[i]){ //让它们依次比较,如果比我们假定的数大
max = arr[i]; // 我们就将那个较大的数的值给max
index = i; // 将较大值的下标给index
}
} 最后的max就是最大值
console.log('该数组中的最大值是:'+ max);
console.log('数组中的最大值的下标是:'+index)
边栏推荐
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- gatling 之性能测试
- 数学分析_笔记_第7章:多元函数的微分学
- 线上MySQL的自增id用尽怎么办?
- General environmental instructions for the project
- [daily question] 871 Minimum refueling times
- Reptile elementary learning
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
- "In Vietnam, money is like lying on the street"
猜你喜欢
简单易用的地图可视化
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
How to improve development quality
90后开始攒钱植发,又一个IPO来了
【Hot100】32. Longest valid bracket
Detectron2 installation method
删除二叉搜索树中的节点附图详解
The block:usdd has strong growth momentum
Reptile elementary learning
明星开店,退,退,退
随机推荐
7 RSA Cryptosystem
国产数据库TiDB初体验:简单易用,快速上手
Interview summary of large factory Daquan II
[211] go handles the detailed documents of Excel library
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
People in the workplace with a miserable expression
With an estimated value of 90billion, the IPO of super chip is coming
怎么开户才是安全的,
你应该懂些CI/CD
Win32 API access route encrypted web pages
删除二叉搜索树中的节点附图详解
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
Neglected problem: test environment configuration management
[209] go language learning ideas
Lua emmylua annotation details
RecastNavigation 之 Recast
上市公司改名,科学还是玄学?
Wuzhicms code audit
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
使用3DMAX制作一枚手雷