Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80436
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: Java

2015-01-17 11:16:55

题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
解决方案:

点击(此处)折叠或打开

  1. public class Solution{
  2.     public int lengthOfLongestSubstring(String s) {
  3.         LinkedList<Character> mylist = new LinkedList<>();
  4.         char[] myarr = s.toCharArray();
  5.         int len = myarr.length;
  6.         int result = 0;
  7.         for(int i = 0; i < len; i++){
  8.             if(mylist.contains(myarr[i])){
  9.                 int tempresult = mylist.size();
  10.                 if(result < tempresult)
  11.                     result = tempresult;
  12.                 while(mylist.removeFirst() != myarr[i]);
  13.             }
  14.             mylist.add(myarr[i]);
  15.         }
  16.         if(result < mylist.size()){
  17.          result = mylist.size();
  18.         }
  19.         return result;
  20.     }
  21. }

           
阅读(665) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~