首页
社区
课程
招聘
一个编程题求助
发表于: 2016-2-29 10:29 3932

一个编程题求助

2016-2-29 10:29
3932
题目是这样的:设计一个class,名叫MutableString,实现以下方法:charAt(int i),substring(int start, int end),setCharAt(int i, char c)。要求空间复杂度O(1)。
难点在空间复杂度上,不知道如何实现,麻烦知道的兄弟们帮个忙,谢谢了。

------在线等待------

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 449
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
java没有指针,可以用String类中的构造函数实现。
String(byte[] bytes,int offset,int length);通过byte数组,从offset开始,总共length长的字节构造字符串对象。
String(byte[] char,int offset,int length);通过char数组,从offset开始,总共length长的字节构造字符串对象
2016-2-29 10:57
0
雪    币: 47
活跃值: (25)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
3
[QUOTE=qqsunqiang;1417445]java没有指针,可以用String类中的构造函数实现。
String(byte[] bytes,int offset,int length);通过byte数组,从offset开始,总共length长的字节构造字符串对象。
String(byte[] char,int offset,int leng...[/QUOTE]

请问可否具体一点?帮忙写一个简单的demo?麻烦了,对java这些东西不熟悉,之前都用C++的。
2016-2-29 11:13
0
雪    币: 449
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
public class MutableString {
   private String str;
        public MutableString(String str) {
          this.str=str;
        }
        char charAt(int i)
        {
                byte[] ff=str.getBytes();
                return (char)ff[i];
               
               
        }
        String substring(int start, int end)
        {
                return new String(str.getBytes(),start-1,end-1);
               
        }
        String setCharAt(int i,char c)
        {  
                byte[] ff=str.getBytes();
                ff[i-1]=(byte) c;
                //str=new String(ff);
                return new String(ff);
               
        }
       
}

测试代码:
public class ggg {

        /**
         * @param args
         */
        public static void main(String[] args) {
         
                String ddd="abcdefghi";
                MutableString ff=new MutableString(ddd);
                System.out.print(ff.substring(2, 3)+"     ");
                System.out.print(ff.charAt(3));
                System.out.print("     "+ff.setCharAt(3,'l'));
               
      
        }

}
测试结果:
bc     d     abldefghi
2016-2-29 12:24
0
雪    币: 47
活跃值: (25)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
[QUOTE=qqsunqiang;1417458]public class MutableString {
   private String str;
        public MutableString(String str) {
          this.str=str;
        }
        char charAt(int i)
        {
                byte[] ff=str.get...[/QUOTE]

感谢你的回复,谢谢了。但是不知道你在子程序中byte[] ff=str.getBytes()之后,算法空间复杂度还算不算是O(1)了?这块不是太明白。
2016-2-29 14:53
0
雪    币: 449
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
如果实现中没有循环就是 O(1)
2016-2-29 17:43
0
雪    币: 47
活跃值: (25)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
7
怎么把看雪币给你呢?
2016-2-29 22:49
0
雪    币: 449
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
标题处不是有一个问题已经解决,点击那个。
2016-3-1 08:08
0
游客
登录 | 注册 方可回帖
返回
//