文章目录
(第三十一题)不用加减乘除做加法(有限制)
2019年9月7日20:38:35
题目链接如下:
https://www.nowcoder.com/practice/59ac416b4b944300b617d4f7f111b215?tpId=13&tqId=11201&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间: 2019年9月7日20:39:44
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月7日20:39:47
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <time.h>
#include <algorithm>
using namespace std;
class Solution {
public:
int Add(int num1, int num2)
{
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
if (num1 < 0)
{
int n = -num1;
while (n--)
{
num2--;
}
return num2;
}
else
{
int n = num1;
while (n--)
{
num2++;
}
return num2;
}
}
};
int main()
{
Solution solution;
cout << solution.Add(1, 2) << endl;
cout << solution.Add(-1, 2) << endl;
cout << solution.Add(-2, 2) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月7日20:40:49
(第三十二题)把字符串转换成整数
2019年9月7日21:00:34
题目链接如下:
https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e?tpId=13&tqId=11202&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间: 2019年9月7日20:43:21
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月7日21:00:45
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
int StrToInt(string str)
{
int size = str.size();
if (size == 0)
return 0;
vector<char>myvec;
int flag_of_data = 1;//默认正数
if (str[0] == '-')
{
flag_of_data = -1;
str[0] = '0';
}
else if (str[0] == '+')
{
str[0] = '0';
}
for (int i=0;i<size;++i)
{
if (isdigit(str[i]))
myvec.push_back(str[i]);
else
return 0;
}
int result = 0, len = myvec.size();
for (char c : myvec)
{
result = result * 10 + c - '0';
}
return result * flag_of_data;
}
};
int main()
{
Solution solution;
cout << solution.StrToInt("+2147483647") << endl;
cout << solution.StrToInt("-11111111") << endl;
cout << solution.StrToInt("+11a11111") << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月7日21:03:17
(第三十三题)孩子们的游戏(圈中最后剩下的数)
2019年9月7日22:55:19
题目链接如下:
https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6?tpId=13&tqId=11199&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月7日21:05:27
*功能描述:2019年9月7日21:52:03
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月7日22:56:33
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
int LastRemaining_Solution(int n, int m)
{
if (m < 1)
return -1;
vector<int>myvec;
for (int i = 0; i < n; ++i)
{
myvec.push_back(i);
}
int begin_num = 0;//这个是 每次开始的那个孩子
for (int i = n; i > 1; --i)//每次干掉一个幸运的孩子
{
int cursize = myvec.size();//幸存的人数
//每次被干掉的孩子的在(0——N-1)的 在幸存中的位置
begin_num = (begin_num + m - 1) % cursize;
vector<int>::iterator it = myvec.begin() + begin_num;//找到他
myvec.erase(it);
}
return myvec[0];//返回其编号
}
};
int main()
{
Solution solution;
//测试数据 n=6 6个人;m=8,每次都是报数0 --- 7
//6个人编号为0,1,2,3,4,5
//最后是 2号幸存
cout << solution.LastRemaining_Solution(6, 8) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月7日22:57:29
(第三十四题)翻转单词顺序列
2019年9月8日01:37:18
题目链接如下:
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3?tpId=13&tqId=11197&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日01:38:57
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
string ReverseSentence(string& str)
{
int size = str.size();
string mystr = "";
if (str.empty())
return mystr;
vector<string>myvec;
bool flag_in_bit = false;//默认不在空格里面 而是单词
for (char c : str)
{
if (c == ' ')
{
if (!mystr.empty() && !flag_in_bit)//把单词推进去
{
myvec.push_back(mystr);
mystr.clear();
}
mystr += c;//这里加的是 空格
flag_in_bit = true;
}
else
{
if (!mystr.empty() && flag_in_bit)//把空格推进去
{
myvec.push_back(mystr);
mystr.clear();
flag_in_bit = false;
}
mystr += c;
}
}
for (vector<string>::reverse_iterator rit = myvec.rbegin();
rit != myvec.rend(); ++rit)
{
mystr += *rit;
}
return mystr;
}
};
int main()
{
Solution solution;
string str = " student. a am I";
//string str = " ";
cout << solution.ReverseSentence(str) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日01:42:04
(第三十五题)左旋转字符串
2019年9月8日01:50:41
题目链接如下:
https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec?tpId=13&tqId=11196&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日01:43:02
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日01:49:59
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
string LeftRotateString(string &str, int n)
{
int size = str.size();
if (size == 0)
return str;
string temp_str;
n = n % size;
int i = 0;
for (; i < n; ++i)
{
temp_str += str[i];
}
for (int j=0; i < size; ++i,++j)
{
temp_str.insert(temp_str.begin() + j, str[i]);
}
return temp_str;
}
};
int main()
{
Solution solution;
string str = "abcdefghijk";
//string str = " ";
cout << solution.LeftRotateString(str, 4) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日01:52:35
(第三十六题)和为S的两个数字
2019年9月8日03:07:12
题目链接如下:
https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13&tqId=11195&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日01:58:59
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日03:08:39
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> &array, int sum)
{
int size = array.size();
if (size == 0)
return array;
vector<int>vec;//最后的输出结果
unordered_map<int, int>mymap;
//array是递增有序的
for (int i = size - 1; i >= 0; --i)
{
if (bool flag = find(array.begin(),array.end(),sum - array[i]) != array.end())
{
if (sum - array[i] != array[i] &&
sum - array[i] < array[i])//两个不一样的数都存在
{
mymap.insert({ sum - array[i],array[i] });
}
else if (count(array.begin(), array.end(), sum - array[i]) != 1)
{
//两个一样的数都存在
mymap.insert({ sum - array[i],array[i] });
}
}
}
if (mymap.empty())
return vec;
vector<int>multiply_result;//放两数乘积的
auto it = mymap.begin(); int number = mymap.size();
for (; it != mymap.end(); ++it)
{
multiply_result.push_back(it->first * it->second);
}
int minval = multiply_result[0];
for (int i = 1; i < number; ++i)
{
if (multiply_result[i] < minval)
minval = multiply_result[i];
}
it = mymap.begin();
for (int i = 0; i < number; ++i)
{
if (it->first * it->second == minval)
break;
it++;
}
vec.push_back(it->first);
vec.push_back(it->second);
return vec;
}
};
int main()
{
Solution solution;
int Array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20 };
int Array2[] = { 2,2,6,6,11,15 };
vector<int>myvec(begin(Array), end(Array));
vector<int>myvec2(begin(Array2), end(Array2));
vector<int>result1 = solution.FindNumbersWithSum(myvec, 21);
if (result1.size() < 2)
{
cout << "不存在" << endl;
}
else
{
cout << result1[0] << endl;
cout << result1[1] << endl;
}
vector<int>result2 = solution.FindNumbersWithSum(myvec2, 12);
if (result2.size() < 2)
{
cout << "不存在" << endl;
}
else
{
cout << result2[0] << endl;
cout << result2[1] << endl;
}
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日03:10:31
(第三十七题)表示数值的字符串
2019年9月8日11:29:59
题目链接如下:
https://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2?tpId=13&tqId=11206&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日10:19:57
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日11:30:53
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
using namespace std;
class Solution {
public:
bool isNumeric(char* string)
{
if (string == nullptr)
{
return false;
}
bool flag_of_sign = false;//这是- +出现的标志
bool flag_of_de = false;//这是 小数点 . 出现的标志
bool flag_of_E = false;//这是 e和E出现的标志
char* p = string;
int begin = 0, length = strlen(string);
while (*p != '\0')
{
if (*p == 'e' || *p == 'E')
{
if (flag_of_E == true)//E 不能出现两次
return false;
if (begin == 0 || begin == length - 1)//E 不能出现在第一位 最后一位
return false;//因为E前后都需要 数字
flag_of_E = true;
p++; begin++;
}
else if (*p == '.')
{
if (flag_of_de == true)//小数点不能出现两次
return false;
if (flag_of_E == true)//小数点 不能出现在E的后面 因为人家要的是一个整数
return false;
if (begin == length - 1)//小数点也不能出现在最后一位上
return false;
flag_of_de = true;
p++; begin++;
}
else if (*p == '+' || *p == '-')
{
//但凡第一次出现 + -,只能处在第一位 和 E的后面
if (flag_of_sign == false && (begin != 0 && flag_of_E == false))
return false;
if (flag_of_sign == true && (*(p - 1) != 'E' && *(p - 1) != 'e'))//第二次只能出现在E的后面
return false;
if (begin == length - 1)//且不能出现在最后一位上
return false;
flag_of_sign = true;
p++; begin++;
}
else
{
if (!isdigit(*p))
return false;//不是0到9
p++; begin++;
}
}
return true;
}
};
int main()
{
Solution solution;
char Array[] = "+100"; if (solution.isNumeric(Array)) cout << "OK" << endl; else cout << "NO" << endl;
char Array1[] = "5e2";if (solution.isNumeric(Array1)) cout << "OK" << endl; else cout << "NO" << endl;
char Array2[] = "-123";if (solution.isNumeric(Array2)) cout << "OK" << endl; else cout << "NO" << endl;
char Array3[] = "3.1416";if (solution.isNumeric(Array3)) cout << "OK" << endl; else cout << "NO" << endl;
char Array4[] = "-1E-16";if (solution.isNumeric(Array4)) cout << "OK" << endl; else cout << "NO" << endl;
cout << "******************************" << endl;
char Array5[] = "12e";if (solution.isNumeric(Array5)) cout << "OK" << endl; else cout << "NO" << endl;
char Array6[] = "1a3.14";if (solution.isNumeric(Array6)) cout << "OK" << endl; else cout << "NO" << endl;
char Array7[] = "1.2.3";if (solution.isNumeric(Array7)) cout << "OK" << endl; else cout << "NO" << endl;
char Array8[] = "+-5";if (solution.isNumeric(Array8)) cout << "OK" << endl; else cout << "NO" << endl;
char Array9[] = "12e+4.3";if (solution.isNumeric(Array9)) cout << "OK" << endl; else cout << "NO" << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日11:32:31
(第三十八题)和为S的连续正数序列
2019年9月8日12:35:13
题目链接如下:
https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日11:34:50
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日12:24:01
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <algorithm>
#include <iomanip>
using namespace std;
class Solution {
public:
vector<vector<int>> FindContinuousSequence(int sum)
{
vector<vector<int>>final_result;
vector<int>vec;
if (sum < 3)
{
return final_result;
}
for (int i = 2;; ++i)
{
if (i % 2 == 0)
{
int n = sum / i;
if (n < i / 2)//这是跳出循环
break;
if ((n + n + 1) * i / 2 == sum)
{
for (int k = n - i / 2 + 1; k <= n + i / 2; ++k)
vec.push_back(k);
final_result.push_back(vec);
vec.clear();
}
}
else
{
int n = sum / i;
if (n <= (i - 1) / 2)//这是跳出循环
break;
if (n * i == sum)
{
for (int k = n - i / 2; k <= n + i / 2; ++k)
vec.push_back(k);
final_result.push_back(vec);
vec.clear();
}
}
}
reverse(final_result.begin(), final_result.end());
return final_result;
}
};
int main()
{
Solution solution;
vector<vector<int>>myvec = solution.FindContinuousSequence(15);
for (vector<int>& vec : myvec)
{
for (int val : vec)
{
cout << setw(2) << val << " ";
}
cout << endl;
}
return 0;
}
/**
*备用注释:
*
*
*
*/
(第三十九题)字符流中第一个不重复的字符
2019年9月8日13:17:14
题目链接如下:
https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720?tpId=13&tqId=11207&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日12:39:12
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日13:18:07
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std;
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
int chval = ch - '0';
mymap[chval]++;
if (mymap[chval] == 1)
{
myvec.push_back(ch);
}
if (mymap[chval] >= 2)
{
for (auto it = myvec.begin(); it != myvec.end(); ++it)
if (*it == ch)
{
myvec.erase(it);
break;
}
}
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
if (myvec.empty())
return '#';
return myvec[0];
}
private:
unordered_map<int, int>mymap;;
vector<char>myvec;
};
int main()
{
Solution solution;
cout << solution.FirstAppearingOnce() << endl;
solution.Insert('g');
cout << solution.FirstAppearingOnce() << endl;
char Array[] = { 'g','o','o','g','l','e'};
for (int i = 0; i < 6; ++i)
{
solution.Insert(Array[i]);
}
cout << solution.FirstAppearingOnce() << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日13:19:52
(第四十题)构建乘积数组
2019年9月8日17:07:39
题目链接如下:
https://www.nowcoder.com/practice/94a4d381a68b47b7a8bed86f2975db46?tpId=13&tqId=11204&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述:
代码如下:
/**══════════════════════════════════╗
*作 者:songjinzhou ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub:https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:2019年9月8日16:49:11
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月8日17:08:35
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std;
class Solution {
public:
vector<int> multiply(const vector<int>& A)
{
vector<int>result_vec;
int size = A.size();
if (size <= 1)
return result_vec;
/*
构建一个数组B[0,1,...,n-1],其中B中的元素
B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
使用双重循环的话 时间复杂度太高
假设 A【】={1,2,3,4,5} size=5;
*/
//采用正去,反回的方式,做到结果数组中数据的 相应相乘而得
result_vec.push_back(1);//给vec【0】,置成1
int temp_val = 1;
for (int i = 1; i < size; ++i)
{
temp_val = A[i - 1] * result_vec[i - 1];
result_vec.push_back(temp_val);
}
//到此时为止,result数组:1,1,2,6,24
//5个数到齐
temp_val = 1;//接下来要进行反向,首先处理倒数第二个
for (int i = size - 2; i >= 0; --i)
{
temp_val = temp_val * A[i + 1];
result_vec[i] *= temp_val;
}
return result_vec;
}
};
int main()
{
Solution solution;
int Array[] = { 1,2,3,4,5 };
vector<int>myvec(begin(Array), end(Array));
for (int val : solution.multiply(myvec))
{
cout << val << " ";
}
cout << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月8日17:09:44