Skip to content

Commit 2f31dc2

Browse files
author
TomasJiang
committed
April 6, 2016: add Contains Duplicate II
1 parent caef593 commit 2f31dc2

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

C++/chapLinearList.tex

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ \chapter{线性表}
55
\section{数组} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66

77
\subsection{Contains Duplicate}
8-
\label{sec:containd_duplicate}
8+
\label{sec:containd-duplicate}
99

1010

1111
\subsubsection{描述}
@@ -43,6 +43,76 @@ \subsubsection{相关题目}
4343
\myenddot
4444

4545

46+
\subsection{Contains Duplicate II}
47+
\label{sec:containd-duplicate-ii}
48+
49+
50+
\subsubsection{描述}
51+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
52+
53+
\subsubsection{分析}
54+
55+
56+
57+
\subsubsection{代码1}
58+
\begin{Code}
59+
// LeetCode, Contains Duplicate
60+
// 时间复杂度O(nlogn), 空间复杂度O(n)
61+
62+
class Solution {
63+
public:
64+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
65+
set<int> s;
66+
for (int i = 0; i < nums.size(); ++i) {
67+
if (i > k) {
68+
s.erase(nums[i-k-1]);
69+
70+
}
71+
if (!s.insert(nums[i]).second) {
72+
return true;
73+
}
74+
}
75+
return false;
76+
}
77+
};
78+
79+
\end{Code}
80+
81+
82+
\subsubsection{相关题目}
83+
84+
\begindot
85+
\item Contains Duplicate,见 \S \ref{sec:contains-duplicate}
86+
\item Contains Duplicate III,见 \S \ref{sec:contains-duplicate-iii}
87+
\myenddot
88+
89+
\subsection{Contains Duplicate III}
90+
\label{sec:containd-duplicate-iii}
91+
92+
93+
\subsubsection{描述}
94+
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.
95+
96+
\subsubsection{分析}
97+
98+
99+
100+
\subsubsection{代码1}
101+
\begin{Code}
102+
// LeetCode, Contains Duplicate
103+
// 时间复杂度O(nlogn), 空间复杂度O(n)
104+
105+
106+
\end{Code}
107+
108+
109+
\subsubsection{相关题目}
110+
111+
\begindot
112+
\item Contains Duplicate,见 \S \ref{sec:contains-duplicate}
113+
\item Contains Duplicate II,见 \S \ref{sec:contains-duplicate-ii}
114+
\myenddot
115+
46116

47117
\subsection{Remove Duplicates from Sorted Array}
48118
\label{sec:remove-duplicates-from-sorted-array}

C++/leetcode-cpp.pdf

423 KB
Binary file not shown.

0 commit comments

Comments
 (0)