<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.3">Jekyll</generator><link href="https://yukiiris.github.io//feed.xml" rel="self" type="application/atom+xml" /><link href="https://yukiiris.github.io//" rel="alternate" type="text/html" /><updated>2018-07-08T13:45:15+00:00</updated><id>https://yukiiris.github.io//</id><title type="html">yukiiris</title><subtitle>少说话，多读书</subtitle><entry><title type="html">Leetcode 763.partition labels</title><link href="https://yukiiris.github.io//Leetcode-763.Partition-Labels/" rel="alternate" type="text/html" title="Leetcode 763.partition labels" /><published>2018-07-06T00:00:00+00:00</published><updated>2018-07-06T00:00:00+00:00</updated><id>https://yukiiris.github.io//Leetcode%20763.Partition%20Labels</id><content type="html" xml:base="https://yukiiris.github.io//Leetcode-763.Partition-Labels/">&lt;h2 id=&quot;leetcode-763partition-labels&quot;&gt;Leetcode 763.Partition Labels&lt;/h2&gt;

&lt;h4 id=&quot;题目&quot;&gt;题目：&lt;/h4&gt;

&lt;p&gt;A string &lt;code class=&quot;highlighter-rouge&quot;&gt;S&lt;/code&gt; of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: S = &quot;ababcbacadefegdehijhklij&quot;
Output: [9,7,8]
Explanation:
The partition is &quot;ababcbaca&quot;, &quot;defegde&quot;, &quot;hijhklij&quot;.
This is a partition so that each letter appears in at most one part.
A partition like &quot;ababcbacadefegde&quot;, &quot;hijhklij&quot; is incorrect, because it splits S into less parts.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;        这题采用贪心算法解，主要思路是先从后往前找到第0 个字符最后一次出现的索引计为i，再遍历0到i中的每个字符，找出最后一次出现的索引，若比i大则更新i，直到结束，这样0到i就是一个最小分区。接下来对i+1之后也做同样的处理。&lt;/p&gt;

&lt;p&gt;        f方法就对所给出的索引进行分区处理。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: S = &quot;ababcbacadefegdehijhklij&quot;
Output: [9,7,8]
Explanation:
The partition is &quot;ababcbaca&quot;, &quot;defegde&quot;, &quot;hijhklij&quot;.
This is a partition so that each letter appears in at most one part.
A partition like &quot;ababcbacadefegde&quot;, &quot;hijhklij&quot; is incorrect, because it splits S into less parts.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;         这题采用贪心算法解，主要思路是先从后往前找到第0 个字符最后一次出现的索引计为i，再遍历0到i中的每个字符，找出最后一次出现的索引，若比i大则更新i，直到结束，这样0到i就是一个最小分区，&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;partitionLabels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">Leetcode 763.Partition Labels</summary></entry><entry><title type="html">Leetcode 16.convert sorted list to binary search tree</title><link href="https://yukiiris.github.io//Leetcode-16.Convert-Sorted-List-to-Binary-Search-Tree/" rel="alternate" type="text/html" title="Leetcode 16.convert sorted list to binary search tree" /><published>2018-06-26T00:00:00+00:00</published><updated>2018-06-26T00:00:00+00:00</updated><id>https://yukiiris.github.io//Leetcode%2016.Convert%20Sorted%20List%20to%20Binary%20Search%20Tree</id><content type="html" xml:base="https://yukiiris.github.io//Leetcode-16.Convert-Sorted-List-to-Binary-Search-Tree/">&lt;h2 id=&quot;leetcode-16convert-sorted-list-to-binary-search-tree&quot;&gt;Leetcode 16.Convert Sorted List to Binary Search Tree&lt;/h2&gt;

&lt;h4 id=&quot;题目&quot;&gt;题目：&lt;/h4&gt;

&lt;p&gt;Given an array &lt;code class=&quot;highlighter-rouge&quot;&gt;nums&lt;/code&gt; of &lt;em&gt;n&lt;/em&gt; integers and an integer &lt;code class=&quot;highlighter-rouge&quot;&gt;target&lt;/code&gt;, find three integers in &lt;code class=&quot;highlighter-rouge&quot;&gt;nums&lt;/code&gt; such that the sum is closest to &lt;code class=&quot;highlighter-rouge&quot;&gt;target&lt;/code&gt;. Return the sum of the three integers. You may assume that each input would have exactly one solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;         主要思路是双指针，从第一个到倒数第三个元素循环，循环中再用两个指针指向i+1和最后这两个元素，将i， i+1,end这三个索引的数相加，若和大于目标值，右指针左移，反之左指针右移，直至两指针相遇。若和更接近目标值则更新答案。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;threeSumClosest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Arrays&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; 
                        &lt;span class=&quot;n&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">Leetcode 16.Convert Sorted List to Binary Search Tree</summary></entry><entry><title type="html">Leetcode 3.Longest Substring Without Repeating Characters</title><link href="https://yukiiris.github.io//Leetcode-3.Longest-Substring-Without-Repeating-Characters/" rel="alternate" type="text/html" title="Leetcode 3.Longest Substring Without Repeating Characters" /><published>2018-04-27T00:00:00+00:00</published><updated>2018-04-27T00:00:00+00:00</updated><id>https://yukiiris.github.io//Leetcode-3.Longest-Substring-Without-Repeating-Characters</id><content type="html" xml:base="https://yukiiris.github.io//Leetcode-3.Longest-Substring-Without-Repeating-Characters/">&lt;h2 id=&quot;leetcode-3longest-substring-without-repeating-characters&quot;&gt;Leetcode 3.Longest Substring Without Repeating Characters&lt;/h2&gt;

