标题: ignore~ this is only for me [打印本页] 作者: 楼外青楼山外山 时间: 2013-06-17 20:27 标题: ignore~ this is only for me class Solution {
public:
vector<vector<int> > func(vector<int>& num)
{
if (num.empty()) {
return vector<vector<int> > ();
}
delete
ignore作者: 楼外青楼山外山 时间: 2013-06-18 20:12
class Solution {
public:
void merge(int A[], int m, int B[], int n)
{
int i = m-1;
int j = n-1;
int k = m+n-1;
while (i >= 0 && j >= 0) {
A[k--] = A[i]<B[j]? B[j--]: A[i--];
}
while (j >= 0) A[k--] = B[j--];
}
};
int main()
{
int A[] = {1, 3, 5, 6, 8, 0, 0, 0, 0, 0};
int B[] = {2, 4, 7, 9, 11};
Solution solve;
solve.merge(A, 5, B, 5);
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-18 21:12
Partition List
class Solution {
public:
ListNode* partition(ListNode* head, int val)
{
if (head == 0) return 0;
ListNode* pre = 0;
ListNode* pIndex = head;
while (pIndex->val <= val) {
pre = pIndex;
pIndex = pIndex->next;
}
if (pIndex == 0) return head;
pIndex = pIndex->next;
if (pIndex == 0) return head;
while (pIndex) {
ListNode* preIndex = 0;
for (; pIndex->val >= val; preIndex = pIndex,
pIndex = pIndex->next);
if (pIndex) {
ListNode* tmp = pIndex->next;
preIndex->next = tmp;
if (pre) {
pIndex->next = pre->next;
pre->next = pIndex;
} else {
pIndex->next = head;
head = pIndex;
}
pIndex = tmp;
}
}
return head;
}
};
int main()
{
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(4);
ListNode* node3 = new ListNode(3);
ListNode* node4 = new ListNode(2);
ListNode* node5 = new ListNode(5);
ListNode* node6 = new ListNode(2);
int main()
{
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(1);
ListNode* node3 = new ListNode(2);
ListNode* node4 = new ListNode(3);
ListNode* node5 = new ListNode(3);
ListNode* node6 = new ListNode(4);
ListNode* node7 = new ListNode(5);
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-19 15:01
Remove Duplicates from Sorted List II:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head)
{
if (head == 0) return 0;
ListNode* pre = 0;
ListNode* pNode = head;
if (pNode == 0) return head;
while (pNode) {
while (pNode && pNode->next && pNode->val != pNode->next->val) {
pre = pNode;
pNode = pNode->next;
}
if (pNode->next == 0) return head;
for (; pNode->next && pNode->val == pNode->next->val; pNode = pNode->next);
if (pre) {
pre->next = pNode->next;
} else {
pre = pNode->next;
head = pre;
}
if (pre) pNode = pre->next;
}
}
};
int main()
{
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(2);
ListNode* node3 = new ListNode(3);
ListNode* node4 = new ListNode(3);
ListNode* node5 = new ListNode(4);
ListNode* node6 = new ListNode(4);
ListNode* node7 = new ListNode(5);
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-19 16:14
// Remove Duplicates from Sorted Array
class Solution {
public:
int removeDuplicates(int A[], int n)
{
int index = -1;
for (int i = 1; i < n; ++i) {
if (A[i] != A[i-1]) {
if (index != -1) A[index++] = A[i];
continue;
}
if (index == -1) index = i;
for (++i; i < n && A[i] == A[i-1]; ++i);
if (i >= n) return index;
A[index++] = A[i];
}
return index;
}
};
int main()
{
int A[] = {1,1,2,2,3,4,5,6,7,7,7,7,8,9,9,9};
Solution solve;
int ret = solve.removeDuplicates(A, sizeof(A)/sizeof(A[0]));
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-19 16:55
// Remove Duplicates from Sorted Array II
class Solution {
public:
int removeDuplicates(int A[], int n)
{
int index = -1;
for (int i = 1; i < n; ++i) {
if (A[i] != A[i-1]) {
if (index != -1) A[index++] = A[i];
continue;
}
if (i < n-1 && A[i] == A[i+1]) {
if (index != -1) A[index++] = A[i];
if (index == -1) index = ++i;
for (++i; i < n && A[i] == A[i-1]; ++i);
if (i < n) A[index++] = A[i];
continue;
}
if (index != -1) A[index++] = A[i];
}
return index;
}
};
int main()
{
int A[] = {1,1,1,2,2,3};
Solution solve;
int ret = solve.removeDuplicates(A, sizeof(A)/sizeof(A[0]));
A[ret] = 0;
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-20 11:46
// Longest Valid Parentheses:
class Solution {
public:
int longestValidParentheses(const string& str)
{
std::stack<int> token;
int max = 0;
int succesiveNum = 0;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == '(') {
token.push(str[i]);
} else if (str[i] == ')') {
if (token.empty()) {
if (succesiveNum) {
max = std::max(max, succesiveNum);
succesiveNum = 0;
}
continue;
}
token.pop();
++succesiveNum;
}
}
return std::max(max, succesiveNum) * 2;
}
};
int main()
{
//std::string str("))(((())))()())()()((()()(");
std::string str("))((((()()()()()))");
Solution solve;
int ret = solve.longestValidParentheses(str);
getchar();
return 0;
}作者: 楼外青楼山外山 时间: 2013-06-20 13:00
Reverse Integer:
class Solution {
public:
int reverse(int x)
{
int flag = 1;
if (x < 0) {
x = -x;
flag = -1;
}
int num = 0;
while (x) {
num = num*10 + x%10;
x /= 10;
}
return num * flag;
}
};
int main()
{
Solution solve;
int ret = solve.reverse(-12300);
class Solution {
public:
ListNode* addTwoNumbers2(const ListNode* a, const ListNode* b)
{
ListNode* pLink = 0;
ListNode* head = 0;
int carry = 0;
while (a && b) {
int node = a->val + b->val + carry;
ListNode* ptmp = new ListNode(node%10);
carry = (node > 9? 1: 0);
!pLink? (pLink = ptmp, head = pLink): (pLink->next = ptmp, pLink = ptmp);
a = a->next;
b = b->next;
}
const ListNode* ptr = a? a: b;
while (ptr) {
int node = ptr->val + carry;
ListNode* ptmp = new ListNode(node%10);
carry = (node > 9? 1: 0);
!pLink? (pLink = ptmp, head = pLink): (pLink->next = ptmp, pLink = ptmp);
ptr = ptr->next;
}
if (carry) {
ListNode* ptmp = new ListNode(carry);
pLink->next = ptmp;
}
return head;
}
ListNode* addTwoNumbers(const ListNode* a, const ListNode* b)
{
return this->addTwoNumbers_Recursive(a, b, 0);
}
ListNode* addTwoNumbers_Recursive(const ListNode* a, const ListNode* b,
int carry = 0)
{
if (!a && !b) {
if (!carry) return 0;
ListNode* pNode = new ListNode(carry);
return pNode;
}
class Solution {
public:
std::string longestPalindrome(const string& str)
{
int max = 0;
int max_s = 0;
for (int i = 1; i < str.size(); ++i) {
int len = 0;
int pos = countPalindrome(str, i-1, i, len);
if (max < len) {
max = len;
max_s = pos+1;
}
pos = countPalindrome(str, i-1, i+1, len);
if (max < len) {
max = len;
max_s = pos+1;
}
}
return str.substr(max_s, max);
}
int countPalindrome(const string& str, int s, int e, int& len)
{
while (s >= 0 && e < str.size()) {
if (str[s] == str[e]) {
--s;
++e;
continue;
}
len = e-s-1;
return s;
}
len = e-s-1;
return s;
}
};
int main()
{
std::string str("abcfcba123414321111111123414321abcfcba");
// std::string str("()()((");
Solution solve;
string ret = solve.longestPalindrome(str);
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
ListNode* pNode = head;
for (int i = 1; i < n && pNode; ++i, pNode = pNode->next);
if (pNode == 0) return 0;
ListNode* pre = head;
ListNode* parent = 0;
while (pNode->next) {
pNode = pNode->next;
parent = pre;
pre = pre->next;
}
if (parent) parent->next = pre->next;
else head = pre->next;
return head;
}
};
int main()
{
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(2);
ListNode* node3 = new ListNode(3);
ListNode* node4 = new ListNode(4);
ListNode* node5 = new ListNode(5);
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
Solution solve;
ListNode* ret = solve.removeNthFromEnd(node1, 2);
int main()
{
ListNode* head = new ListNode(1);
ListNode* node1 = new ListNode(2);
ListNode* node2 = new ListNode(3);
ListNode* node3 = new ListNode(4);
ListNode* node4 = new ListNode(5);
ListNode* node5 = new ListNode(6);
class Solution {
public:
int searchInsert(const int A[], int n, int target)
{
if (A[0] > target) return 0;
if (A[n-1] < target) return n;
int l = 0;
int r = n-1;
while (l <= r) {
int mid = (l + r)/2;
if (A[mid] == target) return mid;
else if (A[mid] < target) {
if (A[mid+1] >= target) return mid+1;
l = mid+1;
} else {
if (mid-1 >= 0 && A[mid-1] < target) return mid;
r = mid-1;
}
}
}
};
int main()
{
int A[] = {1,3,5,6};
Solution solve;
int ret = solve.searchInsert(A, sizeof(A)/sizeof(A[0]), 2);
vector<vector<int>> subs;
};作者: 楼外青楼山外山 时间: 2013-09-06 10:56
Given a collection of integers that might contain duplicates, S, return all possible subsets.
class Solution {
public:
vector<vector<int>> subsets(vector<int>& sub)
{
if (sub.empty()) {
return vector<vector<int>> ();
}
Remove Duplicates from Sorted Array
int removeDuplicates(int A[], int n)
{
int index = -1;
for (uint i = 1; i < n; ++i) {
if (A[i] == A[i-1]) {
if (index == -1) index = i;