https://leetcode.com/problems/reverse-string/description/
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string reverseString(string s)
{
int iStart = 0;
int iEnd = s.size() - 1;
while (iStart < iEnd)
{
vSwap(s[iStart], s[iEnd]);
++iStart;
--iEnd;
}
return s;
}
void vSwap(char & a, char & b)
{
char temp = a;
a = b;
b = temp;
}
};
int main()
{
return 0;
}
https://leetcode.com/problems/odd-even-linked-list/description/
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Given 1->2->3->4->5->NULL
,
return 1->3->5->2->4->NULL
.
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* oddEvenList(ListNode* head)
{
if (NULL == head)
{
return NULL;
}
if (head->next == NULL)
{
return head;
}
if (head->next->next == NULL)
{
return head;
}
ListNode * odd = head;
ListNode * even = head->next;
ListNode * t = even->next;
while (t != NULL)
{
even->next = t->next;
t->next = odd->next;
odd->next = t;
odd = t;
even = even->next;
if (even == NULL)
{
break;
}
t = even->next;
}
return head;
}
ListNode * createList(vector<int> & vecValue)
{
ListNode * head = NULL;
ListNode * tail = NULL;
vector<int>::iterator iter = vecValue.begin();
while (iter != vecValue.end())
{
ListNode * pNew = new ListNode(*iter);
if (head == NULL)
{
head = pNew;
tail = pNew;
}
else
{
tail->next = pNew;
tail = pNew;
}
++iter;
}
return head;
}
void vShowList(ListNode * pList)
{
while (pList != NULL)
{
cout << pList->val << " ";
pList = pList->next;
}
cout << endl;
}
};
int main()
{
Solution o;
vector<int> vecValue = { 1, 2, 3 };
ListNode * pList = o.createList(vecValue);
ListNode * pResult = o.oddEvenList(pList);
o.vShowList(pResult);
return 0;
}
https://leetcode.com/problems/range-sum-query-immutable/description/
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
Note:
#include <iostream>
#include <vector>
using namespace std;
class NumArray {
public:
NumArray(vector<int> nums) : m_Sum(nums.size()+1, 0) {
for (int i = 0; i < nums.size(); i++)
{
m_Sum[i + 1] = m_Sum[i] + nums[i];
}
}
int sumRange(int i, int j) {
return m_Sum[j + 1] - m_Sum[i];
}
private:
vector<int> m_Sum;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
int main()
{
return 0;
}
class NumArray {
public:
vector<int> sums;
NumArray(vector<int> &nums) {
int i;
int iSize = nums.size();
sums = nums;
for (i = 1; i < iSize; i++)
{
sums[i] += sums[i-1];
}
}
int sumRange(int i, int j) {
printf("i = %d, j = %d, ", i, j);
if(i == 0)
{
printf("result = %d\n", sums[j]);
return sums[j];
}
else
{
printf("result = %d\n", sums[j] - sums[i-1]);
return sums[j] - sums[i-1];
}
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
https://leetcode.com/problems/bulls-and-cows/description/
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.
For example:
Secret number: "1807"
Friend's guess: "7810"
Hint: 1
bull and 3
cows. (The bull is 8
, the cows are 0
, 1
and 7
.)
Write a function to return a hint according to the secret number and friend’s guess, use A
to indicate the bulls and B
to indicate the cows. In the above example, your function should return "1A3B"
.
Please note that both secret number and friend’s guess may contain duplicate digits, for example:
Secret number: "1123"
Friend's guess: "0111"
In this case, the 1st 1
in friend’s guess is a bull, the 2nd or 3rd 1
is a cow, and your function should return "1A1B"
.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
class Solution {
public:
string getHint(string secret, string guess)
{
int iBullNum = 0;
int iCowNum = 0;
int aiNum[10] = { 0 };
int aiNumGuess[10] = { 0 };
for (int i = 0; i < secret.size(); i++)
{
if (secret[i] == guess[i])
{
iBullNum++;
}
else
{
aiNum[secret[i] - '0']++;
aiNumGuess[guess[i] - '0']++;
}
}
for (int i = 0; i < 10; i++)
{
if (aiNumGuess[i] > 0)
{
int iMin = aiNumGuess[i] < aiNum[i] ? aiNumGuess[i] : aiNum[i];
iCowNum += iMin;
}
}
char acResult[100] = { 0 };
sprintf(acResult, "%dA%dB", iBullNum, iCowNum);
string result(acResult);
return result;
}
};
int main()
{
Solution o;
string result = o.getHint("1122", "1222");
return 0;
}
https://leetcode.com/problems/nim-game/description/
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
class Solution {
public:
bool canWinNim(int n) {
return n % 4 != 0;
}
};
https://leetcode.com/problems/word-pattern/description/
Given a pattern
and a string str
, find if str
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in str
.
Examples:
"abba"
, str = "dog cat cat dog"
should return true."abba"
, str = "dog cat cat fish"
should return false."aaaa"
, str = "dog cat cat dog"
should return false."abba"
, str = "dog dog dog dog"
should return false.Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string str)
{
map<char, string> mapPatternToString;
map<string, char> mapStringToPattern;
vector<string> vecSubString = splitString(str, ' ');
if (vecSubString.size() != pattern.size())
{
return false;
}
for (int i = 0; i < vecSubString.size(); i++)
{
char c = pattern[i];
map<char, string>::iterator iterPTS = mapPatternToString.find(c);
if (iterPTS == mapPatternToString.end())
{
mapPatternToString.insert(std::pair<char, string>(c, vecSubString[i]));
}
else
{
if (strcmp(iterPTS->second.c_str(), vecSubString[i].c_str()) != 0)
{
return false;
}
}
map<string, char>::iterator iterSTP = mapStringToPattern.find(vecSubString[i]);
if (iterSTP == mapStringToPattern.end())
{
mapStringToPattern.insert(std::pair<string, char>(vecSubString[i], c));
}
else
{
if (iterSTP->second != c)
{
return false;
}
}
}
return true;
}
vector<string> splitString(string str, char sep)
{
vector<string> result;
string strSubString;
const char * pcStr = str.data();
for (int i = 0; i <= str.size(); i++)
{
if (i == str.size() || str[i] == ' ')
{
strSubString.append(1, '\0');
result.push_back(strSubString);
strSubString.clear();
}
else
{
strSubString.append(1, str[i]);
}
}
return result;
}
};
int main()
{
Solution o;
bool bRet = o.wordPattern("jquery", "jquery");
return 0;
}