Skip to content

Commit 6a09590

Browse files
author
chao.yang
committed
leet code
1 parent 1edd9b8 commit 6a09590

File tree

265 files changed

+10533
-0
lines changed

Some content is hidden

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

265 files changed

+10533
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/leetcode.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

1324_PRINT_WORDS_VERTICALLY.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <memory>
2+
#include <vector>
3+
using namespace std;
4+
class Solution {
5+
public:
6+
vector<string> printVertically(string s) {
7+
int totalNum = 0;
8+
int indexs[202];
9+
indexs[0] = 0;
10+
int lengths[202];
11+
int length = s.size();
12+
bool startCheck = true;
13+
int maxSubLen = 0;
14+
for(size_t i = 0; i < length; i++)
15+
{
16+
if(s[i] != ' ' && startCheck)
17+
{
18+
indexs[++totalNum] = i;
19+
lengths[totalNum - 1] = indexs[totalNum] - 1;
20+
maxSubLen = max(maxSubLen, indexs[totalNum] - indexs[totalNum - 1] - 1);
21+
startCheck = false;
22+
}
23+
else if(s[i] == ' ')
24+
{
25+
startCheck = true;
26+
}
27+
}
28+
29+
30+
indexs[++totalNum] = s.size() + 1;
31+
lengths[totalNum - 1] = indexs[totalNum] - 1;
32+
maxSubLen = max(maxSubLen, indexs[totalNum] - indexs[totalNum - 1] - 1);
33+
34+
std::vector<std::string> res;
35+
for(size_t j = 0; j < maxSubLen; j++)
36+
{
37+
std::string tmp(totalNum - 1, ' ');
38+
int lastEmpty = -1;
39+
for(size_t k = 1; k < totalNum; k++)
40+
{
41+
if(indexs[k] < lengths[k])
42+
{
43+
auto c = s[indexs[k]];
44+
tmp[k - 1] = c;
45+
if(c != ' ')
46+
{
47+
lastEmpty = k - 1;
48+
}
49+
indexs[k] += 1;
50+
51+
}
52+
}
53+
tmp.resize(lastEmpty + 1);
54+
res.emplace_back(tmp);
55+
}
56+
57+
return res;
58+
59+
}
60+
};
61+
62+
#include <iostream>
63+
int main()
64+
{
65+
auto result = Solution().printVertically("HOW U AK");
66+
for(auto& s: result)
67+
{
68+
std::cout << s << std::endl;
69+
}
70+
}

133_CLONE_GRAPH.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// Created by Administrator on 07/05/2020.
3+
//
4+
#include <vector>
5+
#include <unordered_map>
6+
using namespace std;
7+
8+
class Node {
9+
public:
10+
int val;
11+
vector<Node*> neighbors;
12+
13+
Node() {
14+
val = 0;
15+
neighbors = vector<Node*>();
16+
}
17+
18+
Node(int _val) {
19+
val = _val;
20+
neighbors = vector<Node*>();
21+
}
22+
23+
Node(int _val, vector<Node*> _neighbors) {
24+
val = _val;
25+
neighbors = _neighbors;
26+
}
27+
};
28+
29+
class Solution {
30+
public:
31+
Node* cloneGraph(Node* node) {
32+
if (node == nullptr)
33+
{
34+
return nullptr;
35+
}
36+
Node* newNode = new Node(node->val, node->neighbors);
37+
map_[newNode->val] = newNode;
38+
cloneNeighbors(newNode);
39+
map_.clear();
40+
return newNode;
41+
}
42+
43+
44+
void cloneNeighbors(Node* newNode)
45+
{
46+
auto& neighbors = newNode->neighbors;
47+
for(size_t i = 0; i < neighbors.size(); i++)
48+
{
49+
auto neighbor = neighbors.at(i);
50+
if(map_.find(neighbor->val) != map_.end())
51+
{
52+
neighbors[i] = map_[neighbor->val];
53+
}
54+
else
55+
{
56+
Node* n = new Node(neighbor->val, neighbor->neighbors);
57+
map_[neighbor->val] = n;
58+
neighbors[i] = n;
59+
cloneNeighbors(n);
60+
}
61+
}
62+
}
63+
private:
64+
unordered_map<int, Node*> map_;
65+
};
66+
67+
int main()
68+
{
69+
Node* one = new Node(1);
70+
Node* two = new Node(2);
71+
72+
one->neighbors = vector<Node*>{two};
73+
two->neighbors = vector<Node*>{one};
74+
75+
Solution().cloneGraph(one);
76+
}

