https://leetcode.com/problems/fizz-buzz/description/
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> vecRet;
for (int i = 1; i <= n; i++)
{
string str;
if(i % 3 == 0 && i % 15 == 0)
{
str = "FizzBuzz";
}
else if(i % 3 == 0)
{
str = "Fizz";
}
else if(i % 5 == 0)
{
str = "Buzz";
}
else
{
str = to_string(i);
}
vecRet.push_back(str);
}
return vecRet;
}
};
https://leetcode.com/problems/longest-palindrome/description/
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example "Aa"
is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
Example:
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
#include <iostream>
using namespace std;
class Solution {
public:
int longestPalindrome(string s) {
int hashtable[128] = { 0 };
for (char c : s)
{
hashtable[c]++;
}
bool bHasOdd = false;
int iResult = 0;
for (int i = 0; i < 128; i++)
{
if (hashtable[i] % 2 == 0)
{
iResult += hashtable[i];
}
else
{
iResult += hashtable[i] - 1;
bHasOdd = true;
}
}
return bHasOdd ? iResult + 1 : iResult;
}
};
int main()
{
Solution o;
int iRet = o.longestPalindrome("abccccdd");
cout << iRet << endl;
return 0;
}
https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
Example 1:
Input:
26
Output:
"1a"
Example 2:
Input:
-1
Output:
"ffffffff"
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
char toChar(unsigned int iNum)
{
if (iNum < 10)
{
return iNum + '0';
}
else
{
return iNum - 10 + 'a';
}
}
string toHex(int num) {
string strRet;
unsigned int uiNum = num;
unsigned int iRemainder = uiNum;
unsigned int iComplement = 0;
while (1)
{
if (iRemainder < 16)
{
strRet.append(1, toChar(iRemainder));
break;
}
else
{
iComplement = iRemainder % 16;
iRemainder = iRemainder / 16;
strRet.append(1, toChar(iComplement));
}
}
int i = 0;
int j = strRet.size() - 1;
while (i < j)
{
int temp = strRet[i];
strRet[i] = strRet[j];
strRet[j] = temp;
i++;
j--;
}
return strRet;
}
};
int main()
{
Solution o;
string strRet = o.toHex(-1);
cout << strRet << endl;
return 0;
}
https://leetcode.com/problems/sum-of-left-leaves/description/
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool bIsLeaves(TreeNode * node)
{
if (NULL == node)
{
return false;
}
if (node->left == NULL && node->right == NULL)
{
return true;
}
else
{
return false;
}
}
int sumOfLeftLeavesRecur(TreeNode* root) {
if (NULL == root)
{
return 0;
}
if (bIsLeaves(root))
{
return root->val;
}
int iLeftSum = sumOfLeftLeavesRecur(root->left);
int iRightSum = sumOfLeftLeavesRecur(root->right);
if (bIsLeaves(root->right))
{
iRightSum -= root->right->val;
}
return iLeftSum + iRightSum;
}
int sumOfLeftLeaves(TreeNode* root) {
if (NULL == root)
{
return 0;
}
if (bIsLeaves(root))
{
return 0;
}
return sumOfLeftLeavesRecur(root);
}
};
int main()
{
return 0;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(NULL == root)
{
return 0;
}
int iSum = 0;
if(root->left != NULL && (root->left->left == NULL && root->left->right == NULL))
{
iSum = root->left->val;
}
return iSum + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
print('Hello')
print('World')
print('Hello', end='')
print('World')
print('Hello', end=' ')
print('World')
print('cats', 'dogs', 'mice')
print('cats', 'dogs', 'mice', sep=',')
Hello
World
HelloWorld
Hello World
cats dogs mice
cats,dogs,mice
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')
My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)
for i in range(12, 16):
print(i)
12
13
14
15
for i in range(0, 10, 2):
print(i)
0
2
4
6
8
for i in range(5, -1, -1):
print(i)
5
4
3
2
1
0