Learning really makes me happy ~

Source: Internet
Author: User

(1) Remove Element

The normal idea code is as follows:

1  Public classSolution {2      Public intRemoveelement (int[] Nums,intval) {3         intCount = 0;4          for(inti = 0; i < nums.length; i++) {5             if(Nums[i]! =val) {6nums[count++] =Nums[i];7             }8         }9         returncount;Ten     } One}
View Code

Learning makes me happy source, the best way to solve the problem is really good wit!!! The code is as follows:

1  Public classSolution {2      Public intRemoveelement (int[] Nums,intval) {3         inti = 0;4         intindex = nums.length-1;5          while(I <=index) {6             if(Nums[i] = =val) {7Nums[i] = nums[index--];8             }9             Else {Teni++; One             } A         } -         returnIndex + 1; -     } the}
View Code

The idea of solving a problem: Starting with the first element, if the element is the same as the specified element, replace with the last element (to prevent the [3,2,2,3] error, you cannot use for to follow the bad every time I is added one), and then the last element position minus 1, if the element is different from the specified element, Just add 1.

(2) Majority Element

The best way to solve the problem is as follows: Record the main element with a variable, initialize it to the first element, a variable record occurrence number, initialize to 1, iterate over the elements in the array, the same as the main element of the current record, the number is 1, the difference is minus 1, if the number is reduced to 0, then the main element is replaced This way, the main element that gets the final record is the result. Because the main elements occur more often than N/2, it is important to imagine that the last one left is the main element. Other elements, even if they are recorded, are discarded because of the number of times they are zeroed out. This method only needs to traverse the array once, the time complexity is 0 (n), the space complexity is 0 (1).

The code is as follows:

1  Public classSolution {2      Public intMajorityelement (int[] nums) {3         intCount = 0;4         intMajorty =-1;5          for(inti = 0; i < nums.length; i++) {6             if(count = = 0) {7Majorty =Nums[i];8Count = 1;9             }Ten             Else if(Majorty = =Nums[i]) { Onecount++; A             } -             Else { -count--; the             } -         } -         returnMajorty; -     } +}
View Code

(3) best time to Buy and Sell Stock

Test Instructions: an array representing the price of the stock per day, and the number of I in the array represents the price of the stock on the first day. If only one transaction is allowed, that is to say, buy only one stock and sell it for maximum benefit.

problem-solving ideas: Dynamic programming method. From the previous traversal of the array, record the current lowest price, as the buy price, and calculate the return on the day of the sale of the proceeds, as the maximum possible benefits, throughout the process, the greatest benefit is the demand.

Code:

1  Public classSolution {2      Public intMaxprofit (int[] prices) {3         if(Prices.length < 2) {4             return0;5         }6         intMaxprofit = 0;7         intCurmin = Prices[0];8          for(inti = 1; i < prices.length; i++) {9Curmin =math.min (Curmin, prices[i]);TenMaxprofit = Math.max (Maxprofit, Prices[i]-curmin); One         } A         returnMaxprofit; -     } -}
View Code

The time complexity is O (n) time, and the space complexity is O (1).

The idea of the best solution is interlinked, the code is slightly different, as follows:

1  Public classSolution {2      Public intMaxprofit (int[] prices) {3         if(Prices.length = = 0 | | prices = =NULL) {4             return0;5         }6         intMaxprofit = 0;7         intCurmin =Integer.max_value;8        //int curmin = Prices[0];(also available)9          for(inti:prices) {TenCurmin = i > curmin?curmin:i; OneMaxprofit = maxprofit > (i-curmin)? MAXPROFIT:I-curmin; A         } -         returnMaxprofit; -     } the}
View Code

A foreach loop traversal is used.

Learning really makes me happy ~

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.