Concise c# solution


  • 1
    M
    public class Solution {
    public int ThirdMax(int[] nums) {
        int m = 0, m2 = 0, m3 = 0, c = 0;
    
        foreach(var n in nums) {
            if(c == 0 || n > m) {
                m3 = m2;
                m2 = m;
                m = n;
            } else if(n < m && (c == 1 || n > m2)) {
                m3 = m2;
                m2 = n;
            } else if(n < m2 && (c == 2 || n > m3)) {
                m3 = n;
            } else {
                continue;
            }
            c++;
        }
        
        return c > 2 ? m3 : m;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.