<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)628:三個(gè)數(shù)的最大乘積

          共 1532字,需瀏覽 4分鐘

           ·

          2022-06-09 01:02

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?三個(gè)數(shù)的最大乘積,我們先來看題面:
          https://leetcode.cn/problems/maximum-product-of-three-numbers/

          Given?an?integer?array?nums,?find?three?numbers?whose?product?is?maximum?and?return?the?maximum?product.

          給你一個(gè)整型數(shù)組 nums ,在數(shù)組中找出由三個(gè)數(shù)組成的最大乘積,并輸出這個(gè)乘積。

          示例

          示例 1:

          輸入:nums = [1,2,3]
          輸出:6

          示例 2:

          輸入:nums = [1,2,3,4]
          輸出:24

          示例 3:

          輸入:nums = [-1,-2,-3]
          輸出:-6


          解題
          https://blog.csdn.net/weixin_45367847/article/details/118599029

          排序后,如果數(shù)組中全是非負(fù)數(shù),則排序后最大的三個(gè)數(shù)相乘即為最大乘積;如果全是非正數(shù),則最大的三個(gè)數(shù)相乘同樣也為最大乘積。


          如果數(shù)組中有正數(shù)有負(fù)數(shù),則最大乘積既可能是三個(gè)最大正數(shù)的乘積,也可能是兩個(gè)最小負(fù)數(shù)(即絕對值最大)與最大正數(shù)的乘積。



          class?Solution?{
          ????public?int?maximumProduct(int[] nums)?{
          ????????int?min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
          ????????int?max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
          ????????for(int?x : nums){
          ????????????if(x < min1){
          ????????????????min2 = min1;
          ????????????????min1 = x;
          ????????????}else?if(x < min2){
          ????????????????min2 = x;
          ????????????}
          ????????????if(x > max1){
          ????????????????max3 = max2;
          ????????????????max2 = max1;
          ????????????????max1 = x;
          ????????????}else?if(x > max2){
          ????????????????max3 = max2;
          ????????????????max2 = x;
          ????????????}else?if(x > max3){
          ????????????????max3 = x;
          ????????????}
          ????????}
          ????????return?Math.max(min1 * min2 * max1, max1 * max2 * max3);
          ????}
          }

          上期推文:
          LeetCode1-620題匯總,希望對你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)621:任務(wù)調(diào)度器
          LeetCode刷題實(shí)戰(zhàn)622:設(shè)計(jì)循環(huán)隊(duì)列
          LeetCode刷題實(shí)戰(zhàn)623:在二叉樹中增加一行
          LeetCode刷題實(shí)戰(zhàn)624:數(shù)組列表中的最大距離
          LeetCode刷題實(shí)戰(zhàn)625:最小因式分解
          LeetCode刷題實(shí)戰(zhàn)626:換座位
          LeetCode刷題實(shí)戰(zhàn)627:變更性別

          瀏覽 23
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天操天天天 | 在线免费观看黄色视频网站 | 黄色一级录像片 | 男人天堂色天堂 | 国产成人精品一区二区毛片 |