2019年7月21日下午
题目链接如下:https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8?tpId=13&tqId=11203&tPage=3&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
class Solution
{
public:
void mySwap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
// 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 (nullptr == numbers || length <= 0)//保证数组合法
{
return false;
}
for (int i = 0; i < length; ++i)//保证数组内数据是合法的
{
if (numbers[i] < 0 || numbers[i]>length - 1)
return false;
}
for (int i = 0; i < length; ++i)
{
if (i != numbers[i])
{
if (numbers[i] != numbers[numbers[i]])
{
mySwap(numbers[i], numbers[numbers[i]]);
i--;
}
else
{
*duplication = numbers[i];
return true;
}
}
}
return false;
}
};
/*
时间限制:1秒 空间限制:32768K 热度指数:262120
本题知识点: 数组
题目描述:
在一个长度为n的数组里的所有数字都在0到n-1的范围内。
数组中某些数字是重复的,但不知道有几个数字是重复的。
也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
例如,如果输入长度为7的数组{2,3,1,0,2,5,3},
那么对应的输出是第一个重复的数字2。
*/
/*
既然这个数组 的数据都在0到n-1的范围内。 且数组长度为n
(1)如果没有重复数字,那么 这组数据一旦排序 数字i就一定在数组的i下标位置
(2)有重复数字,那么数组有序,则 这个数组可以被看做是有
些位置好几个数据,有些位置没有数据
*/
int main()
{
int myArray[] = { 2,3,1,0,2,5,3 };
int len = sizeof(myArray) / sizeof(myArray[0]);
Solution solution;
int number;
if (solution.duplicate(myArray, len, &number))
{
cout << number << endl;
}
return 0;
}
总结:我的做法和课本上的有些区别,但是大致思想都是一样的。课本见p40.
2019年7月21日下午
题目链接如下:
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
问题描述:
代码如下:
#include
#include
using namespace std;
/*
1 2 8 9
2 4 9 12
4 7 10 13
9 10 11 15
找7 true
找6 false
*/
class Solution {
public:
bool Find(int target, vector<vector<int> > &array)
{
if (array.size()==0)
{
return false;
}
bool finaltag = false;//找到标志
int Row = array.size();//这是行数
int Col = (array[0]).size();//这是列数
/*
从左下角进行选择 方向是右上
(1)如果选择的数字等于target OK true
(2)选择的数字小于target 则第一列就可以不要了
(3)选择的数字大于target 则第一列和最后一行都可以不要了(直接瘦了一圈)
*/
int myRow = Row - 1;//劳资从左下角开始的
int myCol = 0;
while (myRow >= 0 && myCol <= Col - 1)//劳资往右上角走了
{
if (array[myRow][myCol] == target)
{
finaltag = true;
break;
}
else if (array[myRow][myCol] < target)
{
myCol++;
}
else
{
myRow--;
}
}
return finaltag;
}
};
int main()
{
vector<vector<int>>myArray;
vector<int>vec;
int Row, Col, data;
cin >> Row >> Col;
cout << "-------------------------" << endl;
for (int i = 0; i < Row; ++i)
{
vec.clear();
for (int j = 0; j < Col; ++j)
{
cin >> data;
vec.push_back(data);
}
myArray.push_back(vec);
}
cout << "-------------------------" << endl;
Solution solution;
if (solution.Find(7, myArray))
{
cout << "二维数组里面有7这个数" << endl;
}
else cout << "二维数组里面没有7这个数" << endl;
cout << "-------------------------" << endl;
for (vector<vector<int>>::iterator IT = myArray.begin();
IT != myArray.end(); ++IT)
{
for (vector<int>::iterator it = (*IT).begin();
it != (*IT).end(); ++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << endl;
return 0;
}
2019年9月5日01:22:24
题目链接如下:
https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&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月5日01:24:26
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日01:24:29
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
using namespace std;
class Solution {
public:
void replaceSpace(char* str, int length)
{
if (length < 1)
return;
int n = length;
char* pfast = str;
string temp_str;
while (n-- && *pfast!='\0')
{
if (*pfast == 32)
{
temp_str.push_back('%');
temp_str.push_back('2');
temp_str.push_back('0');
}
else
{
temp_str.push_back(*pfast);
}
pfast++;
}
int i = 0;
for (; i < temp_str.size(); ++i)
{
str[i] = temp_str[i];
}
str[i] = '\0';
}
};
int main()
{
Solution solution;
char str[1024] = "We Are Happy.";
cout << str << endl;
solution.replaceSpace(str, sizeof(str) / sizeof(str[0]));
cout << str << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月5日02:14:33
题目链接如下:
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage=1&rp=1&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月5日02:20:25
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日02:20:28
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x=0) :val(x), next(nullptr) {}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head)
{
vector<int>ArrayList;
if (head == nullptr)
return ArrayList;
ListNode* p = head;
while (p!=nullptr)
{
ArrayList.push_back(p->val);
p = p->next;
}
//使用反向迭代器创建临时对象
return vector<int>(ArrayList.rbegin(), ArrayList.rend());
}
};
int main()
{
Solution solution;
struct ListNode* list = new ListNode;
for (int i = 0; i < 5; ++i)
{
ListNode* p = new ListNode(i);
p->next = list->next;
list->next = p;
}
ListNode* p = list;
while (p != nullptr)
{
cout << p->val << " ";
p = p->next;
}
cout << endl;
vector<int>myvec = solution.printListFromTailToHead(list);
for (int val : myvec)
cout << val << " ";
cout << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
注:按说 这里头结点是不应该有数据的,但是我最开始没有考虑 head指针指向的节点是有val的,所以这个在 剑指offer的编译器下 是一直不能通过的。大家注意一下就行了!
下面是我最开始写的 也是可以通过的,但是依旧需要注意上面的那个问题:
struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x=0) :val(x), next(nullptr) {}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head)
{
vector<int>ArrayList;
if (head == nullptr)
return ArrayList;
ListNode* p = head;
while (p!=nullptr)
{
ArrayList.push_back(p->val);
p = p->next;
}
/*
//使用反向迭代器创建临时对象
return vector(ArrayList.rbegin(), ArrayList.rend());
*/
int n = ArrayList.size();
for (int i = 0; i <= n / 2 - 1; ++i)
{
swap(ArrayList[i], ArrayList[n - 1 - i]);
}
return ArrayList;
}
};
2019年9月5日15:57:07
题目链接如下:
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&tPage=1&rp=1&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月5日15:54:44
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日15:54:48
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
#include
using namespace std;
class Solution
{
public:
void push(int node)
{
stack1.push(node);
}
int pop()
{
int val;
if (stack2.empty())
{
if (stack1.empty())
throw"queue is empty";
else
{
while (!stack1.empty())
{
val = stack1.top();
stack2.push(val);
stack1.pop();
}
val = stack2.top();
stack2.pop();
return val;
}
}
else
{
val = stack2.top();
stack2.pop();
return val;
}
}
private:
stack<int> stack1;
stack<int> stack2;
};
int main()
{
Solution solution;
/*
测试如下:
1:5,4入队
2:5出队
3:3入队
4:4,3出队
5:1,2入队,并且出队
6:出队
*/
solution.push(5);
solution.push(4);
cout << solution.pop() << "出队" << endl;
solution.push(3);
cout << solution.pop() << "出队" << endl;
cout << solution.pop() << "出队" << endl;
solution.push(1);
solution.push(2);
cout << solution.pop() << "出队" << endl;
cout << solution.pop() << "出队" << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月5日16:11:28
题目链接如下:
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&tPage=1&rp=1&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月5日16:12:32
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日16:12:38
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
class Solution {
public:
int minNumberInRotateArray(vector<int> &rotateArray)
{
int size = rotateArray.size();
if (size == 0)
return 0;
int minval = rotateArray[size - 1];//假设最后一个元素值最小
for (int i = size - 2; i >= 0; --i)
{
if (rotateArray[i] <= minval)
{
minval = rotateArray[i];
}
else break;
}
return minval;
}
};
int main()
{
Solution solution;
vector<int>myvec;
myvec.push_back(3);
myvec.push_back(4);
myvec.push_back(5);
myvec.push_back(1);
myvec.push_back(2);
cout << "min=" << solution.minNumberInRotateArray(myvec) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月5日16:57:18
题目链接如下:
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&tPage=1&rp=1&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月5日16:18:02
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日16:58:10
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
//0、1、1、2、3、5、8、13、21、…
//F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)
class Solution {
public:
int Fibonacci(int n)
{
if (n == 0 || n == 1)
{
return n;
}
unordered_map<int, int>myu_map;
myu_map.insert({ 0,0 });
myu_map.insert({ 1,1 });
return Fibonacci_operator(myu_map, n);
}
int Fibonacci_operator(unordered_map<int, int>&myu_map,int n)
{
int val1 = 0, val2 = 0;
unordered_map<int, int>::iterator it1 = myu_map.find(n - 1);
unordered_map<int, int>::iterator it2 = myu_map.find(n - 2);
if (it1 != myu_map.end())
{
val1 = it1->second;
}
else
{
val1 = Fibonacci_operator(myu_map, n - 1);
myu_map.insert({ n - 1,val1 });
}
if (it2 != myu_map.end())
{
val2 = it2->second;
}
else
{
val2 = Fibonacci_operator(myu_map, n - 2);
myu_map.insert({ n - 2,val2 });
}
return val1 + val2;
}
};
int main()
{
Solution solution;
cout << solution.Fibonacci(39) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月5日17:17:39
题目链接如下:
https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=13&tqId=11161&tPage=1&rp=1&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月5日17:05:55
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日17:16:59
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
//0、1、2、3、5、8、13、21、…
//F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥3,n∈N*)
class Solution {
public:
int jumpFloor(int number)
{
if (number == 1 || number == 0 || number == 2)
return number;
//当number>=1 时,都至少有一种 全1的跳法
unordered_map<int, int>myu_map;
myu_map.insert({ 0,0 });
myu_map.insert({ 1,1 });
myu_map.insert({ 2,2 });
return jumpFloor_operator(myu_map, number);
}
int jumpFloor_operator(unordered_map<int, int>& myu_map, int n)
{
int val1 = 0, val2 = 0;
unordered_map<int, int>::iterator it1 = myu_map.find(n - 1);
unordered_map<int, int>::iterator it2 = myu_map.find(n - 2);
if (it1 != myu_map.end())
{
val1 = it1->second;
}
else
{
val1 = jumpFloor_operator(myu_map, n - 1);
myu_map.insert({ n - 1,val1 });
}
if (it2 != myu_map.end())
{
val2 = it2->second;
}
else
{
val2 = jumpFloor_operator(myu_map, n - 2);
myu_map.insert({ n - 2,val2 });
}
return val1 + val2;
}
};
int main()
{
Solution solution;
cout << "0: " << solution.jumpFloor(0) << endl;
cout << "1: " << solution.jumpFloor(1) << endl;
cout << "2: " << solution.jumpFloor(2) << endl;
cout << "20: " << solution.jumpFloor(20) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
注:本题和上一题的代码 一模一样。2019年9月5日17:19:51
2019年9月5日18:05:53
题目链接如下:
https://www.nowcoder.com/practice/22243d016f6b47f2a6928b4313c85387?tpId=13&tqId=11162&tPage=1&rp=1&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月5日18:06:50
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日18:06:54
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
//0、1、2、4、8、16…
//F(0)=0,F(1)=1,F(2)=2,F(3)=4,
/*
F(n) = F(n-1) + F(n-2) +...+ f(2) + f(1) + 1
F(n-1) = F(n-2) +...+ F(2) + F(1) + 1
===》》 F(n) - F(n-1) = F(n-1) ------>F(n) = 2 * F(n-1)
*/
class Solution {
public:
int jumpFloorII(int number)
{
if (number == 1 || number == 0 )
return number;
//当number>=1 时,都至少有一种 全1的跳法
unordered_map<int, int>myu_map;
myu_map.insert({ 0,0 });
myu_map.insert({ 1,1 });
return jumpFloor_operator(myu_map, number);
}
int jumpFloor_operator(unordered_map<int, int>& myu_map, int n)
{
if (n == 1)
return 0;
int val1 = 0;
unordered_map<int, int>::iterator it1 = myu_map.find(n - 1);
if (it1 != myu_map.end())
{
val1 = it1->second;
}
else
{
val1 = jumpFloor_operator(myu_map, n - 1);
myu_map.insert({ n - 1,val1 });
}
return val1 * 2;
}
};
int main()
{
Solution solution;
cout << "0: " << solution.jumpFloorII(0) << endl;
cout << "1: " << solution.jumpFloorII(1) << endl;
cout << "2: " << solution.jumpFloorII(2) << endl;
cout << "3: " << solution.jumpFloorII(3) << endl;
cout << "4: " << solution.jumpFloorII(4) << endl;
cout << "5: " << solution.jumpFloorII(5) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
2019年9月5日02:14:33
题目链接如下:
https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=13&tqId=11164&tPage=1&rp=1&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月5日23:31:49
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间: 2019年9月5日23:31:54
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include
#include
#include
using namespace std;
class Solution {
public:
int NumberOf1(int n)
{
int count = 0;
if (n == 0)
return 0;
vector<int>myvec;
myvec.resize(32);
int size = 32;
if (n > 0)
{
while (n != 0)
{
myvec[--size] = n % 2;
n /= 2;
}
for (int val : myvec)
{
if (val == 1)
count++;
}
}
else
{
n = -n;
size = 32;
while (n != 0)
{
myvec[--size] = n % 2;
n /= 2;
}
for (vector<int>::iterator it=myvec.begin();
it!=myvec.end();++it)
{
if (*it == 0)
{
*it = 1;
}
else *it = 0;
}
for (vector<int>::iterator it=myvec.end()-1;
it!=myvec.begin();--it)
{
if (*it == 0)
{
*it = 1;
break;
}
else
{
*it = 0;
}
}
myvec[0] = 1;
for (int val : myvec)
{
if (val == 1)
count++;
}
}
return count;
}
};
int main()
{
Solution solution;
cout << solution.NumberOf1(7) << endl;
cout << solution.NumberOf1(-7) << endl;
cout << solution.NumberOf1(5) << endl;
cout << solution.NumberOf1(-5) << endl;
cout << solution.NumberOf1(100) << endl;
cout << solution.NumberOf1(-100) << endl;
cout << solution.NumberOf1(1) << endl;
cout << solution.NumberOf1(-1) << endl;
cout << solution.NumberOf1(2147483647) << endl;
cout << solution.NumberOf1(-2147483648) << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/