1、输入字符长的最后一个word的长度
返回输入字符串中最后一个word的长度,word之间以space分割,如果最后一个word不存在,返回0。
测试用例: input output
“a ” 1
“ a” 1
“a ” 1
“Helloworld ” 5
“” 0(1) 解决方案1(4ms )
int lengthOfLastWord(string s)
{
int pos = 0;
char space = ' ';
vector<string> tmp;
pos = s.find(space);
while( pos != string::npos )
{
string str = s.substr(0,pos);
if ( !(str.size() == 1 && str[0] == ' ' || str.size() == 0 ) )
tmp.push_back(str);
s = s.substr(pos+1);
pos = s.find(space);
}
if ( !(s.size() == 1 && s[0] == ' ' || s.size() == 0 ) )
tmp.push_back(s);
if (tmp.size() == 0)
return 0;
return tmp[tmp.size()-1].size();
}
(2) 解决方案2(0 ms )
int lengthOfLastWord(char* s)
{
int lastLen = 0;
char* p = s + strlen(s) -1;
while(p>=s && isspace(*p)) p--;
while(p>=s && !isspace(*(p--))) lastLen++;
return lastLen;
}
2、二叉查找树的两个节点的最低祖先问题
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
TreeNode* lowestComAnces(TreeNode* root, TreeNode* minNode, TreeNode* maxNode) {
if ( root->val >= minNode->val && root->val <= maxNode->val )
return root;
if ( root->val >= minNode->val && root->val >= maxNode->val )
return lowestComAnces( root->left,minNode,maxNode);
if ( root->val <= minNode->val && root->val <= maxNode->val )
return lowestComAnces( root->right,minNode,maxNode);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if ( root == NULL )
return NULL;
TreeNode *minNode,*maxNode;
if ( p->val > q->val)
{
minNode = q;
maxNode = p;
}
else
{
minNode = p;
maxNode = q;
}
TreeNode* comAnceNode;
comAnceNode = lowestComAnces(root,minNode,maxNode);
return comAnceNode;
}
3、N!末尾0的个数问题
给定一个整数n,求其结果末尾0的个数
int trailingZeroes(int n)
{
int cnt = 0;
while (n)
{
n /= 5;
cnt += n;
}
return cnt;
}
4、判断丑数问题
给定一个整数,判断其是否是丑数,其中丑数是指其因子只有2,3,5的整数,如6,8是丑数,但是14不是丑数,因为它有因子7。注意,1也是丑数(1) 实现代码:
bool isUgly2(int num)
{
if (num <= 0)
return false;
while(num != 0 && num%5 == 0)num /= 5;
while(num != 0 && num%3 == 0)num /= 3;
while(num != 0 && num%2 == 0)num /= 2;
if(num ==1 )
return true;
else
return false;
}
(2)输出第n个丑数(一个变种问题)
int isUgly(int num)
{
if ( num < 0)
return false;
int i = 0,tmp;
int pos2 = 0,pos3 = 0,pos5 = 0;
int *array = new int[num];
array[0] = 1;
for ( i = 1;i< num; i++)
{
tmp = min3(2*array[pos2],3*array[pos3],5*array[pos5]);
if ( tmp > num )
break;
array[i] = tmp;
if ( array[i] == 2*array[pos2] )
pos2++;
if ( array[i] == 3*array[pos3] )
pos3++;
if ( array[i] == 5*array[pos5] )
pos5++;
}
return array[num-1];
}
5、给定一个非负数,将其各个位上的数字相加,然后将结果上的各个数字也相加,直至相加结果为单个数字,返回该单个数字
如输入38,则 3+ 8 = 11, 1 + 1 = 2,返回2。
(1) 解决方案1:(12 ms)---递归实现
int addDigits(int num)
{
if ( num >= 0 && num < 10)
return num;
int numAdd = 0;
while(num > 0)
{
numAdd += num%10;
num /= 10;
}
return addDigits(numAdd);
}
(2)通过求模运算:
原理:
对一个整数N = (100a + 10b + c)%9 = (a+99a+b+9b+c)%9 = (a+b+c)%9
对于单个数字(0~9),N = (N-1+1) = (N-1)%9+1int addDigits(int num)
{
return (num - 1) % 9 + 1;
}
6、给定一颗二叉树,返回所有的从根节点到树子节点的路径,以string类型保存。
1
/ \
2 3
\
5
返回: ["1->2->5", "1->3"]
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void getPath( TreeNode* root, string path,vector<string> *paths)
{
if ( !root->left && !root->right)
{
paths->push_back(path);
return;
}
if( root->left)
getPath( root->left, path+ "->" + to_string( root->left->val),paths );
if (root->right)
getPath( root->right, path+ "->" + to_string(root->right->val),paths );
}
vector<string> binaryTreePaths(TreeNode* root)
{
vector<string> strPaths;
if ( root == NULL)
return strPaths;
string path = to_string(root->val) ;
getPath(root,path,&strPaths);
return strPaths;
}<strong>
</strong>
7、给定两个字符串s和t,判断两个字符串是否含有向右相同的字符,只是打乱了顺序。
如 s = "anagram", t ="nagaram", return true.s ="rat", t ="car", return false.
(1)解决方案1(80 ms)
bool isAnagram(string s, string t)
{
if ( s.size() != t.size() )
return false;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if ( s == t )
return true;
else
return false;
}
(2) 解决方案2(12 ms )
bool isAnagram(string s, string t)
{
if (s.length() != t.length()) return false;
int n = s.length();
int counts[26] = {0};
for (int i = 0; i < n; i++)
{
counts[s[i] - 'a']++;
counts[t[i] - 'a']--;
}
for (int i = 0; i < 26; i++)
if (counts[i]) return false;
return true;
}
8、翻转整数值
(1)解决方案1(12 ms)
int reverse(int x)
{
bool isNegetive = false;
if( x < 0 )
{
isNegetive = true;
x = -x;
}
string strX = to_string(x);
int len = strX.length();
for ( int i = 0; i < len/2; i++ )
{
char tmp = strX[len-1-i];
strX[len-1-i] = strX[i];
strX[i] = tmp;
}
long long rst = stoll(strX);
if ( isNegetive )
rst = -rst;
return (rst<INT_MIN || rst>INT_MAX) ? 0 : rst;
}
(2) 解决方案2(5ms)
int reverse(int x)
{
long long val = 0;
do
{
val = val * 10 + x % 10;
x /= 10;
} while (x);
return (val > INT_MAX || val < INT_MIN) ? 0 : val;
}