查看: 1423| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

CS61B 2015 SPRING discussion5

全局:
公开课

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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的帖子,自己补一个。

上一篇:课程加分帖: COMP107x Introduction to Mobile Application Development using And...
下一篇:CS61B_Week_05_sky420小组
推荐
HNAKXR 2016-2-9 14:55:05 | 只看该作者
全局:

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表