当前位置:网站首页>力扣刷题日记/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)
边栏推荐
- [system disk back to U disk] record the operation of system disk back to U disk
- 你应该懂些CI/CD
- LD_ LIBRARY_ Path environment variable setting
- 项目通用环境使用说明
- Easy to use map visualization
- Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
- android使用SQLiteOpenHelper闪退
- [211] go handles the detailed documents of Excel library
- Implementation of shell script replacement function
- Is it science or metaphysics to rename a listed company?
猜你喜欢
Is it science or metaphysics to rename a listed company?
曾经的“彩电大王”,退市前卖猪肉
创业两年,一家小VC的自我反思
[HCIA continuous update] WAN technology
Machine learning concept drift detection method (Apria)
MySQL common add, delete, modify and query operations (crud)
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
如何提高开发质量
Numpy 的仿制 2
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
随机推荐
2022年全国CMMI认证补贴政策|昌旭咨询
【每日一题】556. 下一个更大元素 III
[system analyst's road] Chapter 7 double disk system design (structured development method)
曾经的“彩电大王”,退市前卖猪肉
【Hot100】31. Next spread
你应该懂些CI/CD
大厂面试总结大全二
Detectron2 installation method
上市公司改名,科学还是玄学?
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
线上MySQL的自增id用尽怎么办?
【210】PHP 定界符的用法
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
RecastNavigation 之 Recast
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
ISO27001认证办理流程及2022年补贴政策汇总
国产数据库TiDB初体验:简单易用,快速上手
网上开户安全吗?是真的吗?
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
About the pit of firewall opening 8848 when Nacos is started