Skip to content

Commit 3185c5a

Browse files
committed
622
1 parent cadb13e commit 3185c5a

9 files changed

+571
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
3+
Given a linked list, remove the n-th node from the end of list and return its head.
4+
5+
Example:
6+
7+
Given linked list: 1->2->3->4->5, and n = 2.
8+
9+
After removing the second node from the end, the linked list becomes 1->2->3->5.
10+
Note:
11+
12+
Given n will always be valid.
13+
14+
Follow up:
15+
16+
Could you do this in one pass?
17+
18+
19+
Solution one need to go through the list and record the length O(N);
20+
Solution two using fast and slow pointer;
21+
22+
*/
23+
24+
/**
25+
* Definition for singly-linked list.
26+
* struct ListNode {
27+
* int val;
28+
* ListNode *next;
29+
* ListNode() : val(0), next(nullptr) {}
30+
* ListNode(int x) : val(x), next(nullptr) {}
31+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
32+
* };
33+
*/
34+
class Solution {
35+
public:
36+
ListNode* removeNthFromEnd(ListNode* head, int n) {
37+
ListNode *temp = head, *prev = nullptr;
38+
int i = 0, j = 0;
39+
while ( temp != nullptr )
40+
{
41+
temp = temp->next;
42+
++i;
43+
}
44+
temp = head;
45+
if ( i == n ) return temp->next;
46+
while ( j++ < i - n )
47+
{
48+
prev = temp;
49+
temp = temp->next;
50+
}
51+
prev->next = temp->next;
52+
return head;
53+
}
54+
};
55+
56+
57+
class Solution {
58+
public:
59+
ListNode* removeNthFromEnd(ListNode* head, int n) {
60+
ListNode *temp = head, *prev = head;
61+
for ( int i = 0; i < n; ++i )
62+
{
63+
temp = temp->next;
64+
}
65+
if ( !temp ) return head->next;
66+
while ( temp->next )
67+
{
68+
prev = prev->next;
69+
temp = temp->next;
70+
}
71+
prev->next = prev->next->next;
72+
return head;
73+
}
74+
};

22. Generate Parentheses.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3+
4+
For example, given n = 3, a solution set is:
5+
6+
[
7+
"((()))",
8+
"(()())",
9+
"(())()",
10+
"()(())",
11+
"()()()"
12+
]
13+
14+
Solution one is DFS, recursive
15+
Two using set to get rid of repeated element
16+
17+
*/
18+
19+
class Solution {
20+
public:
21+
vector<string> generateParenthesis(int n) {
22+
vector<string> res;
23+
if ( n == 0 ) return res;
24+
AddP( n, n , "", res );
25+
return res;
26+
}
27+
void AddP( int l, int r, string out, vector<string>& res )
28+
{
29+
if ( l > r ) return;
30+
if ( l == 0 && r == 0 )
31+
{
32+
res.push_back( out );
33+
return;
34+
}
35+
if ( l > 0 ) AddP( l - 1, r, out + '(', res );
36+
if ( r > 0 ) AddP( l, r - 1, out + ')', res );
37+
}
38+
};
39+
40+
41+
class Solution {
42+
public:
43+
vector<string> generateParenthesis(int n) {
44+
set<string> res;
45+
if ( n == 0 ) res.insert( "" );
46+
else
47+
{
48+
vector<string> temp( generateParenthesis( n - 1 ) );
49+
for ( auto &a: temp )
50+
{
51+
for ( int j = 0; j < a.size(); ++j )
52+
{
53+
if ( a[j] == '(' )
54+
{
55+
a.insert(a.begin() + j + 1, '(' );
56+
a.insert(a.begin() + j + 2, ')' );
57+
res.insert( a );
58+
a.erase( a.begin() + j + 1, a.begin() + j + 3 );
59+
}
60+
61+
}
62+
res.insert( "()" + a );
63+
}
64+
}
65+
return vector<string>( res.begin(), res.end() );
66+
}
67+
};

344. Reverse String.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Write a function that reverses a string. The input string is given as an array of characters char[].
3+
4+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
6+
You may assume all the characters consist of printable ascii characters.
7+
8+
9+
10+
Example 1:
11+
12+
Input: ["h","e","l","l","o"]
13+
Output: ["o","l","l","e","h"]
14+
Example 2:
15+
16+
Input: ["H","a","n","n","a","h"]
17+
Output: ["h","a","n","n","a","H"]
18+
19+
First solution using swap from start -> mid
20+
Solution two using XOR
21+
*/
22+
class Solution {
23+
public:
24+
void reverseString(vector<char>& s) {
25+
int i = 0, j = s.size() - 1;
26+
while ( i < j )
27+
{
28+
swap( s[i++], s[j--] );
29+
}
30+
31+
}
32+
};
33+
34+
class Solution {
35+
public:
36+
void reverseString(vector<char>& s) {
37+
int i = 0, j = s.size() - 1;
38+
while ( i < j )
39+
{
40+
s[i] = s[i] ^ s[j];
41+
s[j] = s[i] ^ s[j];
42+
s[i] = s[i] ^ s[j];
43+
++i; --j;
44+
}
45+
46+
}
47+
};

