文章目錄
41. 和爲S的兩個數字
a+b=sum,a和b越遠乘積越小
- a+b=sum;a+n+b-n=sum;(a+n)(b-n)=ab+n(b-a)>ab
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> ret;
int l = 0;
int r = array.size()-1;
while(l < r)
{
int n = array[l] + array[r];
if(n == sum)
{
ret.push_back(array[l]);
ret.push_back(array[r]);
return ret;
}else if(n < sum){
l++;
}else
r--;
}
return ret;
}
};
42. 左旋轉字符串
法一:
- 字符拼接
class Solution {
public:
string LeftRotateString(string str, int n) {
string ret;
if(n == 0 || str.length() == 0)
return str;
n = n%str.length();
int len = str.length();
ret = str.substr(n, len-n+1);
ret += str.substr(0, n);
return ret;
}
};
法二:
- 三次字符翻轉(總轉一次、前k個數轉一次、後n-k個數轉一次)
class Solution {
public:
string LeftRotateString(string str, int n) {
if(str.length()==0 || n==0)
return str;
int len = str.length();
n %= str.length();
reverse(str.begin(), str.end());
reverse(str.begin(), str.begin()+len-n);
reverse(str.begin()+len-n, str.end());
return str;
}
};
43. 翻轉單詞順序列
class Solution {
public:
string ReverseSentence(string str) {
int len = str.length();
if(len == 0)
return str;
int begin = 0;
int index = str.find(" ", begin); //沒找到,返回 -1
while(index != -1)
{
s.push(str.substr(begin, index-begin));
begin = index + 1;
index = str.find(" ", begin);
}
string ret = str.substr(begin);
while(!s.empty())
{
ret += " " + s.top();
s.pop();
}
return ret;
}
private:
stack<string> s;
};
44. 撲克牌順子
class Solution {
public:
bool IsContinuous( vector<int> numbers ) {
if(numbers.size() == 0)
return false;
sort(numbers.begin(), numbers.end());
int king = 0;
int index = 0;
while(index < numbers.size()-1)
{
if(numbers[index] == 0)
{
king++;
index++;
continue;
}
if(numbers[index]+1 == numbers[index+1]){
index++;
continue;
}
else if(king > 0){
king--;
numbers[index]++;
}
else
return false;
}
return true;
}
};
45. 圓圈中最後剩下的數
約瑟夫環
class Solution {
public:
int LastRemaining_Solution(int n, int m)
{
if(n<1 || m<1)
return -1;
v = vector<bool>(n, false);
int index = -1;
int step = 0;
int count = n;
while(count > 0)
{
index++; //指向已刪除的下個元素
index = (index==n)?0:index;
if(v[index])
continue;
step++;
if(step == m)
{
step = 0;
count--;
v[index] = true;
}
}
return index;
}
private:
vector<bool> v;
};
46. 求1+2+3+…+n
&& 的短路性質模擬 if
class Solution {
public:
int Sum_Solution(int n) {
int ret = n;
n>0 && (ret += Sum_Solution(n-1)) > 0;
return ret;
}
};
47. 不用加減乘除做加法
法一:自增、自減
class Solution {
public:
int Add(int num1, int num2)
{
if(num1 == 0)
return num2;
else if(num1 > 0){
while(num1-- != 0)
num2++;
}else{
while(num1++ != 0)
num2--;
}
return num2;
}
};
法二:
相加 ===> 異或
進位 ===> 相與、左移一位
class Solution {
public:
int Add(int num1, int num2)
{
while( num2 != 0 )
{
int sum = num1 ^ num2;
int carray = (num1 & num2) << 1;
num1 = sum;
num2 = carray;
}
return num1;
}
};
48. 把字符串轉換成整數
class Solution {
public:
int StrToInt(string str) {
int len = str.length();
if(len == 0)
return 0;
long long ret = 0;
int symbol = (str[0]=='-')?-1:1;
for(int i=(str[0]=='-' || str[0]=='+')?1:0; i<len; i++)
{
if(str[i] < '0' || str[i] > '9')
return 0;
ret = ret * 10 + str[i] - '0';
}
return ret*symbol;
}
};
49. 數組中重複的數字
法一:
class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
if(length == 0 || !numbers)
return false;
v = vector<bool>(length, false);
for(int i=0; i<length; i++)
{
if(v[numbers[i]])
{
*duplication = numbers[i];
return true;
}else
v[numbers[i]] = true;
}
return false;
}
private:
vector<bool> v;
};
法二:
numbers[numbers[i] % len] += n, 若再次被訪問則肯定大於n
class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
if(length == 0 || !numbers)
return false;
for(int i=0; i<length; i++)
{
if(numbers[numbers[i]%length] >= length)
{
*duplication = numbers[i]%length;
return true;
}
numbers[numbers[i]%length] += length;
}
return false;
}
};
50. 構建乘積數組
先從上到下計算下三角,在從下到上計算上三角
class Solution {
public:
vector<int> multiply(const vector<int>& A) {
vector<int> ret = vector<int>(A.size(), 1);
for(int i=1; i<A.size(); i++)
ret[i] = ret[i-1] * A[i-1];
int tmp = 1;
for(int i=A.size()-2; i>=0; i--)
{
tmp *= A[i+1];
ret[i] *= tmp;
}
return ret;
}
};