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:
- public class Sorttest {
- public static void Main (string[] args) {
- Int[] A = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1,38,65};
- System.out.println ("Go to weight before ...");
- Printsort (a);
- byte[] ret = new byte[10000000];
- for (int i=0;i<a.length;i++) {
- if (ret[a[i]] = = 0)
- Ret[a[i]] = 1;
- else if (ret[a[i]] = = 1)
- Ret[a[i]] = 2;
- }
- System.out.println ("After the heavy ...");
- for (int i=0;i<ret.length;i++) {
- if (ret[i]==1)
- System.out.print (i + "");
- }
- }
- public static void Printsort (int[] a) {
- for (int i = 0; i < a.length; i++) {
- System.out.print (A[i] + "");
- }
- System.out.println ();
- }
- }
Copy Code
5G 7-digit phone number, de-weight, memory 20MB, code implementation.