Skip to content

Commit 50bb2b0

Browse files
committed
2 parents 9ee54db + 94f5377 commit 50bb2b0

File tree

27 files changed

+771
-100
lines changed

27 files changed

+771
-100
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LeetCode C++ Solutions
55

66
| Title | Solution | Add Date | Difficulty |
77
| ----- | -------- | -------- | ---------- |
8+
|[Read N Characters Given Read4](https://oj.leetcode.com/problems/read-n-characters-given-read4/) :books: | [C++](./src/readNCharactersGivenRead4/readNCharactersGivenRead4.cpp)|2014/11/19|Easy|
9+
|[Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) :books: | [C++](./src/binaryTreeUpsideDown/binaryTreeUpsideDown.cpp)|2014/11/16|Medium|
10+
|[Min Stack](https://oj.leetcode.com/problems/min-stack/)| [C++](./src/minStack/minStack.cpp)|2014/11/09|Easy|
811
|[Find Minimum in Rotated Sorted Array II](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| [C++](./src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.II.cpp)|2014/10/20|Hard|
912
|[Find Minimum in Rotated Sorted Array](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [C++](./src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.cpp)|2014/10/15|Medium|
1013
|[Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/)| [C++](./src/maximumProductSubarray/maximumProductSubarray.cpp)|2014/9/23|Medium|

scripts/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Some Scripts
2+
============
3+
4+
`basic.sh` - use to insert the basic comment at the beginning of the srouce file
5+
6+
`problem.sh` - grab the problem descriptions from oj.leetcode.com and add it into the soure file.
7+

scripts/basic.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
time=`stat -c %x $2 | awk '{print \$1}'`
4+
5+
sed -i '1i\'"// Source : $1" $2
6+
sed -i '2i\'"// Author : Hao Chen" $2
7+
sed -i '3i\'"// Date : $time\n" $2

scripts/problem.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
grep Source $1 | sed -e "s/\/\/ Source : //g" | xargs -I'{}' xidel {} -q -e "css('div.question-content')" | grep -v ' ' |sed '/^$/N;/^\n$/D' |sed 's/^/* /' | sed "1i/*$(printf '%.0s*' {0..80}) \n* " | sed "\$a $(printf '%.0s*' {0..80})*/\n" > /tmp/tmp.txt; sed -i '4 r /tmp/tmp.txt' $1

src/addTwoNumbers/addTwoNumbers.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Solution {
2626
public:
2727
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
2828
int x=0, y=0, z=0;
29-
ListNode *h=NULL, *t=NULL;
29+
ListNode *h=NULL, **t=&h;
3030

3131
while (l1!=NULL || l2!=NULL){
3232
x = getValueAndMoveNext(&l1);
@@ -35,20 +35,15 @@ class Solution {
3535
z = z + x + y;
3636

3737
ListNode *node = new ListNode(z%10);
38-
if (h == NULL){
39-
h = t = node;
40-
}else{
41-
t->next = node;
42-
t = node;
43-
}
38+
*t = node;
39+
t = (&node->next);
4440

4541
z = z/10;
4642
}
4743

48-
while (z > 0) {
44+
if (z > 0) {
4945
ListNode *node = new ListNode(z%10);
50-
t->next = node;
51-
z = z/10;
46+
*t = node;
5247
}
5348

5449
return h;

src/anagrams/anagrams.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ class Solution {
1414
public:
1515
vector<string> anagrams(vector<string> &strs) {
1616
vector<string> result;
17-
map<string, string> m;
17+
map<string, int> m;
1818
for(int i=0; i<strs.size(); i++){
1919
string word = strs[i];
20-
sort(word.begin(), word.end());
20+
//sort it can easy to check they are anagrams or not
21+
sort(word.begin(), word.end());
2122
if (m.find(word)==m.end()){
22-
m[word] = strs[i];
23+
m[word] = i;
2324
}else{
24-
if (m[word]!="#"){
25-
result.push_back(m[word]);
26-
m[word]="#";
25+
if (m[word]>=0){
26+
result.push_back(strs[m[word]]);
27+
m[word]=-1;
2728
}
2829
result.push_back(strs[i]);
2930
}

src/binaryTreeMaximumPathSum/binaryTreeMaximumPathSum.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int maxPathSum(TreeNode *root, int& maxSum ) {
5858
}
5959

6060
int maxPathSum(TreeNode *root) {
61-
#define INT_MIN -2147483648
61+
#define INT_MIN (-2147483647 - 1)
6262
int maxSum = INT_MIN;
6363
maxPathSum(root, maxSum);
6464
return maxSum;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Source : https://oj.leetcode.com/problems/binary-tree-upside-down/
2+
// Author : Hao Chen
3+
// Date : 2014-11-17
4+
5+
/**********************************************************************************
6+
* Given a binary tree where all the right nodes are either leaf nodes with
7+
* a sibling (a left node that shares the same parent node) or empty,
8+
*
9+
* Flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes.
10+
* Return the new root.
11+
*
12+
* For example:
13+
* Given a binary tree {1,2,3,4,5},
14+
* 1
15+
* / \
16+
* 2 3
17+
* / \
18+
* 4 5
19+
* return the root of the binary tree [4,5,2,#,#,3,1].
20+
* 4
21+
* / \
22+
* 5 2
23+
* / \
24+
* 3 1
25+
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
26+
*
27+
**********************************************************************************/
28+
29+
30+
/**
31+
* Definition for binary tree
32+
* struct TreeNode {
33+
* int val;
34+
* TreeNode *left;
35+
* TreeNode *right;
36+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
TreeNode *upsideDownBinaryTree(TreeNode *root) {
42+
//using a dummy node to help to store the new tree
43+
TreeNode dummy(0);
44+
TreeNode *head = &dummy, *left=NULL, *right=NULL;
45+
46+
while ( root!=NULL ) {
47+
//find the right & left
48+
left = root->right;
49+
right = root;
50+
51+
//move root the next
52+
root = root->left;
53+
54+
//replace the right with current root
55+
right->left = head->left;
56+
right->right = head->right;
57+
58+
//move the dummy to the root
59+
dummy.right = right;
60+
dummy.left = left;
61+
62+
//reset the head to the root
63+
head = &dummy;
64+
65+
}
66+
67+
return head->right;
68+
}
69+
};

src/distinctSubsequences/distinctSubsequences.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ int numDistinct1(string S, string T) {
9191
//=====================
9292
//
9393
// The idea here is an optimization of above idea
94-
// (It might be difficult to understand if you don't know the above idea)
94+
// (It might be difficult to understand if you don't understand the above idea)
9595
//
9696
// For example:
9797
//
9898
// S = "abbbc" T="abb"
99-
// posMap = { [a]={0}, [b]={1,2} }
99+
// posMap = { [a]={0}, [b]={2,1} } <- the map of T's every char.
100100
// numOfSubSeq = {1, 0, 0, 0 }
101101
//
102102
// S[0] is 'a', pos is 0, numOfSubSeq = {1, 0+1, 0, 0};

src/firstMissingPositive/firstMissingPositive.cpp

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,47 @@
1414
*
1515
*
1616
**********************************************************************************/
17-
17+
#include <stdlib.h>
18+
#include <time.h>
1819
#include <iostream>
1920
#include <map>
2021
using namespace std;
2122

2223
#define INT_MAX 2147483647
2324

2425
/*
25-
* The idea is simple:
26+
* Idea:
27+
*
28+
* We can move the num to the place whcih the index is the num.
29+
*
30+
* for example, (considering the array is zero-based.
31+
* 1 => A[0], 2 => A[1], 3=>A[2]
32+
*
33+
* Then, we can go through the array check the i+1 == A[i], if not ,just return i+1;
34+
*
35+
* This solution comes from StackOverflow.com
36+
* http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
37+
*/
38+
int firstMissingPositive_move(int A[], int n) {
39+
if (n<=0) return 1;
40+
int num;
41+
for(int i=0; i<n; i++) {
42+
num = A[i];
43+
while (num>0 && num<n && A[num-1]!=num) {
44+
swap(A[i], A[num-1]);
45+
num = A[i];
46+
}
47+
}
48+
for (int i=0; i<n; i++){
49+
if (i+1 != A[i]){
50+
return i+1;
51+
}
52+
}
53+
return n+1;
54+
}
55+
56+
/*
57+
* The idea is simple:
2658
*
2759
* 1) put all of number into a map.
2860
* 2) for each number a[i] in array, remove its continous number in the map
@@ -36,8 +68,10 @@ using namespace std;
3668
*
3769
* However, we missed 1, so, we have to add dummy number 0 whatever.
3870
*
71+
* NOTE: this solution is not constant space slution!!!!
72+
*
3973
*/
40-
int firstMissingPositive(int A[], int n) {
74+
int firstMissingPositive_map(int A[], int n) {
4175
map<int, int> cache;
4276
for(int i=0; i<n; i++){
4377
cache[A[i]] = i;
@@ -51,13 +85,13 @@ int firstMissingPositive(int A[], int n) {
5185
int miss = INT_MAX;
5286
int x;
5387
for (int i=-1; i<n && cache.size()>0; i++){
54-
88+
5589
if (i == -1){
5690
x = 0; //checking dummy
5791
}else{
5892
x = A[i];
5993
}
60-
94+
6195
if ( cache.find(x)==cache.end() ){
6296
continue;
6397
}
@@ -78,32 +112,66 @@ int firstMissingPositive(int A[], int n) {
78112
miss = num;
79113
}
80114
}
81-
115+
82116

83117
return miss;
84118
}
85119

120+
int firstMissingPositive(int A[], int n) {
121+
srand(time(0));
122+
if (rand()%2){
123+
return firstMissingPositive_move(A, n);
124+
}
125+
return firstMissingPositive_map(A, n);
126+
}
127+
128+
129+
void printArray(int a[], int n){
130+
cout << "[ ";
131+
for(int i=0; i<n-1; i++) {
132+
cout << a[i] << ", ";
133+
}
134+
cout << a[n-1] << " ]";
135+
}
136+
137+
void Test(int a[], int n, int expected) {
138+
printArray(a, n);
139+
int ret = firstMissingPositive(a, n);
140+
cout << "\t missed = " << ret << " " << (ret==expected?"passed!":"failed!") << endl;
141+
//printArray(a, n);
142+
//cout <<endl;
143+
}
144+
86145
int main()
87146
{
88-
#define TEST(a) cout << firstMissingPositive(a, sizeof(a)/sizeof(int)) << endl
147+
#define TEST(a, e) Test(a, sizeof(a)/sizeof(int), e)
148+
149+
int a0[]={1};
150+
TEST(a0, 2);
89151

90152
int a1[]={1,2,0};
91-
TEST(a1);
153+
TEST(a1, 3);
92154

93155
int a2[]={3,4,-1,1};
94-
TEST(a2);
156+
TEST(a2, 2);
95157

96158
int a3[]={1000,-1};
97-
TEST(a3);
159+
TEST(a3, 1);
98160

99161
int a4[]={1000, 200};
100-
TEST(a4);
162+
TEST(a4, 1);
101163

102-
int a5[]={2,5,4,3,-1};
103-
TEST(a5);
164+
int a5[]={2,5,3,-1};
165+
TEST(a5, 1);
104166

105167
int a6[]={1, 100};
106-
TEST(a6);
168+
TEST(a6, 2);
169+
170+
int a7[]={7,8,9,11};
171+
TEST(a7, 1);
172+
173+
int a8[]={4,3,2,1};
174+
TEST(a8, 5);
107175

108176
return 0;
109177
}

0 commit comments

Comments
 (0)