5G 7-digit phone number, de-weight, memory 20MB, code implementation.

Source: Internet
Author: User

Transferred from: http://www.aboutyun.com/thread-11139-1-1.html

Answer:
First of all, this question is not distributed
7 digits, at least to use int to save, then int is 4 bytes, 20MB memory 10^7*4/1024*1024=38.14697265625 need at least 38MB, obviously 7 digits can not be saved all
Save a number can not use 4 bytes, to use 2 bytes or 1 bytes, then can only use short or byte, but short maximum 65536,byte maximum 256, can not be satisfied.
Ideas:
No matter the phone number is not there, all the phone numbers are 10^7.
Create an array of type byte with an array size of 10^7, which requires 10^7*1/1024*1024=9.5MB memory altogether
Each subscript of the array corresponds to a phone number, the value of the array represents whether to repeat, the initialization of the time to let the array of each value is 0, read once set to 1, read to set 2, then the value of 1 is the number to go to the weight.
Code:

  1. public class Sorttest {
  2. public static void Main (string[] args) {
  3. Int[] A = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1,38,65};
  4. System.out.println ("Go to weight before ...");
  5. Printsort (a);
  6. byte[] ret = new byte[10000000];
  7. for (int i=0;i<a.length;i++) {
  8. if (ret[a[i]] = = 0)
  9. Ret[a[i]] = 1;
  10. else if (ret[a[i]] = = 1)
  11. Ret[a[i]] = 2;
  12. }
  13. System.out.println ("After the heavy ...");
  14. for (int i=0;i<ret.length;i++) {
  15. if (ret[i]==1)
  16. System.out.print (i + "");
  17. }
  18. }
  19. public static void Printsort (int[] a) {
  20. for (int i = 0; i < a.length; i++) {
  21. System.out.print (A[i] + "");
  22. }
  23. System.out.println ();
  24. }
  25. }
Copy Code

5G 7-digit phone number, de-weight, memory 20MB, code implementation.

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.