leetcode刷题记录贴
leetcode吧
全部回复
仅看楼主
level 1
sanyue83 楼主
开始在leetcode刷题,开个贴记录一下我的答案和进度。
2016年02月24日 05点02分 1
level 1
sanyue83 楼主
从上到下刷题,所以是倒序的。
334:Increasing Triplet Subsequence
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size() == 0) return false;
int first = nums[0];
int second;
bool valid = false;
for (unsigned i = 1; i < nums.size(); ++i)
{
if(nums[i] <= first)
{
first = nums[i];
}
else if (!valid)
{
second = nums[i];
valid = true;
}
else if (valid && nums[i] <= second)
{
second = nums[i];
}
else
{
return true;
}
}
return false;
}
};
2016年02月24日 05点02分 2
level 1
sanyue83 楼主
332. Reconstruct Itinerary
使用priority_queue没有必要,因为从一个点出发,最多只有两条path,所以只要记住最小的连个目的地就可以了,以后在优化吧。
程序运行时间40ms,击败了74%的提交者。
class Solution {
public:
vector<string> findItinerary(vector<pair<string, string>> tickets) {
for (auto t : tickets)
{
t_map[t.first].push(t.second);
}
list<string> result;
f("JFK",result);
vector<string> r;
for (auto i : result)
{
r.push_back(i);
};
return move(r);
}
void f(const string &from, list<string> & r)
{
r.push_back(from);
list<string> result1;
int i = 0;
while (t_map[from].size() != 0)
{
auto &next = t_map[from];
string subpath = next.top();
next.pop();
i == 0 ? f(subpath, result1) : f(subpath, r);
i ++;
}
r.splice(r.end(), result1);
}
private:
unordered_map<string, priority_queue<string, vector<string>, greater<string> > > t_map;
};
2016年02月24日 05点02分 3
level 1
sanyue83 楼主
331 Verify Preorder Serialization of a Binary Tree
class Solution {
public:
bool isValidSerialization(string preorder) {
m_str = &preorder;
m_index = 0;
return isValid() && (m_index == preorder.size() + 1);
}
bool isValid()
{
if (m_str->size() <= m_index) return false;
if ((*m_str)[m_index] == '#')
{
m_index += 2;
return true;
}
while((*m_str)[m_index++] != ',');
bool isLeftValid = isValid();
if (isLeftValid)
{
return isValid();
}
return false;
}
private:
string *m_str;
size_t m_index;
};
2016年02月24日 05点02分 4
level 1
sanyue83 楼主
330 Patching Array
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
size_t pitch = 1;
int count = 0;
for (size_t i = 0; i < nums.size(); ++i)
{
while (pitch < nums[i])
{
count++;
pitch += pitch;
if (n < pitch) return count;
}
pitch += nums[i];
if (n < pitch) return count;
}
while (pitch <= n)
{
count++;
pitch += pitch;
if (n < pitch) return count;
}
}
};
2016年02月24日 05点02分 5
level 1
sanyue83 楼主
329 Longest Increasing Path in a Matrix
很普通的做法,竟然击败了 98%的提交者 [汗]
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
raw = matrix.size();
if (raw == 0) return 0;
col = matrix[0].size();
if (col == 0) return 0;
p = &matrix;
counts = new int[raw * col];
memset(counts, 0, raw * col * sizeof(int));
int result = 1;
for (size_t i = 0; i < raw; ++i)
{
for (size_t j = 0; j < col; ++j)
{
int temp = path(i, j);
result = result < temp ? temp : result;
}
}
delete []counts;
return result;
}
int path(int i, int j)
{
int t = counts[i * col + j];
if (t != 0) return t;
t = 1;
//up
if (i - 1 >= 0)
{
if ((*p)[i - 1][j] > (*p)[i][j])
{
int n = path(i - 1, j);
t = t <= n ? n + 1 : t;
}
}
//down
if (i + 1 < raw)
{
if ((*p)[i + 1][j] > (*p)[i][j])
{
int n = path(i+1, j);
t = t <= n ? n + 1 : t;
}
}
// left
if (j - 1 >= 0)
{
if ((*p)[i][j - 1] > (*p)[i][j])
{
int n = path(i, j - 1);
t = t <= n ? n + 1 : t;
}
}
//right
if (j + 1 < col)
{
if ((*p)[i][j + 1] > (*p)[i][j])
{
int n = path(i, j + 1);
t = t <= n ? n + 1 : t;
}
}
counts[i * col + j] = t;
return t;
}
private:
int raw;
int col;
int * counts;
vector<vector<int>> *p;
};
2016年02月24日 05点02分 6
level 1
sanyue83 楼主
328. Odd Even Linked List
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == 0 || head->next == 0) return head;
ListNode *odd, *oddend;
ListNode *even, *evenend;
odd = oddend = head;
even = evenend = head->next;
head = evenend->next;
while(head)
{
oddend->next = head;
oddend = head;
evenend->next = head->next;
evenend = head->next;
if (evenend == 0) break;
head = evenend->next;
}
oddend->next = even;
return odd;
}
};
2016年02月24日 05点02分 7
level 2
一起
2016年04月10日 09点04分 8
level 1
我建议你去CSDN 上记录一下。
2016年04月15日 07点04分 9
1