• 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度


    题目:

       给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

    题目解析:

       有一个字符串,长度不定, 要找出不重复字符串的长度,我们可以这么假设,先找到第一个下标,然后从后面拿到元素的下标对比,如果不等,就取到下一个元素的下标。如果相等,就取前一元素的下标。最后截取字符串的长度 计算。

        python代码实现:

        

    def finstr(findstr:str):
        for i in range(len(findstr)):
            for j in range(len(findstr)):
                data = findstr[0:i]
                if str(findstr[j])  in str(data):
                    m=findstr[0:i]
                else:
                    m=findstr[0:j+1]
        return len(m)
      测试代码:
      
    from sixexapmle import finstr
    import  unittest
    class Test(unittest.TestCase):
        def setUp(self) -> None:pass
        def tearDown(self) -> None:pass
        def testone(self):
            reslut=finstr("0")
            self.assertEqual(1,reslut)
        def testtewo(self):
            reslut=finstr("01")
            self.assertEqual(2,reslut)
        def testthree(self):
            reslut=finstr("011")
            self.assertEqual(2,reslut)
    if __name__=="__main__":
        unittest.main()
        测试结果:

        代码覆盖率:

    其实实现起来不难,

    我们来看下java版本的实现:

    代码:

      

    public class Five {
        public  Integer maxstr(String max){
            String m="";
            for (int a = 0; a < max.length(); a++) {
                String n=max.substring(0,a);
                for (int b = 0; b < max.length(); b++) {
                    if (n.indexOf(max.substring(b))!=-1 ){
                        m=max.substring(0,b);
                    }else {
                        m=max.substring(0,a+1);
                    }
                }
            }
            return m.length();
        }
    }
    测试代码:
    public class FiveTest {
    
        @Test
        public void testMaxstr() {
                Five five=new Five();
                assertEquals(five.maxstr("12").intValue(),2);
        }
        @Test
        public void testMaxstr1() {
            Five five=new Five();
            assertEquals(five.maxstr("1").intValue(),1);
        }
        @Test
        public void testMaxstr2() {
            Five five=new Five();
            assertEquals(five.maxstr("121").intValue(),2);
        }
    }
      测试结果:

    ​覆盖率:

    自此,上面完成的就是本题。

     注:这是提供一种思路方案,实现方式千万种。​

  • 相关阅读:
    Node.js Express框架
    Node.js Web模块
    Node.js 工具模块
    Node.js GET/POST请求
    Node.js 文件系统
    Node.js 常用工具
    【day03】Xhtml
    【day02】Xhtml
    【紫书】【重要】Not so Mobile UVA
    【紫书】Tree UVA
  • 原文地址:https://www.cnblogs.com/leiziv5/p/11770571.html
Copyright © 2020-2023  润新知