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
>>> 2 ** 3 #2^3
8
>>> 22 % 8
6
>>> 22 / 8
2.75
>>> 22 // 8
2
>>> 'Alice' + 'Bob'
'AliceBob'
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
# comments
print('Hello world!')
print('What is your name?')
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
>>> str(0)
'0'
>>> str(-3.14)
'-3.14'
>>> int('42')
42
>>> int('-99')
-99
>>> int(1.25)
1
>>> float('3.14')
3.14
>>> float(10)
10.0
>>> int('3.14')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
round(…) round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
>>> round(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> round(10.)
10
>>> round(10.0)
10
>>> round(10.1)
10
>>> round(10.123)
10
>>> round(10.123,1)
10.1
>>> round(10.123,2)
10.12
>>> round(10.123,3)
10.123