Clean Java O(n) solution, easy to understand


  • 0
    J
    public class Solution {
        public boolean increasingTriplet(int[] nums) {
            int[] res = new int[3];
            int len = 0;
            for (int num : nums) {
                if (len == 0 || num > res[len - 1]) {
                    res[len++] = num;
                    if (len == 3) return true;
                }
                else {
                    int i = 0;
                    while (res[i] < num) i++;
                    res[i] = num;
                }
            }
            return false;
        }
    }

Log in to reply
 

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