zoukankan      html  css  js  c++  java
  • Leecode-无重复字符的最长子串

    import java.util.LinkedList;
    import java.util.TreeSet;

    class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    }
    public class Solution {

    public Solution() {}
    // TODO Auto-generated constructor stub




    public static int lengthOfLongestSubstring(String s) {

    /*伪代码
    * 初始化一个链表集合作为寻找最大不重复子串的临时存储位置
    * 初始化最大长度count为0
    * 遍历整个字符串
    * 如果该字符在链表中不重复,加入集合
    * 如果该字符在链表中已存在,首先比较当前链表长度与原coun最大值记录到count;然后删除链表链表中从开头到该重复字符(包括该字符)这段字符串;最后然后将该字符加入链表
    * 遍历完成后,再比较链表长度与count,取大值为count
    * 选择linkedList原因:对删除操作友好;保持插入顺序
    */



    int re_count=0,curr_count=0;
    LinkedList<Character> link=new LinkedList<>();
    for(int i=0;i<s.length();i++){
    char curr_ch = s.charAt(i);
    if(link.contains(curr_ch)){

    curr_count = link.size();
    re_count = re_count>curr_count?re_count:curr_count;//找大数
    link.subList(0, link.indexOf(curr_ch)+1).clear();//删除包含重复元素及之前的。
    link.add(curr_ch);//添加该元素
    }else{
    link.add(curr_ch);
    }
    }
    curr_count = link.size();
    re_count = re_count>curr_count?re_count:curr_count;//找大数_
    return re_count;
    }


    public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println(lengthOfLongestSubstring("dvdf"));

    }

    }

  • 相关阅读:
    Objective-C写出Json文件(可作配置文件)
    关于快速排序的部分内容
    关于折半查找排序的部分内容
    异步下载网络图片
    pytest(5):setup/teardown框架结构
    pytest(4):用例参数化
    pytest(3):pytest运行参数介绍
    pytest(2):使用pycharm运行pytest
    pytest(1):pytest的安装与使用
    pytest文档4-Allure报告清除上一次数据
  • 原文地址:https://www.cnblogs.com/yexia/p/11028593.html
Copyright © 2011-2022 走看看