Skip to content

Commit e6fdade

Browse files
committed
new problems
1 parent 8d8baaa commit e6fdade

File tree

5 files changed

+346
-1
lines changed

5 files changed

+346
-1
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+
|[Intersection of Two Linked Lists](https://oj.leetcode.com/problems/intersection-of-two-linked-lists/) | [C++](./src/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.cpp)|2014/11/27|Easy|
9+
|[Longest Substring with At Most Two Distinct Characters](https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) :books: | [C++](./src/longestSubstringWithAtMostTwoDistinctCharacters/longestSubstringWithAtMostTwoDistinctCharacters.cpp)|2014/11/25|Hard|
10+
|[Read N Characters Given Read4 II - Call multiple times](https://oj.leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) :books: | [C++](./src/readNCharactersGivenRead4/readNCharactersGivenRead4.II.cpp)|2014/11/23|Hard|
811
|[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|
912
|[Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) :books: | [C++](./src/binaryTreeUpsideDown/binaryTreeUpsideDown.cpp)|2014/11/16|Medium|
1013
|[Min Stack](https://oj.leetcode.com/problems/min-stack/)| [C++](./src/minStack/minStack.cpp)|2014/11/09|Easy|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Source : https://oj.leetcode.com/problems/intersection-of-two-linked-lists/
2+
// Author : Hao Chen
3+
// Date : 2014-12-01
4+
5+
/**********************************************************************************
6+
*
7+
* Write a program to find the node at which the intersection of two singly linked lists begins.
8+
*
9+
* For example, the following two linked lists:
10+
*
11+
*
12+
* A: a1 → a2
13+
* ↘
14+
* c1 → c2 → c3
15+
* ↗
16+
* B: b1 → b2 → b3
17+
*
18+
* begin to intersect at node c1.
19+
*
20+
* Notes:
21+
*
22+
* If the two linked lists have no intersection at all, return null.
23+
* The linked lists must retain their original structure after the function returns.
24+
* You may assume there are no cycles anywhere in the entire linked structure.
25+
* Your code should preferably run in O(n) time and use only O(1) memory.
26+
*
27+
**********************************************************************************/
28+
29+
/**
30+
* Definition for singly-linked list.
31+
* struct ListNode {
32+
* int val;
33+
* ListNode *next;
34+
* ListNode(int x) : val(x), next(NULL) {}
35+
* };
36+
*/
37+
class Solution {
38+
public:
39+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
40+
41+
//caculate the length of each List
42+
int lenA = getListLength(headA);
43+
int lenB = getListLength(headB);
44+
45+
if (lenA<=0 || lenB<=0 ) return NULL;
46+
47+
//let List A is the longest List;
48+
if (lenA < lenB){
49+
swap(headA, headB);
50+
}
51+
52+
//move head of List A, make both of Lists are same length
53+
for (int i=0; i<abs(lenA-lenB); i++){
54+
headA = headA->next;
55+
}
56+
57+
//synced travel both of Lists and check their nodes are same or not
58+
while (headA != headB){
59+
headA = headA->next;
60+
headB = headB->next;
61+
}
62+
63+
return headA;
64+
}
65+
private:
66+
inline int getListLength(ListNode *head){
67+
int len=0;
68+
while(head!=NULL){
69+
head = head->next;
70+
len++;
71+
}
72+
return len;
73+
}
74+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Source : https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
2+
// Author : Hao Chen
3+
// Date : 2014-12-01
4+
5+
/*
6+
* Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
7+
*
8+
* For example, Given s = “eceba”,
9+
*
10+
* T is "ece" which its length is 3.
11+
*/
12+
13+
14+
15+
#include <iostream>
16+
#include <string>
17+
using namespace std;
18+
19+
20+
/*
21+
* Idea:
22+
*
23+
* 1) Using a map to count every chars.
24+
* 2) Using a 'cnt' to count the current distinct chars.
25+
* 3) if `cnt > 2`, then go through the previous substring to remove one char
26+
*
27+
* The following algorithm run-time complexity is O(n^2)
28+
*
29+
* This solution can be easy to extend to "find the length of longest substring with at most K distinct char(s)"
30+
*/
31+
int lengthOfLongestSubstringTwoDistinct(string s) {
32+
int maxLen = 0;
33+
char charMap[256] = {0};
34+
int wordCnt = 0;
35+
int start = 0;
36+
37+
for(int i=0; i<s.size(); i++){
38+
if ( charMap[s[i]]++ == 0 ){
39+
wordCnt++;
40+
}
41+
while (wordCnt>2){
42+
charMap[s[start]]--;
43+
if (charMap[s[start]]==0){
44+
wordCnt--;
45+
}
46+
start++;
47+
}
48+
maxLen = max(maxLen, i - start + 1);
49+
}
50+
51+
return maxLen;
52+
}
53+
54+
55+
int main(int argc, char** argv)
56+
{
57+
string s = "eceba";
58+
if (argc>1){
59+
s = argv[1];
60+
}
61+
cout << s << " : " << lengthOfLongestSubstringTwoDistinct(s) << endl;
62+
63+
return 0;
64+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Source : https://oj.leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/
2+
// Author : Hao Chen
3+
// Date : 2014-12-01
4+
5+
6+
/*
7+
* The API: int read4(char *buf) reads 4 characters at a time from a file.
8+
*
9+
* The return value is the actual number of characters read.
10+
* For example, it returns 3 if there is only 3 characters left in the file.
11+
*
12+
* By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
13+
*
14+
* Note:
15+
* The read function may be called multiple times.
16+
*/
17+
18+
19+
/*
20+
* The key to solve this problem is:
21+
*
22+
* You have to buffer the extra data return from `read4()` API, which is for next call for `read()` .
23+
*/
24+
25+
#include <stdio.h>
26+
#include <string.h>
27+
// Forward declaration of the read4 API.
28+
static char input[64];
29+
static char *p = input;
30+
31+
int read4(char *buf) {
32+
33+
int s=0;
34+
for (; s<4 && *p!='\0'; s++){
35+
buf[s] = *p;
36+
p++;
37+
}
38+
return s;
39+
}
40+
41+
42+
class RingBuffer {
43+
44+
public:
45+
RingBuffer(int cap=4): _capacity(cap) {
46+
_size = 0;
47+
48+
_pbuf = new char[_capacity];
49+
_p = _pbuf;
50+
_pend = _pbuf + _capacity - 1;
51+
}
52+
~RingBuffer(){
53+
delete _pbuf;
54+
}
55+
int read(char* buf, int n){
56+
//empty, nothing can read
57+
if (_size==0) return 0;
58+
59+
//read `n` or all
60+
int s;
61+
for (s=0; s<n && s<_size; s++){
62+
buf[s] = *_p;
63+
_p = (_p < _pend) ? _p + 1 : _pbuf;
64+
}
65+
_size -= s;
66+
return s;
67+
}
68+
69+
int write(char* buf, int n) {
70+
//full, cannot write
71+
if (_size >= _capacity) return 0;
72+
73+
//write `n` or all
74+
int s;
75+
char *p = _p + _size;
76+
for (s=0; s < _capacity - _size && s < n; s++){
77+
*p = buf[s];
78+
p = (p < _pend) ? p + 1 : _pbuf;
79+
}
80+
_size += s;
81+
return s;
82+
}
83+
84+
private:
85+
const int _capacity;
86+
int _size;
87+
88+
char* _pbuf;
89+
char* _p;
90+
char* _pend;
91+
92+
};
93+
94+
class Solution {
95+
public:
96+
/**
97+
* @param buf Destination buffer
98+
* @param n Maximum number of characters to read
99+
* @return The number of characters read
100+
*/
101+
int read(char *buf, int n) {
102+
int buf_size = 0;
103+
int api_size = 0;
104+
105+
//read the data from the RingBuffer at first
106+
buf_size = _ring_buffer.read(buf, n);
107+
if (buf_size == n) {
108+
return n;
109+
}
110+
n = n - buf_size;
111+
112+
//read the data from the API at second
113+
api_size = read1(buf + buf_size, n);
114+
115+
return buf_size + api_size;
116+
}
117+
118+
// The read1 implementation is from the `Read N Characters Given Read4` problem
119+
// Add the feature - write the extra char(s) into RingBuffer
120+
int read1(char *buf, int n) {
121+
char _buf[4]; // the buffer for read4()
122+
int _n = 0; // the return for read4()
123+
int len = 0; // total buffer read from read4()
124+
int size = 0; // how many bytes need be copied from `_buf` to `buf`
125+
while((_n = read4(_buf)) > 0){
126+
//check the space of `buf` whether full or not
127+
size = len + _n > n ? n-len : _n;
128+
//write the extra char(s) into RingBuffer
129+
if (len + _n > n ){
130+
_ring_buffer.write(_buf + size, _n - size);
131+
}
132+
strncpy(buf+len, _buf, size);
133+
len += size;
134+
//buffer is full
135+
if (len>=n){
136+
break;
137+
}
138+
}
139+
return len;
140+
}
141+
142+
143+
private:
144+
RingBuffer _ring_buffer;
145+
};
146+
147+
148+
int main()
149+
{
150+
Solution s;
151+
const int buf_len = 10;
152+
char buf[buf_len]={0};
153+
#define SET_INPUT(s) strcpy(input, s); p=input; printf("input data = \"%s\"\n", p);
154+
#define CLEAR_BUFFER() memset(buf, 0, sizeof(buf)/sizeof(buf[0]) )
155+
#define READ(s, buf, n) CLEAR_BUFFER(); printf("read(%d) => ",n); s.read(buf, n); printf("%s\n", buf);
156+
157+
printf("-----------------------------\n");
158+
SET_INPUT("1234abcd1234abcdxyz");
159+
for(int i=0; i<5; i++){
160+
READ(s, buf, 4);
161+
}
162+
printf("-----------------------------\n");
163+
SET_INPUT("ab");
164+
READ(s, buf, 1);
165+
READ(s, buf, 2);
166+
167+
printf("-----------------------------\n");
168+
SET_INPUT("abcde");
169+
for(int i=0; i<5; i++){
170+
READ(s, buf, 1);
171+
}
172+
printf("-----------------------------\n");
173+
SET_INPUT("1234xyzabcd00");
174+
READ(s, buf, 4);
175+
for(int i=0; i<3; i++){
176+
READ(s, buf, 1);
177+
}
178+
READ(s, buf, 4);
179+
printf("-----------------------------\n");
180+
return 0;
181+
}

src/readNCharactersGivenRead4/readNCharactersGivenRead4.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,35 @@ class Solution {
2929
* @return The number of characters read
3030
*/
3131
int read(char *buf, int n) {
32+
srand(time(0));
33+
if (rand()%2){
34+
return read1(buf, n);
35+
}
36+
return read2(buf, n);
37+
}
38+
39+
//read the data in-place. potential out-of-boundary issue
40+
int read1(char *buf, int n) {
41+
int len = 0;
42+
int size = 0;
43+
44+
// `buf` could be accessed out-of-boundary
45+
while(len <=n && (size = read4(buf))>0){
46+
size = len + size > n ? n - len : size;
47+
len += size;
48+
buf += size;
49+
}
50+
return len;
51+
}
52+
53+
//using a temp-buffer to avoid peotential out-of-boundary issue
54+
int read2(char *buf, int n) {
3255
char _buf[4]; // the buffer for read4()
3356
int _n = 0; // the return for read4()
3457
int len = 0; // total buffer read from read4()
3558
int size = 0; // how many bytes need be copied from `_buf` to `buf`
3659
while((_n = read4(_buf)) > 0){
37-
//check the space of `buf` whether enough or not
60+
//check the space of `buf` whether full or not
3861
size = len + _n > n ? n-len : _n;
3962
strncpy(buf+len, _buf, size);
4063
len += size;

0 commit comments

Comments
 (0)