leetcode — median-of-two-sorted-arrays
import java.util.HashSet;
import java.util.Set;
/**
* Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
*
* Created by lverpeng on 2017/6/26.
*
* 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.
*
*/
public class LongestSubstring {
/**
* 两个指针:一个指向当前字符,一个指向当前子串开始的位置
* 将指向当前子串的指针依次向后移动,判断当前哈希表(用字符作为key,value不重要,可以直接用set)中是否存在当前字符
* 如果已经存在,说明找到一个子串
* 将指向当前子串的指针向后移动一个位置,作为新的子串起始位置
* 计算刚刚结束子串的长度,和之前长度作比较,选出较大的一个
* 如果不存在,更新当前子串的长度,加1
* 将当前字符加入hash表,将当前指针向后移动,重复上面的操作
*
*
*
* @param str
* @return
*/
public int searchLongestSubstring (String str) {
int lastIndext = 0;
int maxLen = 0;
Set<Character> charSet = new HashSet<Character>();
for (int i = 0; i < str.length(); i++) {
if (charSet.contains(str.charAt(i))) {
int newLen = i - lastIndext;
maxLen = Math.max(newLen, maxLen);
lastIndext ++;
} else {
maxLen ++;
}
charSet.add(str.charAt(i));
}
return maxLen;
}
public static void main(String[] args) {
LongestSubstring longestSubstring = new LongestSubstring();
System.out.println(longestSubstring.searchLongestSubstring("abcabcbb"));
}
}
leetcode — median-of-two-sorted-arrays的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- Python开发——函数【基础】
函数的定义 以下规则 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数. 函数的第一行语句可以选择性地使用文档字符 ...
- mybatis的Sql语句打印
我们在使用mybatis的时候,有时候,希望可以在eclipse的控制台下打印出来sql语句,但是有时候却不希望相关的语句打印.这个时候,需要我们进行一些配置. 在mybatis中,他通过调用一些接 ...
- zeromq学习记录(七)订阅发布消息封装
之前也有提到 使用订阅发布 pub sub模式必须要显示定义ZMQ_SUBSCRIBE 只有以此模式定义的过滤字节开头的消息才会被订阅者收到 如果想收到所有信息 可定义subscriber.setso ...
- java多线程系列14 设计模式 Master-Worker
Master-Worker模式是常用的并行设计模式,可以将大任务划分为小任务,是一种分而治之的设计理念. 系统由两个角色组成,Master和Worker,Master负责接收和分配任务,Worker负 ...
- 初级PM要做什么
首先让我们看下这张图,产品经理进入公司后将要面临着许多工作 或许你有疑问,如果是产品助理的话,上面这么多工作都要去做吗? 其实不然,初级产品经理由于工作经历有限,对行业的研究以及对市场的把控是有视野限 ...
- Note | 学术论文写作方法和技巧
目录 1. 论文发表流程 2. 确定科研方向 3. 思考问题和解决问题 4. 审稿 5. 论文写作 5.1. 标题 5.2. 摘要 5.3.介绍 5.4. 相关工作 5.5. 段落 5.6. 方法 5 ...
- bzoj3637(lct)
又一次把lct写炸了,硬着头皮终于改对了 #include<iostream> #include<cstring> #include<cstdio> #includ ...
- 轮播图js编写
//面向对象 function Left() { this.index = 0; this.lefthover = $('#left-content'); this.listenhover(); th ...
- ubuntu下chrome浏览器安装flash插件(pepperflashplugin-nonfree)
安装前说明: ubuntu的Google 已经不能使用Adobe Flash了,需要用PepperFlashPlayer来替代 Adobe Flash才行. 安装步骤: 1.安装pepperflash ...
- Visual Stuido快捷键
转自:http://www.cnblogs.com/TankXiao/p/3164995.html 整理了一些VS的快捷键 格式化整个文档:(Ctrl + K, Ctrl + D)智能感知:(Ctrl ...