Home > Back-end >  Rookie interview
Rookie interview

Time:12-07

Merge list

//two lists known head1 and head2 each from small to large, orderly, please merge them into a linked list is still growing up orderly
The class Node {
The int data
The Node next
}
Node Merge (Node head1, Node head2) {
//TODO
}


Multithreaded


1, eat orangesA total of nine oranges, have 3 children, kid A every time take 2 oranges, kid B each with three oranges, kid C each take 1 orange, children 10 s eat A orange, after eating to continue to take,
Children after every take orange before and take the orange, the quantity of orange number off, if every time the rest of the orange is not enough children to take the number of children stop take oranges, shout a loud "no" and exit the game, took the
Please the above process is expressed by the Java multi-thread program,

2, running
Three children attend hundred meters race, A kid ran 5 meters per second, children run 4 meters per second, B C children run 3 meters per second, after you ready to shout A cry, "I'm ready," and then start together, after hearing the pistol and shouted at the beginning of the run A cry, "I started running," when A child ran to the finish line, shout A loud "I get to the end", record run at this time how long, other children stop running, and give its run how many meters, "children ran yy m x", for example,
Please the above process is expressed by the Java program,

3, analyze the string

Write a string of analysis function, a string composed of letters and Numbers, for example aBc12D9
1, the number of characters and Numbers in the output string,
2, connected to the digital cannot be divided into two, namely 12 as a digital statistics, the number of characters and Numbers in the output string,
3, character case not distinguish, statistics the number of characters and occurrences,
4, the statistical characters and Numbers, in the most times
According to the requirements of the above, write four function


4, delete

Requirements: to delete a string of characters, most times if multiple occurrences, characters are deleted, required character sequence is changeless,

Public class RemoveMostCharTest {

@ Test
Public void testRemoveMostChar () {
String source="ABCCDEER";
The String result="ABDR";
Assert. AssertEquals (result, getRightString (source));
}

/* *
* delete
* delete a string of characters, most times if multiple occurrences, characters are deleted, required character sequence is changeless,
* @ param sorource
* @ return
*/
Private String getRightString (String source) {
return null;
}
}

CodePudding user response:

Merge list
 
Public static Node Merge (Node head1, Node head2) {
The Node node1.
The Node Node=new Node ();
Node1=node;
While (head1!=null& & Head2!=null) {
If (head1. Data> Head2. Data) {
Node. Next=head2;
The node=node. The next;
Head2=head2. Next;
} else {
Node. Next=head1;
The node=node. The next;
Head1=head1. Next;
}
}
If (head1==null)
Node. Next=head2;
If (head2==null)
Node. Next=head1;
Return node1. Next;
}
@ Test
Public void Testnode () {
The Node Node=new Node (the new Node (the new Node (5), (4), 3);
The Node node1=new Node (the new Node (the new Node (7), 5), 3);
Node merge=merge (Node, node1);
While (merge!=null) {
System. The out. Println (merge. Data);
The merge=merge. Next;
}
}



The class Node {
int data;
The Node next;
Public Node () {}
Public Node (int id) {this.data=https://bbs.csdn.net/topics/id; }
Public Node (the Node n, int id) {this. Next=n; this.data=https://bbs.csdn.net/topics/id; }
}

CodePudding user response:


Eat oranges I use unit test thread a dormant dropped out just know can't use this ah,,,
Let's use the main
 
Public static void main (String [] args) {
Data, Data=https://bbs.csdn.net/topics/new Data ();
Data. The setCount (9);
for (int i=1; i<4. I++) {
Int finalI=I;
New Thread (() - & gt; {
While (true) {
If (data. GetCount () & lt; FinalI) {
System. The out. Println (" don't take the ");
return;
}
Synchronized (data) {
If (data. GetCount () & lt; FinalI) {
System. The out. Println (" don't take the ");
return;
}
Data. SetCount (data. GetCount () - finalI);
System. The out. Println (" got "+ finalI +" orange ");
System. The out. Println (" and "+ data. GetCount ());
}

Try {
TimeUnit. SECONDS. Sleep (finalI * 1);
System. The out. Println (" eat "+ finalI +" orange ");
} the catch (Exception e) {
e.printStackTrace();
}
}

}). The start ();
}
}
The class Data {
Private int count;

Public int getCount () {
Return the count.
}

Public void setCount (int count) {
This. Count=count;
}
}

CodePudding user response:

Running I feel there are still problems just change the place of output statements,,,,
 
Public static volatile Boolean flag=true;
Public static void main (String [] args) {
For (int I=3; i<6; I++) {
Int finalI=I;
New Thread (() - & gt; {
Data, Data=https://bbs.csdn.net/topics/new Data ();
While (data. GetCount () & lt; 100 & amp; & Nospringtest. Flag) {
Synchronized (Thread. Class) {
if (! Nospringtest. Flag)
break;
Data. SetCount (data. GetCount () + finalI);
System. The out. Println (Thread. CurrentThread (). The getName () + "ran" + data. The getCount () + "and" + (100 - data. GetCount ()) + "m");
If (data. GetCount () & gt; {
=100)System. Out. Println (Thread. CurrentThread (). The getName () + "run");
Nospringtest. Flag=false;
return;
}
}
Try {
TimeUnit. SECONDS. Sleep (1);
} the catch (Exception e) {
e.printStackTrace();
}
}
System. The out. Println (Thread. CurrentThread (). The getName () + "ran" + data. The getCount () + "and" + (100 - data. GetCount ()) + "m");
}). The start ();
}
}
  • Related