Merge pull request #1 from Kiritow/master

merge
pull/46/head
JZFamily 2018-06-23 18:49:08 +08:00 committed by GitHub
commit 7b560d544c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 754 additions and 0 deletions

46
LeetCode-CN/1.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdlib.h>
#include <string.h>
typedef struct pack
{
int val;
int pos;
}pack;
int qcmp(const void* a, const void* b)
{
return ((pack*)a)->val - ((pack*)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
pack* p = (pack*)malloc(sizeof(pack)*numsSize);
for (int i = 0; i < numsSize; i++)
{
p[i].val = nums[i];
p[i].pos = i;
}
qsort(p, numsSize, sizeof(pack), qcmp);
int L = 0;
int R = numsSize - 1;
while (L < R)
{
int sum = p[L].val + p[R].val;
if (sum == target)
{
int* m = (int*)malloc(sizeof(int) * 2);
m[0] = p[L].pos;
m[1] = p[R].pos;
return m;
}
else if (sum < target)
{
L++;
}
else if (sum > target)
{
R--;
}
}
return NULL;
}

41
LeetCode-CN/1.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <algorithm>
bool cmp(const pair<int, int>& a, const pair<int, int>& b)
{
return a.first < b.first;
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int len = nums.size();
vector< pair<int, int> > vp;
for (int i = 0; i < len; i++)
{
vp.push_back(std::make_pair(nums[i], i));
}
sort(vp.begin(), vp.end(), cmp);
int L = 0;
int R = len - 1;
while (L < R)
{
int sum = vp[L].first + vp[R].first;
if (sum == target)
{
vector<int> v;
v.push_back(vp[L].second);
v.push_back(vp[R].second);
return v;
}
else if (sum < target)
{
L++;
}
else if (sum > target)
{
R--;
}
}
}
};

89
LeetCode-CN/146.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <cstdlib>
#include <cstring>
#include <unordered_map>
// DON'T USE STL LIST!! IT IS VERY SLOW!!!
struct MyList
{
int key, val;
MyList* pre;
MyList* next;
};
class LRUCache {
public:
LRUCache(int capacity) {
maxsz = capacity;
sz = 0;
head = NULL;
end = NULL;
}
int get(int key) {
std::unordered_map<int, MyList*>::iterator iter = xp.find(key);
if (iter != xp.end())
{
MyList* tnode = iter->second;
if (tnode != head)
{
tnode->pre->next = tnode->next;
if (tnode->next)
{
tnode->next->pre = tnode->pre;
}
else
{
end = tnode->pre;
}
tnode->next = head;
tnode->pre = NULL;
head->pre = tnode;
head = tnode;
}
return head->val;
}
else
{
return -1;
}
}
void put(int key, int value) {
if (get(key) != -1)
{
head->val = value;
}
else
{
MyList* xnode = (MyList*)malloc(sizeof(MyList));
xp.insert(std::make_pair(key, xnode));
xnode->key = key;
xnode->val = value;
xnode->pre = NULL;
xnode->next = head;
if(head) head->pre = xnode;
head = xnode;
if (end == NULL) end = head;
if (maxsz - sz)
{
sz++;
}
else
{
// keep sz
xp.erase(end->key);
end = end->pre;
free(end->next);
end->next = NULL;
}
}
}
private:
int maxsz;
int sz;
std::unordered_map<int, MyList*> xp;
MyList* head;
MyList* end;
};

75
LeetCode-CN/2.c Normal file
View File

@ -0,0 +1,75 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
typedef struct ListNode ListNode;
ListNode* BigIntegerAdd(ListNode* a, ListNode* b,
int* tmp)
{
ListNode* pa = a;
ListNode* pb = b;
int pos = 0;
while (pa != NULL && pb != NULL)
{
tmp[pos] = pa->val + pb->val;
pa = pa->next;
pb = pb->next;
pos++;
}
while (pa != NULL)
{
tmp[pos] = pa->val;
pa = pa->next;
pos++;
}
while (pb != NULL)
{
tmp[pos] = pb->val;
pb = pb->next;
pos++;
}
ListNode* head = NULL;
ListNode** pthis = &head;
for (int i = 0; i < pos; i++)
{
if (tmp[i] >= 10)
{
tmp[i + 1] += tmp[i] / 10;
tmp[i] %= 10;
}
(*pthis) = (ListNode*)malloc(sizeof(ListNode));
(*pthis)->val = tmp[i];
(*pthis)->next = NULL;
pthis = &((*pthis)->next);
}
if (tmp[pos] > 0)
{
(*pthis) = (ListNode*)malloc(sizeof(ListNode));
(*pthis)->val = tmp[pos];
(*pthis)->next = NULL;
pthis = &((*pthis)->next);
}
return head;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int tmp[1024];
memset(tmp, 0, sizeof(tmp));
return BigIntegerAdd(l1, l2, tmp);
}

