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 ...
随机推荐
- Log4Net web.config配置
1 .[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)] 写 ...
- linux就该这么学,第十天了
今天老师主要让要考试的提前预习课程了,提前预习, 今天讲了,防火墙,iptable.firewall-config,firewall-cmd 防火墙和网卡的配置方法,四种,1配置文件方法,主要开启 ...
- 动态iframe 分享
function createIframe() { var i = document.createElement("iframe"); i.src = "/RSK/Rsk ...
- python爬虫笔记
1.抓取网页并保存到txt中.解决控制台乱码问题 #_*_coding:utf-8_*_ import urllib2 response = urllib2.urlopen('http://hws.m ...
- linux_文件夹实现挂载(必须在同一网段)
将外部想要挂载传输的目录开启共享文件夹 首先进行安装 yum install nfs-utils rpcbind yum install nfs* 建立想要挂载的目录 查看可以执行挂载的目录有哪些 s ...
- Spring的介绍与搭建
一.Spring的介绍 二.Spring的搭建 (1)导包 (2)创建一个对象 (3)书写配置注册对象到容器 (4)代码测试
- 说说Runnable与Callable
Callable接口: public interface Callable<V> { V call() throws Exception; } Runnable接口: public int ...
- android 动画学习
android动画基础简介及使用方法:http://www.cnblogs.com/ldq2016/p/5407061.html
- 23.HashMap
HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...
- yii2.0 点击验证码图片不刷新
修改vender\yiisoft\yii2\captcha\CaptchaAction下的run方法 public function run(){ if (Yii::$app->request- ...