345. Reverse Vowels of a String.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Write a function that takes a string as input and reverse only the vowels of a string.
3+
4+
Example 1:
5+
6+
Input: "hello"
7+
Output: "holle"
8+
Example 2:
9+
10+
Input: "leetcode"
11+
Output: "leotcede"
12+
Note:
13+
The vowels does not include the letter "y".
14+
15+
16+
Create a dictionary to check whether it is a vowel;
17+
*/
18+
// a e i o u
19+
class Solution {
20+
public:
21+
string reverseVowels(string s) {
22+
int i = 0, j = s.size() - 1;
23+
unordered_set<char> s1 = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
24+
while ( i < j )
25+
{
26+
if ( s1.count( s[i] ) > 0 && s1.count( s[j] ) > 0 )
27+
{
28+
swap( s[i], s[j] );
29+
++i; --j;
30+
}
31+
while ( s1.count( s[i] ) == 0 && i < j) ++i;
32+
while ( s1.count( s[j] ) == 0 && i < j) --j;
33+
}
34+
return s;
35+
}
36+
};
37+
38+
39+
// a e i o u
40+
class Solution {
41+
public:
42+
string reverseVowels(string s) {
43+
int i = 0, j = s.size() - 1;
44+
while ( i < j )
45+
{
46+
i = s.find_first_of( "aeiouAEIOU", i );
47+
j = s.find_last_of( "aeiouAEIOU", j );
48+
if ( i < j ) swap( s[i++], s[j--]);
49+
}
50+
return s;
51+
}
52+
};

541. Reverse String II.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
4+
Example:
5+
Input: s = "abcdefg", k = 2
6+
Output: "bacdfeg"
7+
Restrictions:
8+
The string consists of lower English letters only.
9+
Length of the given string and k will in the range [1, 10000]
10+
11+
Solution discuss different possibilities
12+
*/
13+
class Solution {
14+
public:
15+
string reverseStr(string s, int k) {
16+
int i = 0, size = s.size() - 1;
17+
if ( size < k )
18+
{
19+
while( i < size ) swap( s[i++], s[size--] );
20+
return s;
21+
}
22+
if ( size < 2 * k )
23+
{
24+
size = k - 1;
25+
while( i < size ) swap( s[i++], s[size--] );
26+
return s;
27+
}
28+
for ( int j = 0; j < size; j += 2 * k )
29+
{
30+
i = j;
31+
int b = min ( size, j + k - 1 );
32+
while ( i < b )
33+
{
34+
swap( s[i++], s[b--] );
35+
}
36+
37+
}
38+
return s;
39+
}
40+
};
41+
42+
43+
//Optimized, first couple of lines are redundant
44+
class Solution {
45+
public:
46+
string reverseStr(string s, int k) {
47+
int i = 0, size = s.size() - 1;
48+
for ( int j = 0; j < size; j += 2 * k )
49+
{
50+
i = j;
51+
int b = min ( size, j + k - 1 );
52+
while ( i < b )
53+
{
54+
swap( s[i++], s[b--] );
55+
}
56+
57+
}
58+
return s;
59+
}
60+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
3+
4+
Example 1:
5+
Input: "Let's take LeetCode contest"
6+
Output: "s'teL ekat edoCteeL tsetnoc"
7+
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
8+
9+
Solution one using iterative
10+
Solution two using istringstream
11+
*/
12+
class Solution {
13+
public:
14+
string reverseWords(string s) {
15+
if ( s == "" ) return s;
16+
for ( int i = 0; i < s.size() - 1; ++i )
17+
{
18+
int j = i;
19+
while ( s[i] != ' ' && i != s.size() - 1 ) ++i;
20+
if ( s[i] == ' ' ) reverse( s.begin() + j, s.begin() + i );
21+
else reverse( s.begin() + j, s.end() );
22+
}
23+
return s;
24+
}
25+
};
26+
27+
class Solution {
28+
public:
29+
string reverseWords(string s) {
30+
if ( s == "" ) return s;
31+
string res = "", t = "";
32+
istringstream is( s );
33+
while ( is >> t )
34+
{
35+
int size = t.size() - 1;
36+
int i = 0;
37+
while ( i < size )
38+
{
39+
swap( t[i++], t[size--] );
40+
}
41+
res = res + t + " ";
42+
}
43+
res.pop_back();
44+
return res;
45+
}
46+
};

0 commit comments

Comments
 (0)