66
LeetCode-CN/2.cpp Normal file
View File

@ -0,0 +1,66 @@
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
ListNode* BigIntegerAdd(ListNode* a, ListNode* b,
int* tmp)
{
ListNode* pa = a;
ListNode* pb = b;
int pos = 0;
while (pa != NULL && pb != NULL)
{
tmp[pos] = pa->val + pb->val;
pa = pa->next;
pb = pb->next;
pos++;
}
while (pa != NULL)
{
tmp[pos] = pa->val;
pa = pa->next;
pos++;
}
while (pb != NULL)
{
tmp[pos] = pb->val;
pb = pb->next;
pos++;
}
ListNode* head = NULL;
ListNode** pthis = &head;
for (int i = 0; i < pos; i++)
{
if (tmp[i] >= 10)
{
tmp[i + 1] += tmp[i] / 10;
tmp[i] %= 10;
}
(*pthis) = new ListNode(tmp[i]);
pthis = &((*pthis)->next);
}
if (tmp[pos] > 0)
{
(*pthis) = new ListNode(tmp[pos]);
pthis = &((*pthis)->next);
}
return head;
}
class Solution {
public:
ListNode * addTwoNumbers(ListNode* l1, ListNode* l2) {
int tmp[1024] = { 0 };
return BigIntegerAdd(l1, l2, tmp);
}
};

32
LeetCode-CN/225.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <list>
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
lst.push_back(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int t = lst.back();
lst.pop_back();
return t;
}
/** Get the top element. */
int top() {
return lst.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return lst.empty();
}
private:
std::list<int> lst;
};

70
LeetCode-CN/25.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <stack>
class Solution {
public:
ListNode * reverseKGroup(ListNode* head, int k) {
ListNode* seg_start = head;
ListNode* now = head;
ListNode* return_head = head;
ListNode* last_seg = NULL;
int passed = 0;
while (now != NULL)
{
passed++;
if (passed == k)
{
// Meet a segment
ListNode* seg_end = now;
// Change head
if (seg_start == head)
{
return_head = seg_end;
}
ListNode* p = seg_start;
std::stack<ListNode*> stk;
while (p != seg_end)
{
stk.push(p);
p = p->next;
}
// p is now seg_end
// checkpoint
ListNode* next_seg_start = seg_end->next;
while (!stk.empty())
{
p->next = stk.top();
stk.pop();
p = p->next;
}
// stack is empty. p is now seg_start.
// p->next now pointes to the orignal "next"
// set last segment
if (last_seg)
{
last_seg->next = seg_end;
}
last_seg = seg_start;
// set it to next_seg_start
p->next = next_seg_start;
seg_start = next_seg_start;
now = next_seg_start;
passed = 0;
// debug
//printList(return_head);
}
else
{
now = now->next;
}
}
return return_head;
}
};

38
LeetCode-CN/3.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <cstdlib>
#include <cstring>
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int sz = s.size();
int bin[256];
int maxlen = 0;
int clen = 0;
int L = 0;
while (L < sz)
{
memset(bin, 0, sizeof(int) * 256);
int i;
for (i = L; i < sz; i++)
{
if (bin[s[i]])
{
maxlen = clen > maxlen ? clen : maxlen;
clen = 0;
L++;
break;
}
else
{
bin[s[i]] = 1;
clen++;
}
}
if (i == sz)
{
maxlen = clen > maxlen ? clen : maxlen;
break;
}
}
return maxlen;
}
};

43
LeetCode-CN/32.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <algorithm>
#include <stack>
class Solution {
public:
int longestValidParentheses(string s) {
int len = s.size();
int maxp = 0;
stack<int> stk;
for (int i = 0; i < len; i++)
{
if (s[i] == '(')
{
stk.push(i);
}
else // if (s[i] == ')')
{
// Stack not empty, and symbol matches.
if (!stk.empty() && s[stk.top()]=='(')
{
// Pop matched pairs.
stk.pop();
if (stk.empty())
{
// From Start to i is a valid string.
maxp = std::max(i + 1, maxp);
}
else
{
// From stk.top()+1 to i is a valid string. Length: i-(stk.top()+1)+1
maxp = std::max(i - stk.top(), maxp);
}
}
else
{
// new start point (stub)
stk.push(i);
}
}
}
return maxp;
}
};

24
LeetCode-CN/3_beihe.cpp Normal file
View File

@ -0,0 +1,24 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, short> v;
int max_num = 0;
for (int i = 0; i < s.size(); ++i) {
if (v.find(s[i]) != v.end()) {
if (max_num < v.size()) {
max_num = v.size();
}
i = v.find(s[i])->second + 1;
v.clear();
}
if (i >= s.size()) {
break;
}
v[s[i]] = i;
if (max_num < v.size()) {
max_num = v.size();
}
}
return max_num;
}
};

