2014年5月14日星期三

A JAVA pen questions

had to interview , do a set of questions , including a question as follows :

have an object StringBuilder sentence = new StringBuilder ("This is an apple");
requires only char c, int p1, int p2 these three variables , and is no longer open up the case more memory , the output apple an is This

I will not do , so do not surface, there no one to help do it ah ? ?
------ Solution ---------------------------------------- ----
casually wrote a request should be able to reach , but the premise is
1, there is a space where the original sentence , the output where there is space , and the original sentence several output on several
2, each word length is short ( short refers to the char range , there will be tens of thousands of words in length )


StringBuilder sentence = new StringBuilder("This is an apple");
char c;
int p1, p2;

p1 = sentence.length();

while (p1 > 0) {
p1--;
p2 = p1;

while (p1 >= 0 && sentence.charAt(p1) != ' ') {
p1--;
}

c = (char) (p2 - p1);
p2 = p1 + 1;

while (p1 + c >= p2) {
System.out.print(sentence.charAt(p2));
p2++;
}

if (p1 >= 0) {
System.out.print(' ');
}
}


------ Solution ------------------------------------ --------
public class Test {
public static void main(String[] args) {
StringBuilder sentence = new StringBuilder("This is an apple");

int p1 = sentence.length() - 1;
int p2 = 1;
char c;

while (p1 >= 0) {
c = sentence.charAt(p1);
while (c != ' ' && p1 > 0) {
c = sentence.charAt(--p1);
}
System.out.print(c);
c = sentence.charAt(++p1);

while (c != ' ' && p1 < sentence.length()) {
c = sentence.charAt(p1++);
System.out.print(c);
++p2;
}

p1 = p1 - p2 - 1;
p2 = 1;
}
}
}

------ Solution -------------------------- ------------------
thank a recursive

public class Test001 {
static char c = ' ';
static StringBuilder sentence = new StringBuilder("This is an apple");
static int p1 = sentence.length();
static int p2 = 0;

public static void main(String[] args) {
do{
p1 = getIndex(sentence ,p1,p2,c);
} while( p1 != -10 );

}

public static int getIndex(StringBuilder sentence ,int p1,int p2,char c){
if( sentence.substring(0, p1).lastIndexOf(c) > 0 ){
p2 = sentence.substring(0, p1).lastIndexOf(c);
System.out.print(sentence.substring(0, p1).substring(p2+1)+c);
}else{
System.out.print(sentence.substring(0, p1));
p2 = -10;
}
return p2;
}
}

------ Solution ------------------------------------- -------
write wrong.
 char c;int p1;int p2;
       System.out.println(sentence.toString().split("\\s+")[3]+" "+sentence.toString().split("\\s")[2]+" "
        +  sentence.toString().split("\\s+")[1]+" "+sentence.toString().split("\\s")[0]);

------ Solution ------------------------------------- -------
char c=' ';
int p1=0,p2;
StringBuilder sentence = new StringBuilder("This is an apple");
p2=sentence.toString().length();
while((p1=sentence.toString().lastIndexOf(" ",p2-1))!=-1){
System.out.print(sentence.toString().substring(p1+1,p2)+c);
p2=sentence.toString().lastIndexOf(" ",p1);
if(sentence.toString().lastIndexOf(" ",p2-1)==-1){
System.out.print(sentence.toString().substring(0,p2));
}
}

------ Solution --------------------------- -----------------
I'll stick with no technical content

p2 = sentence.length();
while(true){
p1 = 0;
for(int j=0;j<p2;j++){
c = sentence.charAt(j);
if(c ==' '){
p1 = j;
}
}
if(p1!=0){
System.out.print(sentence.substring(p1+1, p2)+" ");
}else{
System.out.print(sentence.substring(0, p2));
break;
}
p2 = p1;
}

------ Solution ------------------------------------- -------
first reverse search space, and then find the positive output because it is only allowed to use three variables thing so I used a point opportunistic way c = (char) p1; Haha , anyway, the effect is achieved.

StringBuilder sentence = new StringBuilder("This is an apple");

char c;
int p1, p2;

p1 = p2 = sentence.length() - 1;

while (p1 >= 0) {
c = sentence.charAt(p1);
if (c == ' ' || p1 == 0) {

if (p1 == 0) {
System.out.print(sentence.charAt(p1));
}

c = (char) p1;
while (p1 < p2) {
System.out.print(sentence.charAt(p1 + 1));
p1++;
}

p1 = c;
p2 = p1 - 1;

if (p1 > 0) {
System.out.print(sentence.charAt(p1));
}

}
p1--;
}

------ Solution ------------------------------------- -------
If StringBuilder.substring (int start, int end) method is not opened in the new memory , then
on the line as long as two int .

        StringBuilder sentence = new StringBuilder("This is an apple");
        int p1, p2;
        p1 = p2 = sentence.length();
        while (p1 >= 0) {
            if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
                System.out.print(sentence.substring(p1, p2));
                if (p1 >= 1)
                    System.out.print(' ');
                p2 = p1 - 1;
            }
            p1--;
        }
    }
Otherwise


        StringBuilder sentence = new StringBuilder("This is an apple");
        int p1, p2;
        char c;
        p1 = p2 = sentence.length();
        while (p1 >= 0) {
            if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
                c = (char) p1;
                while (p1 < p2) {
                    System.out.print(sentence.charAt(p1++));
                }
                p1 = c;
                if (p1 >= 1)
                    System.out.print(' ');
                p2 = p1 - 1;
            }
            p1--;
        }
    

------ Solution ------------------------------------- -------

found the third floor to write their own code, there is a small bug, so re- send a
StringBuffer str = new StringBuffer("This is an apple");

int p1 = str.length() - 1;
int p2;
char c;

while (p1 >= 0) {
c = str.charAt(p1);

while (c != ' ' && p1 > 0) {
c = str.charAt(--p1);
}

if (c != ' ') {
System.out.print(' ');
}

System.out.print(c);

p2 = p1;
c = str.charAt(++p2);

while (c != ' ' && p2 < str.length()) {
c = str.charAt(p2++);

if (c != ' ') {
System.out.print(c);
}
}

p1--;
}

------ For reference only ----------------------------------- ----

want to use recursion to forward , unconsciously or written in a while loop
------ For reference in case there is only ---------------------------------------
memory space limitations , the recursion is inappropriate , 2L, 3L direct print output method should be right.

Also, the topic is likely to want the contents inside the sentence is converted from This an apple to apple an is This,
If this is the case , the replacement method can be used twice , the first string reversed, the result is
elppa na si sihT
then be reversed for each word , the result is
apple na is This
------ For reference only --------------------------- ------------
agree 3L wording !
------ For reference only -------------------------------------- -
learn
------ For reference only ---------------------------------------
accordance empty Geqie look, cut a String array , reverse output array ok. . .

------ For reference only ---------------------------------- -----
System.out.println ("apple an is This");
------ For reference only ---------------------------------------
substring certainly can not be used
The problem there is if there is C -based , considered entry title
------ For reference only --------------------- ------------------
substring returns a String, is a constant, is bound to consume memory
------ For reference only - --------------------------------------
we are very good too , had not difficult, but why did not I think . .
------ For reference only -------------------------------------- -
Hey , by the way stole a bit
------ For reference only --------------------------- ------------


The laughing urine

没有评论:

发表评论