The Android algorithm is a small algorithm about recursion and dichotomy.

Source: Internet
Author: User

The Android algorithm is a small algorithm about recursion and dichotomy.
// 1. Implement a function. Search for the specified value in two parts of an ordered integer array. If it is found, the position of the value is returned. If it is not found,-1 is returned.

Package demo; public class Mytest {public static void main (String [] args) {int [] arr = {1, 2, 5, 9, 11, 45}; int index = findIndext (arr, 0, arr. length-1, 12); System. out. println ("index =" + index);} // 1. implement a function that finds the specified value in two parts of an ordered integer array. If the value is found, the position of the value is returned. If the value is not found,-1 is returned. Public static int findIndext (int [] arr, int left, int right, int abc) {if (arr = null | arr. length = 0) {return-1;} if (left = right) {if (arr [left]! = Abc) {return-1 ;}} int mid = left + (right-left)/2; // 3 // 4 if (arr [mid]> abc) {// right = mid-1; return findIndext (arr, left, right, abc);} else if (arr [mid] <abc) {// 5 <45 // 9 <45/11 <45 left = mid + 1; return findIndext (arr, left, right, abc ); // 4.5 //} else {return mid ;}}}

 



 
/1. Implement a function. Search for the specified value in the second part of an ordered integer array. If it is found, the position of the value is returned. If it is not found,-1 is returned. // Array up the virtual array public int find (int [] array, int n) {if (array = null) {return-1;} int len = array. length; if (n <array [0] | n> array [len-1]) {return-1;} int left = 0; int right = len-1; while (left <right) {int mid = left + (right-left)/2; if (array [mid] = n) {return mid ;} else if (array [mid] <n) {left = mid + 1;} else {right = mid-1;} if (array [left] = n) {return left;} else {retu Rn right ;}// 2. Write a function to convert an array into a linked list. // Requirement: Do not use library functions (such as List). public static class Node {Node next; int data;} // return the public Node convert (int [] array) of the linked List header) {if (array = null | array. length = 0) {return null;} Node head = new Node (); head. data = array [0]; int len = array. length; Node end = head; for (int I = 1; I <len; I ++) {end = addNode (end, array [I]);} return head ;} // Add a public Node addNode (Node end, int data) {Node node Node = new No to the end of the linked list De (); node. data = data; end. next = node; return node;} // 3. there are two Arrays: [,] and [,]. Use the above function to generate two linked lists: linkA and // linkB, then combine the two linked lists into a linked list and the result is [1, 2, 3, 4, 5, 6, 7, 8, 9]. // requirement: do not generate a third linked list or a new node. // 3.1 Implement using recursion // public Node comb (int [] arrayA, int [] arrayB) {Node linkA = convert (arrayA); Node linkB = convert (arrayB ); node head; if (linkA. data = linkB. data) {head = linkA; linkA = linkA. next; linkB = linkB. next; head. next = null;} else if (linkA. data <linkB. data) {head = linkA; linkA = linkA. next; head. next = null;} else {head = linkB; linkB = linkB. next; head. next = null;} Node end = hea D; comb (end, headA, headB); return head;} // implement recursive public void comb (Node end, Node headA, Node headB) {if (headA = null & headB = null) {return;} else if (headA = null) {end. next = headB; return;} else if (headB = null) {end. next = headA; return;} if (headA. data 

 

Related Article

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.