Skip to content

Commit 70de8e9

Browse files
committed
bk
1 parent 9021b7c commit 70de8e9

File tree

93 files changed

+4200
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+4200
-423
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
// https://www.cnblogs.com/grandyang/p/4350381.html 的思想
4+
// https://leetcode.com/problems/text-justification/discuss/24873/Share-my-concise-c++-solution-less-than-20-lines 中 @rpfly3的回答
5+
vector<string> fullJustify(vector<string>& words, int maxWidth) {
6+
vector<string> res;
7+
int n=words.size();
8+
int cur_width=0;
9+
int j;
10+
for(int i=0;i<n;i=j){
11+
12+
for(j=i,cur_width=0;j<n && cur_width+words[j].size()+j-i<=maxWidth;j++)
13+
cur_width+=words[j].size();
14+
15+
// 这是为了最后一行准备的默认值
16+
int space=1;
17+
int extra=0;
18+
19+
if(j-1!=i && j!=words.size()){
20+
space=(maxWidth-cur_width)/(j-i-1); // j-i-1 是空的数目,space是空的大小
21+
extra=(maxWidth-cur_width)%(j-i-1);
22+
}
23+
// cout<<words[i]<<" "<<words[j-1]<<" "<<space<<" "<<extra<<endl;
24+
25+
string line="";
26+
for(int k=i;k<j;k++){
27+
line+=words[k];
28+
if(k!=j-1) line+=string(space, ' ');
29+
if(extra-->0) line+=' ';
30+
}
31+
line += string(maxWidth - line.size(), ' ');
32+
res.push_back(line);
33+
}
34+
return res;
35+
}
36+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p>Given an array of words and a width&nbsp;<em>maxWidth</em>, format the text such that each line has exactly <em>maxWidth</em> characters and is fully (left and right) justified.</p>
2+
3+
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces <code>&#39; &#39;</code> when necessary so that each line has exactly <em>maxWidth</em> characters.</p>
4+
5+
<p>Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.</p>
6+
7+
<p>For the last line of text, it should be left justified and no <strong>extra</strong> space is inserted between words.</p>
8+
9+
<p><strong>Note:</strong></p>
10+
11+
<ul>
12+
<li>A word is defined as a character sequence consisting&nbsp;of non-space characters only.</li>
13+
<li>Each word&#39;s length is&nbsp;guaranteed to be greater than 0 and not exceed <em>maxWidth</em>.</li>
14+
<li>The input array <code>words</code>&nbsp;contains at least one word.</li>
15+
</ul>
16+
17+
<p><strong>Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong>
21+
words = [&quot;This&quot;, &quot;is&quot;, &quot;an&quot;, &quot;example&quot;, &quot;of&quot;, &quot;text&quot;, &quot;justification.&quot;]
22+
maxWidth = 16
23+
<strong>Output:</strong>
24+
[
25+
&nbsp; &nbsp;&quot;This &nbsp; &nbsp;is &nbsp; &nbsp;an&quot;,
26+
&nbsp; &nbsp;&quot;example &nbsp;of text&quot;,
27+
&nbsp; &nbsp;&quot;justification. &nbsp;&quot;
28+
]
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong>
35+
words = [&quot;What&quot;,&quot;must&quot;,&quot;be&quot;,&quot;acknowledgment&quot;,&quot;shall&quot;,&quot;be&quot;]
36+
maxWidth = 16
37+
<strong>Output:</strong>
38+
[
39+
&nbsp; &quot;What &nbsp; must &nbsp; be&quot;,
40+
&nbsp; &quot;acknowledgment &nbsp;&quot;,
41+
&nbsp; &quot;shall be &nbsp; &nbsp; &nbsp; &nbsp;&quot;
42+
]
43+
<strong>Explanation:</strong> Note that the last line is &quot;shall be &quot; instead of &quot;shall be&quot;,
44+
&nbsp; because the last line must be left-justified instead of fully-justified.
45+
Note that the second line is also left-justified becase it contains only one word.
46+
</pre>
47+
48+
<p><strong>Example 3:</strong></p>
49+
50+
<pre>
51+
<strong>Input:</strong>
52+
words = [&quot;Science&quot;,&quot;is&quot;,&quot;what&quot;,&quot;we&quot;,&quot;understand&quot;,&quot;well&quot;,&quot;enough&quot;,&quot;to&quot;,&quot;explain&quot;,
53+
&nbsp; &quot;to&quot;,&quot;a&quot;,&quot;computer.&quot;,&quot;Art&quot;,&quot;is&quot;,&quot;everything&quot;,&quot;else&quot;,&quot;we&quot;,&quot;do&quot;]
54+
maxWidth = 20
55+
<strong>Output:</strong>
56+
[
57+
&nbsp; &quot;Science &nbsp;is &nbsp;what we&quot;,
58+
&quot;understand &nbsp; &nbsp; &nbsp;well&quot;,
59+
&nbsp; &quot;enough to explain to&quot;,
60+
&nbsp; &quot;a &nbsp;computer. &nbsp;Art is&quot;,
61+
&nbsp; &quot;everything &nbsp;else &nbsp;we&quot;,
62+
&nbsp; &quot;do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;
63+
]
64+
</pre>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p>Given a text file <code>file.txt</code> that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.</p>
2+
3+
<p>You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)</p>
4+
5+
<p>You may also assume each line in the text file must not contain leading or trailing white spaces.</p>
6+
7+
<p><strong>Example:</strong></p>
8+
9+
<p>Assume that <code>file.txt</code> has the following content:</p>
10+
11+
<pre>
12+
987-123-4567
13+
123 456 7890
14+
(123) 456-7890
15+
</pre>
16+
17+
<p>Your script should output the following valid phone numbers:</p>
18+
19+
<pre>
20+
987-123-4567
21+
(123) 456-7890
22+
</pre>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node{
2+
public:
3+
bool end=false;
4+
vector<Node*> next;
5+
6+
Node(){
7+
next.clear();
8+
next.resize(26,nullptr);
9+
}
10+
};
11+
12+
class WordDictionary {
13+
public:
14+
Node *root=new Node();
15+
16+
/** Initialize your data structure here. */
17+
WordDictionary() {
18+
19+
}
20+
21+
/** Adds a word into the data structure. */
22+
void addWord(string word) {
23+
auto cur=root;
24+
for(const auto &c:word){
25+
if(cur->next[c-'a']==nullptr) cur->next[c-'a']=new Node();
26+
cur=cur->next[c-'a'];
27+
}
28+
cur->end=true;
29+
}
30+
31+
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
32+
bool search(string word) {
33+
return dfs(word,0,root);
34+
}
35+
36+
bool dfs(string &s, int i, Node* cur){
37+
if(i==s.size()){
38+
if(cur->end) return true;
39+
else return false;
40+
}
41+
42+
if(s[i]=='.'){
43+
for(auto &v:cur->next){
44+
if(v==nullptr) continue;
45+
if(dfs(s,i+1,v)) return true;
46+
}
47+
return false;
48+
}
49+
50+
if(cur->next[s[i]-'a']==nullptr) return false;
51+
return dfs(s,i+1,cur->next[s[i]-'a']);
52+
}
53+
};
54+
55+
/**
56+
* Your WordDictionary object will be instantiated and called as such:
57+
* WordDictionary* obj = new WordDictionary();
58+
* obj->addWord(word);
59+
* bool param_2 = obj->search(word);
60+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>Design a data structure that supports the following two operations:</p>
2+
3+
<pre>
4+
void addWord(word)
5+
bool search(word)
6+
</pre>
7+
8+
<p>search(word) can search a literal word or a regular expression string containing only letters <code>a-z</code> or <code>.</code>. A <code>.</code> means it can represent any one letter.</p>
9+
10+
<p><strong>Example:</strong></p>
11+
12+
<pre>
13+
addWord(&quot;bad&quot;)
14+
addWord(&quot;dad&quot;)
15+
addWord(&quot;mad&quot;)
16+
search(&quot;pad&quot;) -&gt; false
17+
search(&quot;bad&quot;) -&gt; true
18+
search(&quot;.ad&quot;) -&gt; true
19+
search(&quot;b..&quot;) -&gt; true
20+
</pre>
21+
22+
<p><b>Note:</b><br />
23+
You may assume that all words are consist of lowercase letters <code>a-z</code>.</p>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Solution {
2+
public:
3+
// 这个解法实在是很trick了 使用了分桶思想
4+
// https://leetcode.com/problems/contains-duplicate-iii/discuss/61645/AC-O(N)-solution-in-Java-using-buckets-with-explanation
5+
// 大致看了楼主的帖子,然后是comments揭露了本质 @wzrthhj @WendyO__O
6+
// bucket_size要+1:避免了t为0时的除法。另外,本来就应该是t+1的,假设t=3,则[0,1,2,3]应该被分到一个桶中。0和3除以4都为0
7+
// remapn:需要重hash,是因为负数在除以整数的时候会向0靠近,这样把不应该分为一桶的数分到一桶里,例如t=2,则[0,1,2]应该在一桶,但是如果有-1,则变成了[-1,0,1,2],而distance(-1,2)大于了t,因此有问题。如果我们找到一个base,然后根据距离base的距离来进行分桶,就避免了负数的尴尬问题
8+
// 相邻桶中元素也需要做比较的原因(remapn-bucket_set[bucketid-1]<=t):假如桶是这样的[[1,2,3],[4,5,6]],我们发现3和4的距离其实也是小于t的,即两个相邻桶中的元素也可能符合条件
9+
// bucket_set[bucketid]=remapn;会把之前的元素替换掉,但不会有问题的原因:我之前担心这个会影响相邻桶中元素做比较(假如有A、B两个桶,A中本来有一个元素a1距离B中元素b是小于t的,但是又来了一个a2将a1替换掉了,这样等到遍历到b时就不会发现a1了)。但这个其实不会影响,因为我们把上述顺序打出来看一下:a1->a2->b,当遍历到a2时,就会发现a1已经和自己落到同一个桶中了,直接return true,都等不到遍历到b。
10+
// 我们上面只考虑了t的限制,没有考虑到位置k的限制。解决方案:我们通过动态删除bucket_set元素,保证了当遍历到元素x时,桶中的元素的位置距离x一定是小于等于k的。
11+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
12+
if(t<0 || k<0) return false;
13+
14+
long long bucket_size=0ll+t+1;
15+
unordered_map<long long , long long> bucket_set;
16+
17+
for(int i=0;i<nums.size();i++){
18+
long long n=nums[i];
19+
long long remapn = 0ll+n-INT_MIN;
20+
long long bucketid = remapn/bucket_size;
21+
if(bucket_set.find(bucketid)!=bucket_set.end()) return true;
22+
if(bucket_set.find(bucketid-1)!=bucket_set.end() && remapn-bucket_set[bucketid-1]<=t) return true;
23+
if(bucket_set.find(bucketid+1)!=bucket_set.end() && bucket_set[bucketid+1]-remapn<=t) return true;
24+
25+
bucket_set[bucketid]=remapn;
26+
27+
if(bucket_set.size()>k){
28+
long long remapdeleten = 0ll+nums[i-k]-INT_MIN;
29+
long long deletebucketid=remapdeleten/bucket_size;
30+
bucket_set.erase(deletebucketid);
31+
}
32+
33+
}
34+
35+
return false;
36+
}
37+
38+
39+
40+
// 自己写的 暴力解法
41+
bool containsNearbyAlmostDuplicate1(vector<int>& nums, int k, int t) {
42+
// cout<<nums.size()<<" "<<k<<" "<<t<<endl;
43+
if(k<t) return k_is_samller(nums,k,t);
44+
else return t_is_samller(nums,k,t);
45+
}
46+
47+
// 遍历位置
48+
bool k_is_samller(vector<int>& nums, int k, int t){
49+
for(int i=0;i<nums.size();i++){
50+
for(int j=i+1;(j<nums.size() && j-i<=k);j++){
51+
if(llabs(nums[i]+0ll+-nums[j])<=t) return true;
52+
}
53+
}
54+
return false;
55+
}
56+
57+
// 遍历值
58+
bool t_is_samller(vector<int>& nums, int k, int t){
59+
unordered_map<long long,int> um; //value->position
60+
for(int i=0;i<nums.size();i++){
61+
62+
int v=nums[i];
63+
int p=i;
64+
65+
for(int i=0;i<=t;i++){
66+
if(um.find(v+0ll-i)!=um.end() && abs(um[v-i]-p)<=k) return true;
67+
if(um.find(v+0ll+i)!=um.end() && abs(um[v+i]-p)<=k) return true;
68+
}
69+
70+
um[v]=p;
71+
72+
}
73+
74+
return false;
75+
}
76+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>Given an array of integers, find out whether there are two distinct indices <i>i</i> and <i>j</i> in the array such that the <b>absolute</b> difference between <b>nums[i]</b> and <b>nums[j]</b> is at most <i>t</i> and the <b>absolute</b> difference between <i>i</i> and <i>j</i> is at most <i>k</i>.</p>
2+
3+
<div>
4+
<p><strong>Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input: </strong>nums = <span id="example-input-1-1">[1,2,3,1]</span>, k = <span id="example-input-1-2">3</span>, t = <span id="example-input-1-3">0</span>
8+
<strong>Output: </strong><span id="example-output-1">true</span>
9+
</pre>
10+
11+
<div>
12+
<p><strong>Example 2:</strong></p>
13+
14+
<pre>
15+
<strong>Input: </strong>nums = <span id="example-input-2-1">[1,0,1,1]</span>, k = <span id="example-input-2-2">1</span>, t = <span id="example-input-2-3">2</span>
16+
<strong>Output: </strong><span id="example-output-2">true</span>
17+
</pre>
18+
19+
<div>
20+
<p><strong>Example 3:</strong></p>
21+
22+
<pre>
23+
<strong>Input: </strong>nums = <span id="example-input-3-1">[1,5,9,1,5,9]</span>, k = <span id="example-input-3-2">2</span>, t = <span id="example-input-3-3">3</span>
24+
<strong>Output: </strong><span id="example-output-3">false</span>
25+
</pre>
26+
</div>
27+
</div>
28+
</div>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution {
2+
public:
3+
// https://leetcode.com/problems/majority-element-ii/discuss/63520/Boyer-Moore-Majority-Vote-algorithm-and-my-elaboration
4+
vector<int> majorityElement(vector<int>& nums) {
5+
vector<int> res;
6+
int t=nums.size()/3+1;
7+
8+
int c1=0;
9+
int c2=0;
10+
int a=INT_MIN; // 任意值
11+
int b=a;
12+
for(const auto &n:nums){
13+
if(n==a) c1++; // 先看值,而不是看个数。这样更简单
14+
else if(n==b) c2++;
15+
else if(c1==0) a=n,c1=1;
16+
else if(c2==0) b=n,c2=1;
17+
else{
18+
c1--;
19+
c2--;
20+
}
21+
// cout<<n<<"..."<<a<<":"<<c1<<" | "<<b<<":"<<c2<<endl;
22+
}
23+
24+
c1=0;
25+
c2=0;
26+
for(const auto &n:nums){
27+
c1+=(n==a);
28+
c2+=(n==b);
29+
}
30+
if(c1>=t) res.push_back(a);
31+
if(c2>=t) res.push_back(b);
32+
if(res.size()==2 && a==b) res.pop_back();
33+
34+
return res;
35+
}
36+
37+
// 自己写的,但是没有模板的感觉
38+
vector<int> majorityElement1(vector<int>& nums) {
39+
vector<int> res;
40+
int t=nums.size()/3+1;
41+
42+
int c1=0;
43+
int c2=0;
44+
int a=INT_MIN; // 任意值
45+
int b=a;
46+
for(const auto &n:nums){
47+
48+
if(c1==0 && n!=b) a=n;
49+
else if(c2==0) b=n;
50+
51+
if(n==a) c1++;
52+
else if(n==b) c2++;
53+
else{
54+
c1--;
55+
c2--;
56+
}
57+
// cout<<n<<"..."<<a<<":"<<c1<<" | "<<b<<":"<<c2<<endl;
58+
}
59+
60+
c1=0;
61+
c2=0;
62+
for(const auto &n:nums){
63+
c1+=(n==a);
64+
c2+=(n==b);
65+
}
66+
if(c1>=t) res.push_back(a);
67+
if(c2>=t && (res.empty() || res[0]!=b) ) res.push_back(b);
68+
69+
return res;
70+
}
71+
};

0 commit comments

Comments
 (0)