当前位置:网站首页>力扣刷题日记/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
- Neglected problem: test environment configuration management
- 【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
- 项目通用环境使用说明
- Win32 API access route encrypted web pages
- Machine learning concept drift detection method (Apria)
- The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
- Redis主从复制
- Thawte通配符SSL证书提供的类型有哪些
- [HCIA continuous update] WAN technology
猜你喜欢

Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)

蓝桥:合根植物

RecastNavigation 之 Recast

Blue bridge: sympodial plant

Imitation of numpy 2

Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction

爬虫初级学习

DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌

Machine learning concept drift detection method (Apria)

Make a grenade with 3DMAX
随机推荐
Android uses sqliteopenhelper to flash back
TCP两次挥手,你见过吗?那四次握手呢?
Is it safe to download the mobile version of Anxin securities and open an account online
Make a grenade with 3DMAX
表情包坑惨职场人
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
数学分析_笔记_第7章:多元函数的微分学
无心剑中译伊丽莎白·毕肖普《一门技艺》
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
Grain Mall (I)
The controversial line of energy replenishment: will fast charging lead to reunification?
Performance test of Gatling
Interview summary of large factory Daquan II
通过事件绑定实现动画效果
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
Wuzhicms code audit
【210】PHP 定界符的用法