iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
AI & Data

想到甚麼打甚麼系列 第 21

找LeetCode上簡單的題目來撐過30天啦(DAY21)

  • 分享至 

  • xImage
  •  

不知道要打什麼,直接開始

題號:739 標題:Daily Temperatures 難度:Medium

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]

Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]

Constraints:
• 1 <= temperatures.length <= 105
• 30 <= temperatures[i] <= 100

我的程式碼

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] result = new int[temperatures.length];
        Arrays.fill(result, 0);
        int i,j,k,count=0;
        for(i=0;i<temperatures.length-1;i++){
            count = 1;
            if(temperatures[i]<temperatures[i+1]){
                result[i] = 1;
                continue;
            }
            for(j=i+1;j<temperatures.length;j++){
                if(temperatures[i]<temperatures[j]){
                    result[i] = count;
                    break;
                }
                count++;
            }
        }
        return result;
    }
}

花比較久的時間
又是Time Limit Exceeded,我在第二層for前加上,if(temperatures[i]<temperatures[i+1])判斷,就過了!?

DAY21心得
我真的很常遇到Time Limit Exceeded,我想是我得思考還不夠精準?真的很欽佩大神們都有很多更聰明的解法,慢慢學習囉


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY20)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY22)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言