&lt;h4 id=&quot;题目&quot;&gt;题目：&lt;/h4&gt;

&lt;p&gt;Given a string, find the length of the &lt;strong&gt;longest substring&lt;/strong&gt; without repeating characters.&lt;/p&gt;

&lt;h4 id=&quot;示例&quot;&gt;示例：&lt;/h4&gt;

&lt;p&gt;Given &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;abcabcbb&quot;&lt;/code&gt;, the answer is &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;abc&quot;&lt;/code&gt;, which the length is 3.&lt;/p&gt;

&lt;p&gt;Given &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;bbbbb&quot;&lt;/code&gt;, the answer is &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;b&quot;&lt;/code&gt;, with the length of 1.&lt;/p&gt;

&lt;p&gt;Given &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;pwwkew&quot;&lt;/code&gt;, the answer is &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;wke&quot;&lt;/code&gt;, with the length of 3. Note that the answer must be a &lt;strong&gt;substring&lt;/strong&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;pwke&quot;&lt;/code&gt; is a &lt;em&gt;subsequence&lt;/em&gt; and not a substring.&lt;/p&gt;

&lt;p&gt;        这题的思路是用两个指针指向字符串，如果没有重复情况，右指针向右移动，左指针不动，如果出现重复，左指针向左移动直至没有出现重复。维护一个set（数组可以更简单）来确定是否出现重复。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lengthOfLongestSubstring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Character&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toCharArray&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="Leetcode" /></entry><entry><title type="html">MIT 6.830 Lab1 SimpleDB</title><link href="https://yukiiris.github.io//MIT-6.830-Lab1-SimpleDB/" rel="alternate" type="text/html" title="MIT 6.830 Lab1 SimpleDB" /><published>2018-04-08T00:00:00+00:00</published><updated>2018-04-08T00:00:00+00:00</updated><id>https://yukiiris.github.io//MIT%206.830%20Lab1%20SimpleDB</id><content type="html" xml:base="https://yukiiris.github.io//MIT-6.830-Lab1-SimpleDB/">&lt;h2 id=&quot;mit-6830-lab1-simpledb&quot;&gt;MIT 6.830 Lab1 SimpleDB&lt;/h2&gt;

&lt;h3 id=&quot;简介&quot;&gt;简介&lt;/h3&gt;

&lt;p&gt;         在这个系列的课程中，我们会完成一个简单的&lt;strong&gt;关系型数据库&lt;/strong&gt;，了解数据库的底层实现。&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;1the-database-class&quot;&gt;1.The Database Class&lt;/h3&gt;

&lt;p&gt;        Database类提供数据库中的全局变量的访问方法，尤其是访问catalog（数据库中所有表的列表）、bufferpool（缓冲池）和日志文件的方法。&lt;/p&gt;

&lt;h3 id=&quot;2-fiels-and-tuples&quot;&gt;2. Fiels and Tuples&lt;/h3&gt;

&lt;p&gt;        tuples是数据库里基础的组成部分，它们由一系列Field物体构成，每个field构成一个tuple。Field是不同数据类型的实现接口。tuple由底层访问创建。tuple也有一种类型叫做 &lt;em&gt;tuple descriptor&lt;/em&gt;，被TupleDesc类代表。这个对象由一系列Type构成，每个tuple里的field描述相应字段的类型。&lt;/p&gt;

&lt;h3 id=&quot;3catalog&quot;&gt;3.Catalog&lt;/h3&gt;

&lt;p&gt;        catalog由一个表格和模式的列表构成，支持添加新表、获取特定数据。与每个表关联的是一个TupleDesc对象，它允许操作者确定表中字段的类型和数量。&lt;/p&gt;

&lt;p&gt;         全局的catalog是一个单例，通过Database.getCatalog()获取。&lt;/p&gt;

&lt;h3 id=&quot;4bufferpool&quot;&gt;4.BufferPool&lt;/h3&gt;

&lt;p&gt;        缓冲池负责将最近读过的page缓存起来。所有从不同文件读写page的操作都要经过缓冲池，它包含固定数字的page。&lt;/p&gt;