146_LRU_CACHE.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// Created by Administrator on 06/05/2020.
3+
//
4+
5+
#include <unordered_map>
6+
#include <list>
7+
using namespace std;
8+
class LRUCache {
9+
public:
10+
LRUCache(int capacity):
11+
capacity_(capacity)
12+
{
13+
14+
}
15+
16+
int get(int key) {
17+
if(map_.find(key) == map_.end())
18+
{
19+
return -1;
20+
}
21+
22+
auto it = map_[key];
23+
24+
auto nodePointer = *it;
25+
list_.erase(it);
26+
list_.push_front(nodePointer);
27+
map_[key] = list_.begin();
28+
return nodePointer->value;
29+
}
30+
31+
void put(int key, int value) {
32+
if(capacity_ <= 0)
33+
{
34+
return;
35+
}
36+
37+
if(map_.find(key) == map_.end())
38+
{
39+
if(list_.size() >= capacity_)
40+
{
41+
auto it = --list_.end();
42+
auto nodePointer = *it;
43+
map_.erase(map_.find(nodePointer->key));
44+
list_.pop_back();
45+
46+
delete nodePointer;
47+
}
48+
list_.push_front(new ListNode(key, value));
49+
map_[key] = list_.begin();
50+
}
51+
else{
52+
auto it = map_[key];
53+
auto nodePointer = *it;
54+
nodePointer->value = value;
55+
list_.erase(it);
56+
list_.push_front(nodePointer);
57+
map_[key] = list_.begin();
58+
}
59+
}
60+
61+
private:
62+
struct ListNode{
63+
int key;
64+
int value;
65+
ListNode(int k, int v):
66+
key(k),
67+
value(v)
68+
{
69+
70+
}
71+
};
72+
list<ListNode*> list_;
73+
using MyIterator = list<ListNode*>::iterator;
74+
unordered_map<int, MyIterator> map_;
75+
int capacity_;
76+
};
77+
78+
/**
79+
* Your LRUCache object will be instantiated and called as such:
80+
* LRUCache* obj = new LRUCache(capacity);
81+
* int param_1 = obj->get(key);
82+
* obj->put(key,value);
83+
*/
84+
85+
#include <iostream>
86+
int main()
87+
{
88+
LRUCache cache(2);
89+
90+
cache.put(2, 1);
91+
cache.put(2, 2);
92+
std::cout << cache.get(2) << std::endl;
93+
cache.put(1, 1);
94+
cache.put(4, 1);
95+
std::cout << cache.get(2) << std::endl;
96+
}

148_SORT_LIST.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct ListNode {
2+
int val;
3+
ListNode *next;
4+
ListNode(int x) : val(x), next(nullptr) {}
5+
};
6+
/**
7+
* Definition for singly-linked list.
8+
* struct ListNode {
9+
* int val;
10+
* ListNode *next;
11+
* ListNode(int x) : val(x), next(NULL) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
ListNode* sortList(ListNode* head) {
17+
// last pivot current
18+
ListNode* middle = findMiddle(head, nullptr, head->val);
19+
return nullptr;
20+
}
21+
22+
ListNode* findMiddle(ListNode* start, ListNode* end, int pivot)
23+
{
24+
return nullptr;
25+
}
26+
27+
};

151_Reverse_Words_in_a_String.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <memory>
3+
using namespace std;
4+
class Solution {
5+
public:
6+
string reverseWords(string s) {
7+
8+
std::string output;
9+
output.reserve(s.size());
10+
11+
int outputIndex = 0;
12+
int inputIndex = s.size() - 1;
13+
int endIndex = 0;
14+
int startIndex = 0;
15+
bool hasLast = false;
16+
while(true)
17+
{
18+
endIndex = findNextNoneSpacePos(s, inputIndex);
19+
if(endIndex >= 0)
20+
{
21+
if(hasLast)
22+
{
23+
output[outputIndex++] = ' ';
24+
}
25+
startIndex = findNextSpacePos(s, endIndex - 1);
26+
memcpy((void *) (output.data() + outputIndex), s.data() + startIndex + 1, endIndex - startIndex);
27+
outputIndex += (endIndex - startIndex);
28+
inputIndex = startIndex - 1;
29+
hasLast = true;
30+
}
31+
else
32+
{
33+
break;
34+
}
35+
}
36+
output[outputIndex] = '\0';
37+
return output;
38+
}
39+
40+
int findNextSpacePos(string const& s, int index)
41+
{
42+
43+
for(int i = index; i >= 0; i--)
44+
{
45+
if(s[i] == ' ')
46+
{
47+
return i;
48+
}
49+
}
50+
return -1;
51+
}
52+
int findNextNoneSpacePos(string const& s, int index)
53+
{
54+
for(int i = index; i >= 0; i--)
55+
{
56+
if(s[i] != ' ')
57+
{
58+
return i;
59+
}
60+
}
61+
62+
return -1;
63+
}
64+
65+
};
66+
int main()
67+
{
68+
std::cout << Solution().reverseWords("a good example").c_str() << std::endl;
69+
}

0 commit comments

Comments
 (0)