注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
1:counting stars
import java.util.Arrays;
public class StringProcess{
public static String[] starCount(String[] args){
int len=args.length;
int count=1;
int index=0;
String[] returnValue=new String[len];
for(int i=0;i<len;i++){
if(args[i]!=null){
if(args[i].equals("star"))
count=count*(-1);
else
returnValue[index++]=args[i];
}
else{
returnValue[index++]=null;
}
}
// System.out.println("count"+count);
if(count==1)
return args;
else{
// System.out.println("do return");
// printResult(returnValue);
return Arrays.copyOf(returnValue,index);
}
}
public static void printResult(String[] input){
int len=input.length;
for(int i=0;i<len;i++){
if(input[i]!=null)
System.out.println(input[i]);
else
System.out.println("null");
}
}
public static void main(String [] args){
String[] input1={"star","star1","sta2",null,null,"star","star"};
String[] input2={"star","star","star","star","star","star"};
input2=starCount(input1);
printResult(input2);
}
}
2:CNode
public class CNode{
char head;
CNode next;
public CNode(char head, CNode next){
this.head=head;
this.next=next;
}
}
3,4:
public class LinkedString{
public static void printLinkedList(CNode head){
CNode test;
test=head.next;
while(test.next!=null){
System.out.println(test.head);
test=test.next;//always forget to include this
}
System.out.println(test.head);
}
public static CNode makeHugString(String s){
String input=s;
CNode temp=new CNode('*',null);
CNode head=temp;
for(int i=0;i<input.length();i++){
CNode t=new CNode(input.charAt(i),null);
temp.next=t;
temp=t;
}
return head;
}
public static void swapSpace(CNode in){//use two pointers
CNode pre, cur;
cur=in.next;
pre=in;
while(cur!=null){
if(cur.head==' '){
CNode temp=new CNode('C',cur.next);
pre.next=temp;
cur=cur.next;
}else{
cur=cur.next;
pre=pre.next;
}
}
printLinkedList(in);
}
public static void main(String args[]){
CNode test=makeHugString("ma ke Hug String");
printLinkedList(test);
swapSpace(test);
}
}
//要求换成61B,感觉和前面的要求不符合,但是还是贴上来了。。。。这门课跟得灰常慢,proj0到现在都没有调试完成,希望最后还是能够全部做完。论坛里没有看到discussion5的帖子,自己补一个。
|