Algorithm--The number of different absolute values for an ordered array

Source: Internet
Author: User

Topic:

Given an ordered array, the absolute number of its elements is calculated. such as array [-3,-1, 0, 0, 2, 3, 5], returns 5.

Analysis:

First way of thinking. The array is traversed once, putting the absolute value of each element into a set, and finally finding the size of the set. Although this method is very simple, but the space complexity is not small, is O (n). So can you just go through it again and the space complexity is O (1). Well, here's a way to analyze the time complexity of O (n) and the spatial complexity of O (1). Suppose, if the absolute value of each element in the array is not the same, then when traversing to the first element, count Count plus 1, then the final result is the absolute number, right? What if there are two identical elements adjacent to each other? Then you can skip this element without counting. What if there is a positive or negative two element relative to the absolute value? So the two elements of the and 0 pairs, and then make the traversal of the two index, the small plus 1, the large minus 1, and count only calculate a number on it. Right?

Now let's take a look at the specific Java code:

1  Public intCountdistinctabs (int[] nums) {2     if(Nums = =NULL|| Nums.length = = 0) {3         return-1;4     }5     intI, J, Count;6i = 0;7j = Nums.length-1;8Count = 0;9      while(I <j) {Ten         if(I < nums.length-1 && nums[i] = = nums[i+1]) { One             Continue; A         } -         if(J > 0 && nums[j] = = Nums[j-1]) { -             Continue; the         } -         if(Nums[i]+nums[j] = = 0) {//the absolute value of the plus or minus two numbers is the same, if the array has only positive or negative numbers, then there is no such case, if the branch will go to the following two cases -i++; -j--; +}Else if(Nums[i] + nums[j] > 0) {//if the absolute value of a positive number is large, or if it is all positive, just walk the branch -j--; +}Else if(Nums[i] + Nums[j] < 0) {//if the absolute value of a negative number is large, or if the proposition is negative, go only that branch Ai++; at         } -count++ -     } -     returncount; -}

and solved a problem!!! Ha ha

Algorithm--The number of different absolute values for an ordered array

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.