Java algorithm applet (1)

Source: Internet
Author: User

1. In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.

A) regular programs that traverse two-dimensional arrays directly

public class Solution {

public boolean Find (int [] [] array,int target) {

int flag = 0;

for (int i = 0; i < Array.Length; i++) {

int[] arr2 = array[i];

for (int j = 0; J < Arr2.length; J + +) {

if (array[i][j] = = target) {

flag = 1;

}

}

}

if (flag = = 1) {

return true;

} else{

return false;

}

}

public static void Main (string[] args) {

int[][] array = new int[][] {{1}, {2, 3}, {4, 5, 6}};

Solution s = new solution ();

System.out.print (S.find (array, 7));

}

}

b) best solution, starting from the lower left corner, when the key value is less than the value in the array, move up when the key value is greater than the value in the array to the right.

public class Solution {

public boolean find (int[][] array, int key) {

int len = array.length-1;

int i = 0;

while ((Len > 0) && (i < array[0].length)) {

if (Key < Array[len][i]) {

len--;

}else if (key > Array[len][i]) {

i++;

}else{

return true;

}

}

return false;

}

public static void Main (string[] args) {

int[][] array = new int[][]{{1,2,3},{4,5,6},{7,8,9}};

Solution solution = new solution ();

System.out.println (Solution.find (array, 5));

}

}

c) The most concise code

public boolean Find (int [] [] array,int target) {

For (int[] is:array) {

for (int i = 0; i < is.length; i++) {

if (is[i] = = target) {

return true;

}

}

}

return false;

}

2. Implement a function that replaces a space in a string with "%20". For example, when the string is we are Happy. The string after substitution is we%20are%20happy.

A) My implementation, first convert the StringBuffer into a string string, and then traverse the string, if there are spaces, replace the "%20"

public class Solution {

Public String replacespace (StringBuffer str) {

String[] s = str.tostring (). Split ("");

String string = "";

for (int i = 0; i < s.length; i++) {

if (S[i].equals ("")) {

S[i] = "%20";

}

String = string + S[i];

}

return string;

}

public static void Main (string[] args) {

Solution solution = new solution ();

StringBuffer str = new StringBuffer ("We are Happy");

SYSTEM.OUT.PRINTLN (Solution.replacespace (str));

}

}

3. Enter a list to print the values of each node of the list from the end of the head.

Input Description:

Enter the table header for the linked list

Output Description:

Output as a "new linked list" header that needs to be printed

Import java.util.ArrayList;

public class Solution {

Public arraylist<integer> Printlistfromtailtohead (ListNode listnode) {

Arraylist<integer> a = new arraylist<integer> ();

ListNode node = ListNode;

while (node! = null) {

A.add (New Integer (Node.val));

node = Node.next;

}

Integer b;

for (int i = 0; i < a.size ()/2; i++) {//Use the FOR Loop statement to change the value of the list back and forth//

B=a.get (i);

A.set (i, A.get (A.size ()-i-1));

A.set (A.size ()-i-1,b);

}

return A;

}

}

4. To enter the results of the pre-order traversal and the middle sequence traversal of a binary tree, rebuild the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.

public class Solution {

Root Node class

Class TreeNode {

int Val;

TreeNode left;

TreeNode right;

TreeNode (int x) {

val = x;

}

}

Public TreeNode Reconstructbinarytree (int[] Pre, int[] in) {

The root node is the first element in a pre-order traversal

TreeNode root = new TreeNode (pre[0]);

When there is only one data

int len = pre.length;

if (len = = 1) {

Root.left = null;

Root.right = null;

}

Find the location of the root node in the middle sequence traversal

int rootval = Root.val;

int i;

for (i = 0; i < in.length; i++) {

if (Rootval = = In[i]) {

Break

}

}

Create Zuozi

if (i > 0) {

int[] Leftpre = new Int[i];

int[] LeftIn = new Int[i];

for (int j = 0; J < i; J + +) {

LEFTPRE[J] = pre[j+1];

LEFTIN[J] = In[j];

}

Root.left = Reconstructbinarytree (Leftpre, LeftIn);

} else {

Root.left = null;

}

Create right subtree

if (len-i-1 > 0) {

int[] Rightpre = new int[len-i-1];

int[] RightIn = new int[len-i-1];

for (int j = i + 1; j < Len; J + +) {

Rightpre[j-i-1] = pre[j];

Rightin[j-i-1] = in[j];

}

Root.right = Reconstructbinarytree (Rightpre, RightIn);

} else {

Root.right = null;

}

return root;

}

}

5. Implement a queue with two stacks to complete the push and pop operations of the queue. The elements in the queue are of type int.

public class Solution {

stack<integer> stack1 = new stack<integer> ();

stack<integer> Stack2 = new stack<integer> ();

public void push (int node) {

Stack1.push (node);//Get in the queue and go straight to stack1.

}

public int pop () {

int val= 0;

Put the contents of the Stack1 in reverse into the Stack2

while (!stack1.empty ()) {

Stack2.push (Stack1.pop ());

}

if (!stack2.empty ()) {

val = Stack2.pop ();

} else {

System.out.println ("Queue is empty, return default value 0");

}

After the implementation of the queue, but also to the contents of the Stack2 back to Stack1

while (!stack2.empty ()) {

Stack1.push (Stack2.pop ());

}

return Val;

}

public static void Main (string[] args) {

Solution s = new solution ();

S.push (1);

S.push (2);

S.push (4);

System.out.println (S.pop ());

System.out.println (S.pop ());

System.out.println (S.pop ());

System.out.println (S.pop ());

}

}

Java algorithm applet (1)

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.