📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 1793| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 一道medium题,我的代码无法处理带“\”的字符串

全局:
5小米
本帖最后由 silence_shadow 于 2016-6-9 21:19 编辑

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

我的JAVA代码:

  1. public class Solution {
  2.     public int lengthOfLongestSubstring(String s) {
  3.         if(s.length() == 0)
  4.             return s.length();
  5.         int max = 1;
  6.         for(int i = 0; (i+1)<s.length(); i++){
  7.             int j = i+1;
  8.             int count = 1;
  9.            
  10.             while(j < s.length() && !s.substring(i,i+1).equals(s.substring(j,j+1)) ){
  11.                 boolean dup = false;
  12.                 int tempI = i+1;
  13.                 while(tempI < j){
  14.                     if(s.substring(tempI, tempI+1).equals(s.substring(j,j+1))){
  15.                         dup = true;
  16.                         break;
  17.                     }
  18.                     tempI ++;
  19.                 }
  20.                 if(dup == true)
  21.                     break;
  22.                 count ++;
  23.                 j ++;
  24.             }
  25.             if(count > max)
  26.                 max = count;
  27.         }
  28.         return max;
  29.     }
  30. }
复制代码
正常字符串或者带?,.的都没问题,但无法处理像“\qwertq”这样的带了“\”的字符。。过不了testcase。。

求大神解答~~谢谢~~~



上一篇:求问一道substring题目(可能含有星号)
下一篇:请问一道Google onsite题
🔗
chitose 2016-6-10 14:05:59 | 只看该作者
全局:
本帖最后由 hakase 于 2016-6-10 14:43 编辑

java里面斜杠需要转译,凡是“\”出现必须“\\”.和C语言一样。换言之,想要测试"\qwertq"的话,需要输入"\\qwertq" 作为测试用例。
回复

使用道具 举报

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

本版积分规则

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