&lt;h3 id=&quot;5heapfile-access-method&quot;&gt;5.HeapFile access method&lt;/h3&gt;

&lt;p&gt;         访问方法提供从硬盘读写数据的特定方法。一般的访问方法有heap files和b树。&lt;/p&gt;

&lt;p&gt;          HeapFIle对象被排列成一组page，每个都包含固定byte的tuples，包括header。在SimpleDB中，每个table都有一个HeapFile，每个HeapFile里的page被排列成一组槽，每个slot里有一个tuple。除了这些槽，每个page包含一个含有bitmap的header（每个比特来自一个tuple槽）。如果相应比特为1，则这个tuple可用，反之不行。page储存在缓冲池中，但是读写它们要通过HeapFile类。&lt;/p&gt;

&lt;p&gt;        SimpleDB存储的heap files或多或少有相同的格式，它们存储在内存中的磁盘。每个文件由磁盘上连续排列的页面数据组成。每个页面由一个或多个字节组成，代表header，接着是实际页面内容的页面大小字节。每个元组要求元组大小为其内容的8位，header的1位。因此，可以在单个页面中匹配的元组数是：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;_tuples per page_ = floor((_page size_ * 8) / (_tuple size_ * 8 + 1))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;        tuple的大小是是这个page中的tuple的byte的大小。这里的想法是，每个tuple要求在header中存储额外的位。我们计算在一个页面中的比特数（page大小乘8），并将该数量的tuple中的比特数（包括额外的header）得到的每个page的uple数量。&lt;/p&gt;

&lt;p&gt;        每个字节的低（最少）位表示文件前面的槽的状态。因此，第一个字节的最低位表示页面中的第一个槽是否在使用中。第一个字节的第二个最低位表示页面中的第二个槽是否在使用，等等。另外，注意最后一个字节的高阶位可能与实际在文件中的插槽不对应，因为插槽的数目可能不是8的倍数。也注意到所有的java虚拟机的大小。&lt;/p&gt;

&lt;h3 id=&quot;6operators&quot;&gt;6.Operators&lt;/h3&gt;

&lt;p&gt;         运算符负责查询计划的实际执行。它们实现关系代数的运算。在SimpleDB，运营商是基于迭代器的；每个经营者实施dbiterator接口。
        操作符通过将低级操作符传递到高级操作符的构造函数中，即通过将它们链接在一起，将它们连接到一个计划中。“计划”叶子的特殊访问方法运算符负责从磁盘读取数据（因此在它们下面没有任何操作符）。
在计划的顶部，与SimpleDB程序只需调用在根算子GETNEXT；这个操作员随后呼吁儿童GETNEXT，等等，直到这些叶者被称为。他们把元组从磁盘和山上的树（返回参数GETNEXT）；元组传播了这样的计划，直到他们在根或合并或被另一家运营商在计划输出。&lt;/p&gt;</content><author><name></name></author><category term="Database" /><summary type="html">整理下思路</summary></entry><entry><title type="html">Leetcode 53.maximum subarray</title><link href="https://yukiiris.github.io//LeetCode-53.Maximum-Subarray/" rel="alternate" type="text/html" title="Leetcode 53.maximum subarray" /><published>2018-03-29T00:00:00+00:00</published><updated>2018-03-29T00:00:00+00:00</updated><id>https://yukiiris.github.io//LeetCode%2053.Maximum%20Subarray</id><content type="html" xml:base="https://yukiiris.github.io//LeetCode-53.Maximum-Subarray/">&lt;h2 id=&quot;leetcode-53maximum-subarray&quot;&gt;LeetCode 53.Maximum Subarray&lt;/h2&gt;

&lt;h4 id=&quot;题目&quot;&gt;题目&lt;/h4&gt;

&lt;p&gt;Find the contiguous subarray within an array (containing at least one number) which has the largest sum.&lt;/p&gt;

&lt;p&gt;For example, given the array &lt;code class=&quot;highlighter-rouge&quot;&gt;[-2,1,-3,4,-1,2,1,-5,4]&lt;/code&gt;,
the contiguous subarray &lt;code class=&quot;highlighter-rouge&quot;&gt;[4,-1,2,1]&lt;/code&gt; has the largest sum = &lt;code class=&quot;highlighter-rouge&quot;&gt;6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;  &lt;/p&gt;</content><author><name></name></author><summary type="html">LeetCode 53.Maximum Subarray</summary></entry><entry><title type="html">Leetcode 109.Convert Sorted List to Binary Search Tree</title><link href="https://yukiiris.github.io//Leetcode-109.Convert-Sorted-List-to-Binary-Search-Tree/" rel="alternate" type="text/html" title="Leetcode 109.Convert Sorted List to Binary Search Tree" /><published>2018-03-28T00:00:00+00:00</published><updated>2018-03-28T00:00:00+00:00</updated><id>https://yukiiris.github.io//Leetcode%20109.Convert%20Sorted%20List%20to%20Binary%20Search%20Tree</id><content type="html" xml:base="https://yukiiris.github.io//Leetcode-109.Convert-Sorted-List-to-Binary-Search-Tree/">&lt;h2 id=&quot;leetcode-109convert-sorted-list-to-binary-search-tree&quot;&gt;Leetcode 109.Convert Sorted List to Binary Search Tree&lt;/h2&gt;

