14. Longest Common Prefix
题目描述
Write a function to find the longest common prefix string amongst an array of strings.
代码实现
法一:
class Solution {
public:
string Compare(string s1, string s2) {
int len1 = s1.length(), len2 = s2.length(), slen = len1 > len2? len2:len1;
string res;
for(int i = 0; i < slen; i++) {
if(s1[i] == s2[i]) res += s1[i];
else break;
}
return res;
}
string longestCommonPrefix(vector<string>& strs) {
int slen = strs.size();
string res;
if(!slen) return res;
res = strs[0];
for(int i = 1; i < slen; i++) {
res = Compare(res, strs[i]);
}
return res;
}
};
法二:这道题目就是对字符串进行排序,那么就需要比较第一个和最后字符串即可得到最后的结果。
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int i, j, n = strs.size();
if (n == 0) return "";
sort(strs.begin() ,strs.begin() + n);
for (j = 0; j < strs[0].size() && j < strs[n - 1].size() && strs[0][j] == strs[n - 1][j]; j++);
return strs[0].substr(0, j);
}
};
法三:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string prefix = "";
for(int idx=0; strs.size()>0; prefix+=strs[0][idx], idx++)
for(int i=0; i<strs.size(); i++)
if(idx >= strs[i].size() ||(i > 0 && strs[i][idx] != strs[i-1][idx]))
return prefix;
return prefix;
}
};
441. Arranging Coins
题目描述
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
Given n, find the total number of full staircase rows that can be formed.
n is a non-negative integer and fits within the range of a 32-bit signed integer.
Example 1:
n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
代码实现
class Solution {
public:
int arrangeCoins(int n) {
int incom = 0;
if(n == 1) return 1;
long long int tmp = (long long int)n;
for(long long int i = 1; i <= ceil(sqrt(tmp << 1)); i++) {
if(i * (i+1) > (tmp << 1))
return i-1;
}
return 0;
}
};
这道题目就是数学题目,通过简化之后可以得到:
class Solution {
public:
int arrangeCoins(int n) {
return floor(-0.5+sqrt((double)2*n+0.25));
}
};
415. Add Strings
题目描述
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both num1 and num2 is < 5100.
- Both num1 and num2 contains only digits 0-9.
- Both num1 and num2 does not contain any leading zero.
- You must not use any built-in BigInteger library or convert the inputs to integer directly.
代码实现
class Solution {
public:
string addStrings(string num1, string num2) {
int n1 = num1.length() - 1, n2 = num2.length() - 1;
int rlen = n1 > n2?n2:n1;
string res;
int rem = 0;
for(; n1 >= 0 || n2 >= 0 || rem; n1--, n2--) {
int tmp = ((n1 >= 0)?(num1[n1] - '0'):0) + ((n2 >= 0)?(num2[n2] - '0'):0) + rem;
res = char(tmp%10 + '0') + res;
rem = tmp/10;
}
return res;
}
};
367. Valid Perfect Square
题目描述
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Returns: True
Example 2:
Input: 14
Returns: False
代码实现
使用1+3+5+7+9……
class Solution {
public:
bool isPerfectSquare(int num) {
for (int i = 1; num > 0; i += 2)
num -= i;
return !num;
}
};
使用数据类型的方法:
class Solution {
public:
bool isPerfectSquare(int num) {
return ceil(sqrt(num)) == floor(sqrt(num));
}
};
使用二分计算:
class Solution {
public:
bool isPerfectSquare(int num) {
int l = 0, r = num;
while(l <= r) {
long long mid = (l + r) / 2;
if(mid * mid < num) l = mid + 1;
else if(mid * mid > num) r = mid - 1;
else return true;
}
return false;
}
};