39
LeetCode-CN/3_jzf.cpp Normal file
View File

@ -0,0 +1,39 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int num =1;
int max =0;
if(s.length()==1)
{
return 1;
}
for(size_t i =0;i<s.length();i++)
{
for(size_t j=i+1;j<s.length();j++)
{
int flag =0;
for(size_t k =i;k<j;k++)
{
if(s[j]==s[k])
{
flag =1;
break;
}
num++;
}
if(num>max)
{
max =num;
}
if(flag ==1)
{
break;
}
num =1;
}
num =1;
}
return max;
}
};

35
LeetCode-CN/3_puck.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <algorithm>
#include <string>
#include <set>
using std::string;
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int result{};
for (std::size_t i{}; i < s.size(); ++i)
{
string tmp_str = s.substr(i);
int cu_length{};
std::set<char> set_except;
for (auto ch : tmp_str)
{
if (set_except.end() == set_except.find(ch))
{
set_except.insert(ch);
++cu_length;
}
else
{
break;
}
}
result = std::max(result, cu_length);
}
return result;
}
};

77
LeetCode-CN/4.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <algorithm>
class Solution {
public:
void forward_until(vector<int>& nums1, vector<int>& nums2,
int lenA,int lenB,
int& idxA, int& idxB, int& passed, int midLen)
{
while (idxA < lenA&&idxB < lenB && passed < midLen)
{
if (nums1[idxA] < nums2[idxB])
{
idxA++;
passed++;
}
else
{
idxB++;
passed++;
}
}
while (idxA < lenA&&passed < midLen)
{
idxA++;
passed++;
}
while (idxB < lenB&&passed < midLen)
{
idxB++;
passed++;
}
}
int getCurrent(vector<int>& nums1, vector<int>& nums2,
int lenA,int lenB,
int idxA, int idxB)
{
if (idxA < lenA&&idxB < lenB)
{
return std::min(nums1[idxA], nums2[idxB]);
}
else if (idxA < lenA)
{
return nums1[idxA];
}
else
{
return nums2[idxB];
}
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int lenA = nums1.size();
int lenB = nums2.size();
int sumLen = lenA + lenB;
int midLen = sumLen / 2 - (1 - (sumLen % 2));
int idxA = 0;
int idxB = 0;
int passed = 0;
forward_until(nums1, nums2, lenA, lenB, idxA, idxB, passed, midLen);
if (sumLen % 2)
{
return getCurrent(nums1, nums2, lenA, lenB, idxA, idxB);
}
else
{
int step1 = getCurrent(nums1, nums2, lenA, lenB, idxA, idxB);
forward_until(nums1, nums2, lenA, lenB, idxA, idxB, passed, midLen + 1);
int step2 = getCurrent(nums1, nums2, lenA, lenB, idxA, idxB);
return (step1 + step2) / 2.0;
}
}
};

36
LeetCode-CN/494.cpp Normal file
View File

@ -0,0 +1,36 @@
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
if (S < -1000 || S>1000) return 0;
int sz = nums.size();
int msz = sizeof(int) * 2048;
int _bin[2048] = { 0 };
int _xbin[2048] = { 0 };
int* p = _bin;
int* q = _xbin;
memset(p, 0, msz);
p[1000] = 1;
for (int i = 0; i < sz; i++)
{
memset(q, 0, msz);
for (int j = 0; j < 2048; j++)
{
if (p[j])
{
//printf("p[%d]=%d\n", p[j]);
// now=j-1000;
int L = j - nums[i]; // now-nums[i]+1000
int R = j + nums[i];
q[L] += p[j];
q[R] += p[j];
//printf("q[%d]=%d\n", L, q[L]);
//printf("q[%d]=%d\n", R, q[R]);
}
}
std::swap(p, q);
}
return p[S + 1000];
}
};

4
LeetCode-CN/627.sql Normal file
View File

@ -0,0 +1,4 @@
update salary set sex = case sex
when 'f' then 'm'
when 'm' then 'f'
end

2
LeetCode-CN/627_jzf.sql Normal file
View File

@ -0,0 +1,2 @@
update salary
set sex = case when sex ='m' then 'f' else 'm' end;

37
LeetCode-CN/9.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <cstdio>
class Solution {
public:
bool isPalindrome(int x) {
/*
// Converting to string is too slow!!
char buff[64] = { 0 };
sprintf(buff, "%d", x);
int len = strlen(buff);
int L = 0, R = len - 1;
while (L < R)
{
if (buff[L] != buff[R]) return false;
++L;
--R;
}
return true;
*/
if (x < 0) return false;
int buff[64] = { 0 };
int c = 0;
int tx = x;
while (tx > 0)
{
buff[c++] = tx % 10;
tx /= 10;
}
int L = 0, R = c - 1;
while (L < R)
{
if (buff[L] != buff[R]) return false;
++L;
--R;
}
return true;
}
};