&lt;h4 id=&quot;题目&quot;&gt;题目：&lt;/h4&gt;

&lt;p&gt;Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.&lt;/p&gt;

&lt;p&gt;For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of &lt;em&gt;every&lt;/em&gt; node never differ by more than 1.&lt;/p&gt;

&lt;h4 id=&quot;示例&quot;&gt;示例：&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;        由于链表的值是顺序排列的，自然可以想的到用递归二分地构造BST，因为数组的长度非奇即偶，所以结点深度相差不会超过一。那么先取到链表的最中间元素作为根节点，再取前半部分以同样过程构造左子树，后半部分构造右子树即可。因为之前写过数组转BST，所以直接将链表转为数组再转为BST。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sortedListToBST&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;mapToInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; 			&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toArray&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="Leetcode" /></entry><entry><title type="html">MIT 6.824 Lab1 MapReduce</title><link href="https://yukiiris.github.io//mapreduce/" rel="alternate" type="text/html" title="MIT 6.824 Lab1 MapReduce" /><published>2018-03-22T00:00:00+00:00</published><updated>2018-03-22T00:00:00+00:00</updated><id>https://yukiiris.github.io//mapreduce</id><content type="html" xml:base="https://yukiiris.github.io//mapreduce/">&lt;h2 id=&quot;mit-6824-lab1-mapreduce&quot;&gt;MIT 6.824 Lab1 MapReduce&lt;/h2&gt;

&lt;p&gt;###简介&lt;/p&gt;

&lt;p&gt;         &lt;strong&gt;MapReduce&lt;/strong&gt;是Google提出的一个软件架构，用于大规模数据集（大于1TB）的并行运算。概念“Map（映射）”和“Reduce（归纳）”。&lt;/p&gt;

&lt;p&gt;        当前的软件实现是指定一个&lt;em&gt;Map（映射）&lt;/em&gt;函数，用来把一组键值对映射成一组新的键值对，指定并发的&lt;em&gt;Reduce（归纳）&lt;/em&gt;函数，用来保证所有映射的键值对中的每一个共享相同的键组。&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-i-mapreduce-input-and-output&quot;&gt;Part I: Map/Reduce input and output&lt;/h3&gt;

&lt;p&gt;        在这个part中，需要扩展common_map.go的doMap()和在common_reduce中的doReduce()。具体如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;doMap方法的任务：它读入输入文件，对内容调用用户定义的map方法mapF()，然后将mapF的输出分割成nReduce个中间文件。&lt;/li&gt;
  &lt;li&gt;每个reduce任务都有一个中间文件，用reduceName()生成文件名。为每个键调用ihash()并模除 nReduce，为键值对选择r。&lt;/li&gt;
  &lt;li&gt;mapF()是用户提供的方法，它为reduce返回一个包含一个键值对的切片。&lt;/li&gt;
  &lt;li&gt;doreduce方法的任务：它为任务读出中间文件，通过键为这些中间键值对排序，为每个键调用用户定义的reduceF方法，将结果写入硬盘。&lt;/li&gt;
  &lt;li&gt;你必须为每个map任务读出中间文件，reduceName方法为每个map任务产生文件名。&lt;/li&gt;
  &lt;li&gt;你的domap方法将中间文件的键值对进行了编码，所以你需要解码。&lt;/li&gt;
  &lt;li&gt;reduceF方法由用户提供，你必须为每个不同的键以及它的所有值调用它。这个方法返回这些值应该对应的键。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;part-ii-single-worker-word-count&quot;&gt;Part II: Single-worker word count&lt;/h3&gt;

&lt;p&gt;        这个part需要实现一个统计文档中出现单词数目的简单例子，具体如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;mapF()方法将每个传入的内容分割为单词，用单词作为键，为每个单词创建一个KeyValue，值为”1”，表示这个单词出现了一次，将所有KeyValue作为一个切片返回。&lt;/li&gt;
  &lt;li&gt;redecuF()方法传入一个键和键对应的值数组，这个方法需要将所以mapF()中统计的次数相加，返回值数组的长度即可。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;part-iii-distributing-mapreduce-tasks&quot;&gt;Part III: Distributing MapReduce tasks&lt;/h3&gt;

&lt;p&gt;        这个part需要用分布式的思想实现上一个part的内容，具体采用的是RPC调用和go的并发编程来实现。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;schedule()在所给的阶段（map或reduce）启动和等待所有任。mapFIles参数决定map阶段输入文件的名字。nReduce参数是reduce任务的数量。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;所有ntasks个任务都会在worker中被调度，当所有任务都成功结束，schedule()会返回。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;master线程在mapreduce中调用两次schedule()，map阶段一次，reduce()阶段一次。这个方法的任务是，把任务交给可用的工作线程。因为任务一般比工作线程多，所以schedule()方法要给每个工作线程一系列任务。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;schedule()方法从rigisterChan参数中知道有多少可用的工作线程。这个channel为每个worker产生一个字符串包含它的RPC地址。一些worker在schedule()方法被调用之前已经存在，另一些会在这个方法运行时生成，schedule()必须将这些worker充分利用。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;schedule()通过发送一个Worker.DoTask RPC给worker来告诉它要执行任务。&lt;/p&gt;

    &lt;p&gt;这里踩了好几个坑，说明一下。一个是关于sync.WaitGroup的add和Done两个方法，add只能在主线程里面用，想要同步线程就要在其他线程里用Done。然后将worker从registerChan中读出来并结束一个任务以后要放回去（来自一个没怎么学go并发编程的兄弟）&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;part-iv-handling-worker-failures&quot;&gt;Part IV: Handling worker failures&lt;/h3&gt;

&lt;p&gt;        在这个part中，需要处理失败的线程。&lt;/p&gt;

&lt;p&gt;        一个RPC调用失败不意味着这个线程没有在执行这个任务，这可能是因为返回值丢失或者master线程的RPC调用超时了。因此，这可能会发生于两个worker收到相同任务，处理他，然后生成输出。两个map或reduce调用必须对同一个输入生成相同的输出，所以如果后续处理有时读取一个输出并且有时读取另一个输出，则不会有不一致。除此之外，MapReduce框架确保map方法和reduce方法自动输出：输出文件可能不存在也可能包含map或reduce方法的单个执行的全部输出。&lt;/p&gt;</content><author><name></name></author><category term="go" /><summary type="html">踩了一万个坑才成功</summary></entry><entry><title type="html">理解JAVA虚拟机（三）</title><link href="https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA-%E4%B8%89/" rel="alternate" type="text/html" title="理解JAVA虚拟机（三）" /><published>2017-10-07T00:00:00+00:00</published><updated>2017-10-07T00:00:00+00:00</updated><id>https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA(%E4%B8%89)</id><content type="html" xml:base="https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA-%E4%B8%89/">&lt;h2 id=&quot;1-类加载的时机&quot;&gt;1. 类加载的时机&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;类的生命周期
    &lt;ul&gt;
      &lt;li&gt;加载、验证、准备、解析、初始化、使用、卸载&lt;/li&gt;
      &lt;li&gt;其中验证、准备、解析又被称为连接&lt;/li&gt;
      &lt;li&gt;这是一个交叉的过程&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;如何触发类的初始化（&lt;strong&gt;有且只有&lt;/strong&gt;）
    &lt;ul&gt;
      &lt;li&gt;遇到new、getstatic、putstatic、invokestatic&lt;/li&gt;
      &lt;li&gt;使用反射&lt;/li&gt;
      &lt;li&gt;初始化一个类的时候父类还没有初始化&lt;/li&gt;
      &lt;li&gt;虚拟机启动时，含main方法&lt;/li&gt;
      &lt;li&gt;啥句柄之类的，看不懂&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-类加载的过程&quot;&gt;2. 类加载的过程&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;加载
    &lt;ul&gt;
      &lt;li&gt;通过一个类的全限定名来获取定义此类的二进制字节流&lt;/li&gt;
      &lt;li&gt;将这个字节流所代表的静态存储结构转化为方法区运行时的数据结构&lt;/li&gt;
      &lt;li&gt;在内存中生成一个代表此类的Class对象，作为方法区这个类各种数据的入口&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;验证
    &lt;ul&gt;
      &lt;li&gt;文件格式验证（魔数、版本号、常量类型等）&lt;/li&gt;
      &lt;li&gt;元数据验证（是否有父类、父类是否允许被继承、是否实现未实现的方法或接口等）&lt;/li&gt;
      &lt;li&gt;字节码验证（通过数据流和控制流分析确定程序语义是合法的、符合逻辑的&lt;/li&gt;
      &lt;li&gt;符号引用验证（对除自身以外的类的信息的匹配））&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;准备（正式为类分配内存）
    &lt;ul&gt;
      &lt;li&gt;仅包括类变量，而不是实例变量&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;解析（将符号引用转为直接引用）
    &lt;ul&gt;
      &lt;li&gt;符号引用：一组描述所引用目标的符号，可以是任何字面量&lt;/li&gt;
      &lt;li&gt;直接引用：如果有了直接引用，引用的目标必定已存在在内存中&lt;/li&gt;
      &lt;li&gt;可能会对同一个符号引用进行多次解析，可以进行缓存&lt;/li&gt;
      &lt;li&gt;类或接口的解析
        &lt;ul&gt;
          &lt;li&gt;不是数组&lt;/li&gt;
          &lt;li&gt;是数组&lt;/li&gt;
          &lt;li&gt;访问权限&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;字段解析&lt;/li&gt;
      &lt;li&gt;类方法解析&lt;/li&gt;
      &lt;li&gt;接口方法解析&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;初始化（执行类构造器的&lt;clinit&gt;()方法
&lt;/clinit&gt;    &lt;ul&gt;
      &lt;li&gt;clinit是所有类变量的赋值动作和静态语句块的集合&lt;/li&gt;
      &lt;li&gt;此方法不需要显示调用父类构造器&lt;/li&gt;
      &lt;li&gt;可能会产生阻塞&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;3-类加载器&quot;&gt;3. 类加载器&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;类与类加载器&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><category term="JVM" /><category term="Java" /><summary type="html">虚拟类加载机制</summary></entry><entry><title type="html">操作系统学习笔记（三）</title><link href="https://yukiiris.github.io//%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%89/" rel="alternate" type="text/html" title="操作系统学习笔记（三）" /><published>2017-10-01T00:00:00+00:00</published><updated>2017-10-01T00:00:00+00:00</updated><id>https://yukiiris.github.io//%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%B8%89)</id><content type="html" xml:base="https://yukiiris.github.io//%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%89/">&lt;h2 id=&quot;第三章-存储系统&quot;&gt;第三章 存储系统&lt;/h2&gt;

&lt;h3 id=&quot;31-无存储器抽象&quot;&gt;3.1 无存储器抽象&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;用户可以直接访问物理内存，不安全&lt;/li&gt;
  &lt;li&gt;运行多个程序很困难&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;32-地址空间&quot;&gt;3.2 地址空间&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;地址空间的概念
    &lt;ul&gt;
      &lt;li&gt;地址空间是一个进程可用于寻址内存的一套地址集合&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;动态重定位&lt;/strong&gt;将进程的地址空间和物理内存进行一个简单的映射&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;交换技术
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;交换&lt;/strong&gt;：把一个进程完整调入内存，运行一段时间后存回磁盘
        &lt;ul&gt;
          &lt;li&gt;不同的进程一直在交换&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;内存紧缩&lt;/strong&gt;：将交换产生的空洞尽量合成大的内存区&lt;/li&gt;
          &lt;li&gt;动态分配：预留空间&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;虚拟内存&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;空闲内存管理（动态分配内存）
    &lt;ul&gt;
      &lt;li&gt;使用位图的存储管理
        &lt;ul&gt;
          &lt;li&gt;分配单元&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;使用链表的存储管理
        &lt;ul&gt;
          &lt;li&gt;链表中的一个结点或者包含一个进程，或者两个进程间的空闲区&lt;/li&gt;
          &lt;li&gt;结点包含以下区域：空闲区或进程的指示标志、起始地址、长度和指向下一结点的指针&lt;/li&gt;
          &lt;li&gt;首次适配算法&lt;/li&gt;
          &lt;li&gt;下次适配算法&lt;/li&gt;
          &lt;li&gt;最佳适配算法&lt;/li&gt;
          &lt;li&gt;最差适配算法&lt;/li&gt;
          &lt;li&gt;进程与空闲区用两个链表管理&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;33-虚拟内存&quot;&gt;3.3 虚拟内存&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;虚拟内存的基本思想：每个程序有自己的地址空间，这个空间被分成多个块，每一块被称作一页或&lt;strong&gt;页面&lt;/strong&gt;，每一页有连续的内存范围&lt;/li&gt;
  &lt;li&gt;分页
    &lt;ul&gt;
      &lt;li&gt;当程序运行时会产生的地址成为虚拟地址，它们构成虚拟地址空间
        &lt;ul&gt;
          &lt;li&gt;没有虚拟内存的计算机上，系统将虚拟地址送到内存总线上，直接操作物理地址&lt;/li&gt;
          &lt;li&gt;有虚拟内存的计算机上，系统将虚拟地址送到内存管理单元上（MMU），MMU将其映射到物理内存&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;虚拟地址空间被分为页面，对应的物理内存单元被称为页框，通常一样大
        &lt;ul&gt;
          &lt;li&gt;虚拟内存能比物理内存大，通过设置MMU可以使其完整映射，但不能解决这个问题&lt;/li&gt;
          &lt;li&gt;页面没有被映射而被访问会引起页面中断陷阱&lt;/li&gt;
          &lt;li&gt;MMU内部
            &lt;ul&gt;
              &lt;li&gt;虚拟地址二进制为16位，被分为4位的页号和12位的偏移量，前者表示16个页面，后者可以表示一页4096个全部地址之一&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;页表（虚拟地址和物理地址的关系）
    &lt;ul&gt;
      &lt;li&gt;最简单的映射实现：虚拟地址被分为虚拟页号和偏移量&lt;/li&gt;
      &lt;li&gt;虚拟页号可以作为页表的索引，页表可以找到页框号，用页框号替换页号与偏移量拼接形成物理地址&lt;/li&gt;
      &lt;li&gt;页表项的细节
        &lt;ul&gt;
          &lt;li&gt;页框号&lt;/li&gt;
          &lt;li&gt;“在/不在”位&lt;/li&gt;
          &lt;li&gt;“保护”：指出允许访问的类型（权限）&lt;/li&gt;
          &lt;li&gt;“修改”/“访问”位：修改过要保存，否则丢弃&lt;/li&gt;
          &lt;li&gt;“禁止被高速缓存”&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;虚拟内存的本质是建立对物理内存的抽象&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;加速分页过程
    &lt;ul&gt;
      &lt;li&gt;主要问题
        &lt;ul&gt;
          &lt;li&gt;映射要很快&lt;/li&gt;
          &lt;li&gt;如果虚拟地址空间很大，页表也要很大&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;转换检测缓冲区
        &lt;ul&gt;
          &lt;li&gt;大多数程序总是对少量页表进行多次访问&lt;/li&gt;
          &lt;li&gt;为计算机设置一个小型硬件将虚拟地址直接映射到物理地址，这个硬件被称为&lt;strong&gt;转换检测缓冲区/相联存储器（TLB）&lt;/strong&gt;，通常在MMU中&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;软件TLB管理：由操作系统管理被频繁访问的地址
        &lt;ul&gt;
          &lt;li&gt;软失效：访问的页面在内存而不在TLB中&lt;/li&gt;
          &lt;li&gt;硬失效：访问的页面不在内存不在TLB而在磁盘中&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;针对大内存的页表
    &lt;ul&gt;
      &lt;li&gt;多级页表
        &lt;ul&gt;
          &lt;li&gt;避免把全部页表存在内存中&lt;/li&gt;
          &lt;li&gt;PT1、PT2、Offset&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;倒排页表
        &lt;ul&gt;
          &lt;li&gt;实际内存中的每一个页框有一个表项&lt;/li&gt;
          &lt;li&gt;映射会变得困难，需要倒排而不能用索引直接访问&lt;/li&gt;
          &lt;li&gt;散列表/TLB&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;34-页面置换算法&quot;&gt;3.4 页面置换算法&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;发生页面中断时，操作系统要在内存中选择一个页面将其换出内存，腾出空间&lt;/li&gt;
  &lt;li&gt;最优页面置换算法：置换最不容易被访问的页面（无法实现）&lt;/li&gt;
  &lt;li&gt;最近未使用页面置换算法：随机地从类编号最小的非空类中选一个淘汰&lt;/li&gt;
  &lt;li&gt;先进先出页面置换算法&lt;/li&gt;
  &lt;li&gt;第二次机会页面置换算法：寻找最近的时钟间隔以来没有被访问的页面，是对先进先出页面置换算法的改进&lt;/li&gt;
  &lt;li&gt;时钟页面置换算法：第二次机会页面置换算法的改进，环形链表&lt;/li&gt;
  &lt;li&gt;最近最少使用页面置换算法&lt;/li&gt;
  &lt;li&gt;软件模拟LRU&lt;/li&gt;
  &lt;li&gt;工作集页面置换算法&lt;/li&gt;
  &lt;li&gt;工作集时钟页面置换算法&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;35-分页系统中的设计问题&quot;&gt;3.5 分页系统中的设计问题&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;局部分配策略与全局分配策略&lt;/li&gt;
