697. degree of an Array

Source: Internet
Author: User

Original title link: https://leetcode.com/problems/degree-of-an-array/description/

Import Java.util.HashMap;

Import Java.util.Map;
 /** * Created by Joe on 2018/3/14. * 697.  Degree of an Array * https://leetcode.com/problems/degree-of-an-array/description/*/public class P697 {public int
        Findshortestsubarray (int[] nums) {//Create a HashMap//key for the value//value to degree, start, end of the ternary group

        Map<integer, int[]> map = new hashmap<> ();
                for (int i = 0; i < nums.length; i++) {//If there is no this value if (!map.containskey (Nums[i])) {
                int[] Value = new Int[]{1, I, i};
            Map.put (Nums[i], value);
                } else {int[] value = Map.get (Nums[i]);
                Modification and end position Value[0] + = 1;

                VALUE[2] = i;
            Map.put (Nums[i], value);
        }} int maxdegree = 0;


        int smallest = Integer.max_value;
            For (Integer Key:map.keySet ()) {int[] value = Map.get (key); if (valuE[0] > Maxdegree) {maxdegree = value[0];
            smallest = value[2]-value[1] + 1; } else if (value[0] = = maxdegree) {smallest = smallest < value[2]-value[1] + 1? smallest:value[2]
            -value[1] + 1;
    }} return smallest;
    public static void Main (string[] args) {new P697 (). Findshortestsubarray (New int[]{1,2,2,3,1});
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.