Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. // Вариант 6
  2. //Pivot index
  3. // Найти элемент, для которого сумма всех элементов справа будет равна сумме всех элементов слева.
  4.         public int pivotIndex(int[] nums) {
  5.             int right = 0;
  6.             int left = 0;
  7. //Находим сумму элементов справа, а потом при движении по массиву,
  8. //отнимаем от нее действительный(на котором мы сейчас находимся) элемент,
  9. //и прибавляя прошлый к левой части.
  10.             for(int i = 0; i < nums.length; i++){
  11.                 right += nums[i];
  12.             }
  13.             for(int i = 0; i < nums.length; i++){
  14.                 if(i != 0){
  15.                     left += nums[i-1];
  16.                 }
  17.                 right -= nums[i];
  18.                 if(right == left){
  19.                     return i;
  20.                 }
  21.             }
  22.             return -1;
  23.         }
  24.  
  25. // 2.   Список з нулів і одиничок потрібно переупорядкувати так що нулі були спочатку
  26. // Я сделал двонаправленный
  27. // 0 and 1
  28.         private static DLNode order(DLNode head) {
  29.             DLNode pres = head;
  30.             DLNode zero = null;
  31.             DLNode one = null;
  32.             DLNode tmp = null;
  33.             while(pres != null) {
  34.                     //Delete
  35.                     if (pres.next != null) {
  36.                         pres.next.prev = pres.prev;
  37.                     }
  38.                     tmp = pres;
  39.                     //Move
  40.                     pres = pres.next;
  41.                     tmp.next = null;
  42.                     //Add in the end
  43.                     if(tmp.data == 0) {
  44.                         zero = addInTheEnd(zero, tmp);
  45.                     }
  46.                     else {
  47.                         one = addInTheEnd(one, tmp);
  48.                     }
  49.             }
  50.             //Connection
  51.             zero = addInTheEnd(zero, one);
  52.             return zero;
  53.         }
  54. // Дополнительная функция
  55.     private static DLNode addInTheEnd(DLNode head, DLNode node) {
  56.             if(head == null) {
  57.                 return node;
  58.             }
  59.             if(node == null) {
  60.                 return head;
  61.             }
  62.             DLNode tail = head;
  63.             while(tail.next != null) {
  64.                 tail = tail.next;
  65.             }
  66.             tail.next = node;
  67.             node.prev = tail;
  68.             return head;
  69.         }
  70.  
  71.  
  72. // Вариант 7
  73. // Найти количество вхождений через бинарку
  74.         public static int frequency(char ar[], char x) {
  75.             //Sort
  76.             SelectionSort(ar);
  77.             //Search
  78.             int firstIndex = binFirst(ar, x);
  79.             //Counting
  80.             int count = 0;
  81.             if(firstIndex != -1) {
  82.                 // If firstIndex == ar.length, we will go beyond the array
  83.                 while(firstIndex != ar.length && ar[firstIndex] == x) {
  84.                     count++;
  85.                     firstIndex++;
  86.                 }
  87.             }
  88.             // Visual check
  89.             System.out.println("In this array");
  90.             printList(ar);
  91.             System.out.printf("Symbol \"" + x + "\" is repeated " + count + " times");
  92.            
  93.             return count;
  94.         }
  95. //Дополнительные функции
  96. public static void SelectionSort(char [] ar) {
  97.            
  98.             char tmp = 0;
  99.             // sorting
  100.             for (int i = 0; i < ar.length - 1; i++) {
  101.                 int minIndex = i;
  102.                 for (int j = i + 1; j < ar.length; j++) {
  103.                     if (ar[j] < ar[minIndex]) {
  104.                         minIndex = j;
  105.                     }
  106.                 }
  107.                 if (i != minIndex) {
  108.                     tmp = ar[i];
  109.                     ar[i] = ar[minIndex];
  110.                     ar[minIndex] = tmp;
  111.                 }
  112.             }
  113.         }
  114.  
  115. public static int binFirst(char ar[], char x) {
  116.             int l = 0;
  117.             int h = ar.length - 1;
  118.             while(h >= l) {
  119.                 int mid = (l + h)/2;
  120.                 if((mid == 0 || x > ar[mid-1]) && ar[mid] == x)
  121.                     return mid;
  122.                 else if (x > ar[mid])
  123.                     l = mid + 1;
  124.                 else
  125.                     h = mid - 1;               
  126.             }
  127.             return -1;
  128.         }
  129.  
  130. private static void printList(char [] array) {
  131.             if(array.length == 0) {
  132.                 System.out.println("Your Array is empty.");
  133.             }else {
  134.                 for(int i = 0; i <  array.length; i++) {
  135.                     System.out.printf(array[i] + " ");
  136.                 }
  137.             }
  138.             System.out.println("");
  139.         }
  140.  
  141. // Сорт выборкой двонаправленного списка с фиктивными узлами
  142. //  Selection sort (взято у Хоменко)
  143.         private static DLNode selectionSort(DLNode h) {
  144.            
  145.             DLNode s = null; // head of sorted list
  146.             //Гениально...
  147.             h = DelFict(h);
  148.             while (h != null) {
  149.                
  150.                 // find the node with maximum value in the unsorted list
  151.                 DLNode max = maxNode(h);
  152.                 // remove the node with maximum value from unsorted list
  153.                 h = remove(h, max);
  154.                 // insert the node with maximum value at the front of sorted list
  155.                 s = addFirst(s, max);
  156.             }
  157.             s = makeFictList(s);
  158.             return s;
  159.         }
  160. //Дополнительные функции
  161. //Delete a fict
  162.         public static DLNode DelFict(DLNode h) {
  163.             if(h == null) return h;
  164.             if(h.prev == null) return h;
  165.             h.prev.prev.next = null;
  166.             h.prev = null;
  167.             return h;
  168.         }
  169.  
  170.     public static DLNode maxNode(DLNode h) {
  171.             if (h == null || h.data == FICT_NUMBER)
  172.                 return h;
  173.             else {
  174.                 DLNode max = h;
  175.                 DLNode cur = h;
  176.                 while (cur != null && cur.data != FICT_NUMBER) {
  177.                     if (cur.data > max.data) {
  178.                         max = cur;
  179.                     }
  180.                     cur = cur.next;
  181.                 }
  182.                 return max;
  183.             }
  184.         }
  185.  
  186.         public static DLNode remove(DLNode h, DLNode x) {
  187.             // check if x is the head
  188.             if (x.prev != null) {
  189.                 x.prev.next = x.next;
  190.             } else {
  191.                 h = h.next;
  192.             }
  193.             // check if x is the tail
  194.             if (x.next != null) {
  195.                 x.next.prev = x.prev;
  196.             }
  197.             return h;
  198.         }
  199.  
  200. public static DLNode addFirst(DLNode h, DLNode newNode) {
  201.             newNode.prev = null;
  202.             newNode.next = h;
  203.             if (h != null) {
  204.                 h.prev = newNode;
  205.             }
  206.             return newNode;
  207.         }
  208.        
  209.         public static DLNode makeFictList(DLNode h) {
  210.             DLNode fict = createDLNode(FICT_NUMBER);
  211.             DLNode tail = tailSearch(h);
  212.             tail.next = fict;
  213.             fict.prev = tail;
  214.             h.prev = fict;
  215.             fict.next = h;
  216.             return h;
  217.         }
  218.        
  219.         public static DLNode tailSearch(DLNode h) {
  220.             if (h == null)
  221.                 return h;
  222.             DLNode t = h;
  223.             while(t.next != null) {
  224.                 t = t.next;
  225.             }
  226.             return t;
  227.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement