-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.xml
112 lines (89 loc) · 23.2 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Leetcode Repository</title>
<description></description>
<link>http://web.cs.dal.ca//~zwu/Leetcode/</link>
<atom:link href="http://web.cs.dal.ca//~zwu/Leetcode/feed.xml" rel="self" type="application/rss+xml"/>
<pubDate>Fri, 03 Jul 2015 10:54:34 -0700</pubDate>
<lastBuildDate>Fri, 03 Jul 2015 10:54:34 -0700</lastBuildDate>
<generator>Jekyll v2.5.3</generator>
<item>
<title>Rectangle Area</title>
<description><p>Find the total area covered by two rectilinear rectangles in a 2D plane.</p>
<p>Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. <a href="https://leetcode.com/static/images/problemset/rectangle_area.png">image</a></p>
<p>Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">Solution</span><span class="p">:</span>
<span class="c"># @param {integer} A</span>
<span class="c"># @param {integer} B</span>
<span class="c"># @param {integer} C</span>
<span class="c"># @param {integer} D</span>
<span class="c"># @param {integer} E</span>
<span class="c"># @param {integer} F</span>
<span class="c"># @param {integer} G</span>
<span class="c"># @param {integer} H</span>
<span class="c"># @return {integer}</span>
<span class="k">def</span> <span class="nf">computeArea</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">F</span><span class="p">,</span> <span class="n">G</span><span class="p">,</span> <span class="n">H</span><span class="p">):</span>
<span class="n">X1</span><span class="o">=</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n">C</span><span class="p">))</span>
<span class="n">X2</span><span class="o">=</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">E</span><span class="p">,</span><span class="n">G</span><span class="p">))</span>
<span class="n">Y1</span><span class="o">=</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="n">D</span><span class="p">))</span>
<span class="n">Y2</span><span class="o">=</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">H</span><span class="p">))</span>
<span class="n">Xlength</span><span class="o">=</span><span class="n">X1</span> <span class="o">&amp;</span> <span class="n">X2</span>
<span class="n">Ylength</span><span class="o">=</span> <span class="n">Y1</span> <span class="o">&amp;</span> <span class="n">Y2</span>
<span class="n">area</span><span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">X1</span><span class="p">)</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">Y1</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">X2</span><span class="p">)</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">Y2</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">Xlength</span><span class="p">)</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">Ylength</span><span class="p">)</span>
<span class="k">return</span> <span class="n">area</span>
<span class="k">def</span> <span class="nf">computeArea_anotherMethod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">F</span><span class="p">,</span> <span class="n">G</span><span class="p">,</span> <span class="n">H</span><span class="p">):</span>
<span class="c">#if two rectangles are not overlap, their left-bottom&#39;s (x,y) must be bigger than the other rectangle right-top point&#39;s either (x,y) </span>
<span class="k">if</span> <span class="n">C</span><span class="o">&lt;=</span><span class="n">E</span> <span class="ow">or</span> <span class="n">F</span><span class="o">&gt;=</span><span class="n">D</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="n">C</span><span class="o">-</span><span class="n">A</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">D</span><span class="o">-</span><span class="n">B</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">G</span><span class="o">-</span><span class="n">E</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">H</span><span class="o">-</span><span class="n">F</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">G</span><span class="o">&lt;=</span><span class="n">A</span> <span class="ow">or</span> <span class="n">B</span><span class="o">&gt;=</span><span class="n">H</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="n">C</span><span class="o">-</span><span class="n">A</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">D</span><span class="o">-</span><span class="n">B</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">G</span><span class="o">-</span><span class="n">E</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">H</span><span class="o">-</span><span class="n">F</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">common</span><span class="o">=</span> <span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">C</span><span class="p">,</span><span class="n">G</span><span class="p">)</span><span class="o">-</span><span class="nb">max</span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n">E</span><span class="p">))</span><span class="o">*</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">D</span><span class="p">,</span><span class="n">H</span><span class="p">)</span><span class="o">-</span><span class="nb">max</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="n">F</span><span class="p">))</span>
<span class="k">return</span> <span class="p">(</span><span class="n">C</span><span class="o">-</span><span class="n">A</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">D</span><span class="o">-</span><span class="n">B</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">G</span><span class="o">-</span><span class="n">E</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">H</span><span class="o">-</span><span class="n">F</span><span class="p">)</span><span class="o">-</span><span class="n">common</span></code></pre></div>
</description>
<pubDate>Wed, 01 Jul 2015 00:00:00 -0700</pubDate>
<link>http://web.cs.dal.ca//~zwu/Leetcode/leetcode/2015/07/01/Rectangle-Area.html</link>
<guid isPermaLink="true">http://web.cs.dal.ca//~zwu/Leetcode/leetcode/2015/07/01/Rectangle-Area.html</guid>
<category>leetcode</category>
</item>
<item>
<title>Subset I</title>
<description><p>Problem 1: Subset I</p>
<p>Given a set of distinct integers, S, return all possible subsets.</p>
<p>Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.</p>
<p>For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]</p>
<p>用二进制来排列组合</p>
<div class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">import</span> <span class="n">java</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">ArrayList</span><span class="p">;</span>
<span class="sr">/**</span>
<span class="sr"> * Created by zwu on 15/</span><span class="mi">2</span><span class="o">/</span><span class="mi">12</span><span class="o">.</span>
<span class="o">*/</span>
<span class="kp">public</span> <span class="k">class</span> <span class="nc">Subset</span> <span class="p">{</span>
<span class="kp">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="nb">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="n">int</span><span class="o">[]</span> <span class="n">array</span><span class="p">;</span>
<span class="n">array</span> <span class="o">=</span> <span class="kp">new</span> <span class="n">int</span><span class="o">[</span><span class="mi">3</span><span class="o">]</span><span class="p">;</span>
<span class="n">array</span> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="n">array</span> <span class="o">[</span><span class="mi">1</span><span class="o">]=</span> <span class="mi">2</span><span class="p">;</span>
<span class="n">array</span> <span class="o">[</span><span class="mi">2</span><span class="o">]=</span> <span class="mi">3</span><span class="p">;</span>
<span class="no">ArrayList</span><span class="o">&lt;</span><span class="no">ArrayList</span><span class="o">&lt;</span><span class="nb">Integer</span><span class="o">&gt;&gt;</span> <span class="n">subset</span> <span class="o">=</span> <span class="kp">new</span> <span class="no">ArrayList</span><span class="o">&lt;</span><span class="no">ArrayList</span><span class="o">&lt;</span><span class="nb">Integer</span><span class="o">&gt;&gt;</span><span class="p">();</span>
<span class="k">for</span><span class="p">(</span><span class="n">int</span> <span class="n">i</span> <span class="o">=</span><span class="mi">0</span> <span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="no">ArrayList</span><span class="o">&lt;</span><span class="nb">Integer</span><span class="o">&gt;</span> <span class="n">set</span> <span class="o">=</span> <span class="kp">new</span> <span class="no">ArrayList</span><span class="o">&lt;</span><span class="nb">Integer</span><span class="o">&gt;</span><span class="p">();</span>
<span class="k">for</span> <span class="p">(</span><span class="n">int</span> <span class="n">j</span><span class="o">=</span> <span class="mi">0</span> <span class="p">;</span> <span class="n">j</span><span class="o">&lt;</span><span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">){</span>
<span class="k">if</span> <span class="p">((</span><span class="n">i</span><span class="o">&amp;</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">j</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span> <span class="sr">//</span> <span class="err">判断二进制哪一位是</span><span class="mi">1</span>
<span class="n">set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">array</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">subset</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">set</span><span class="p">);</span>
<span class="p">}</span>
<span class="no">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="n">subset</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></div>
</description>
<pubDate>Fri, 13 Feb 2015 00:00:00 -0800</pubDate>
<link>http://web.cs.dal.ca//~zwu/Leetcode/leetcode/2015/02/13/Subset-I.html</link>
<guid isPermaLink="true">http://web.cs.dal.ca//~zwu/Leetcode/leetcode/2015/02/13/Subset-I.html</guid>
<category>leetcode</category>
</item>
</channel>
</rss>