&lt;/ol&gt;</content><author><name></name></author><summary type="html">第三章 存储系统</summary></entry><entry><title type="html">理解java虚拟机（二）</title><link href="https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA-%E4%BA%8C/" rel="alternate" type="text/html" title="理解java虚拟机（二）" /><published>2017-09-25T00:00:00+00:00</published><updated>2017-09-25T00:00:00+00:00</updated><id>https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA(%E4%BA%8C)</id><content type="html" xml:base="https://yukiiris.github.io//%E7%90%86%E8%A7%A3JAVA%E8%99%9A%E6%8B%9F%E6%9C%BA-%E4%BA%8C/">&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;h2 id=&quot;2对象&quot;&gt;2.对象&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;引用计数法
    &lt;ul&gt;
      &lt;li&gt;难以解决对象之间互相循环引用的问题&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;可达性分析算法
    &lt;ul&gt;
      &lt;li&gt;当一个对象&lt;/li&gt;
      &lt;li&gt;到GC没有任何引用链，此对象不可用&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;引用
    &lt;ul&gt;
      &lt;li&gt;强引用、软引用、弱引用、虚引用&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;生存还是死亡
    &lt;ul&gt;
      &lt;li&gt;如果对象在可达性分析后被发现不可达，它会被第一次标记并且进行一次筛选，筛选的条件是此对象是否有必要执行finalize（）方法&lt;/li&gt;
      &lt;li&gt;若有必要，这个对象则会被放到&lt;strong&gt;F-Queue&lt;/strong&gt;中缓慢执行&lt;/li&gt;
      &lt;li&gt;在执行finalize（）是最后一次逃脱被销毁的机会——将自己与另一个引用链上的对象建立引用&lt;/li&gt;
      &lt;li&gt;finalize（）只会被执行一次&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;回收方法区&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;3-垃圾回收算法&quot;&gt;3. 垃圾回收算法&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;标记-清除算法
    &lt;ul&gt;
      &lt;li&gt;首先标记出要回收的对象，在标记完成后统一回收所有被标记的对象&lt;/li&gt;
      &lt;li&gt;效率不高，标记和清除都比较慢&lt;/li&gt;
      &lt;li&gt;空间问题，标记清除会产生大量不连续的内存碎片，分配大对象内存时，如果没有足够内存会再次触发垃圾收集&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;复制算法
    &lt;ul&gt;
      &lt;li&gt;将可用内存分为大小相等两块，当其中一块内存用完了，就把还活着的对象复制到另一块上，再将这一块一次清理完&lt;/li&gt;
      &lt;li&gt;减小一半可用内存，代价太大&lt;/li&gt;
      &lt;li&gt;不需要1：1划分空间&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;标记-整理算法
    &lt;ul&gt;
      &lt;li&gt;复制算法在存活率高的时候效率比较低&lt;/li&gt;
      &lt;li&gt;标记后使其向一边移动，然后直接清理边界内存&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;分代收集算法
    &lt;ul&gt;
      &lt;li&gt;把内存按照对象存活周期分为几个部分，根据各个年代采用最适当的算法&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;4-hotspot算法实现&quot;&gt;4. HotSpot算法实现&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;枚举根节点
    &lt;ul&gt;
      &lt;li&gt;查找全局引用和暂停来保证一致性上会耗费很多时间&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;安全点
    &lt;ul&gt;
      &lt;li&gt;程序不能再任意位置停下来进行GC，于是设置适当的安全点&lt;/li&gt;
      &lt;li&gt;抢先式中断：首先把线程全部暂停，如果发现有线程不在安全点上，就恢复它让它跑到安全点上&lt;/li&gt;
      &lt;li&gt;主动式中断：不对线程进行操作，设置一个标志，各个线程执行时主动轮询这个标志，发现中断标志为真就暂停&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;安全区域
    &lt;ul&gt;
      &lt;li&gt;为正在休眠的线程设计&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;5-垃圾收集器&quot;&gt;5. 垃圾收集器&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Serial收集器
    &lt;ul&gt;
      &lt;li&gt;单线程、“stop the world”&lt;/li&gt;
      &lt;li&gt;没有线程交互开销，专心做垃圾收集自然可以获得最高的单线程收集效率&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;ParNew收集器
    &lt;ul&gt;
      &lt;li&gt;Serial的多线程版&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Parallel Scavenge收集器
    &lt;ul&gt;
      &lt;li&gt;目标是达到一个可控制的吞吐量，更高效利用CPU&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Serial Old收集器
    &lt;ul&gt;
      &lt;li&gt;Serial收集器的老年代版&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Parallel Old收集器
    &lt;ul&gt;
      &lt;li&gt;Parallel Scavenge老年版&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;CMS收集器
    &lt;ul&gt;
      &lt;li&gt;以最短停顿时间为目标的收集器&lt;/li&gt;
      &lt;li&gt;基于“标记-清除”实现&lt;/li&gt;
      &lt;li&gt;CMS收集器的内存回收过程和用户线程一起并发执行&lt;/li&gt;
      &lt;li&gt;并发收集、低停顿&lt;/li&gt;
      &lt;li&gt;对CPU资源非常敏感&lt;/li&gt;
      &lt;li&gt;无法处理浮动垃圾&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;GI收集器
    &lt;ul&gt;
      &lt;li&gt;并行与并发&lt;/li&gt;
      &lt;li&gt;分代收集&lt;/li&gt;
      &lt;li&gt;空间整合&lt;/li&gt;
      &lt;li&gt;可预测停顿&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;GC日志&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;6-内存分配与回收策略&quot;&gt;6. 内存分配与回收策略&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;对象优先在eden分配&lt;/li&gt;
  &lt;li&gt;大对象直接进入老年代&lt;/li&gt;
  &lt;li&gt;长期存活的对象将进入老年代&lt;/li&gt;
  &lt;li&gt;动态对象年龄判断&lt;/li&gt;
  &lt;li&gt;空间分配担保&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">1. 概述</summary></entry></feed>