3数组中的重复数字
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(numbers == nullptr||length <= 1)
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) {
while(numbers[i]!=i){
if(numbers[i]==numbers[numbers[i]]){
*duplication=numbers[i];
return true;
}
int temp=numbers[i];
numbers[i]=numbers[temp];
numbers[temp]=temp;
}
}
return false;
}
};
4 二维数组中的查找
class Solution {
public:
bool Find(int target,vector<vector<int>> &matrix ) {
if (matrix.empty())
return false;
bool found = false;
int rows=matrix.size();
int columns=matrix[0].size();
if(rows > 0 && columns > 0 ){
int row = 0;
int column = columns-1;
while(row<rows&&column>=0){
if(matrix[row][column]==target){
found=true;
break;
}else if(matrix[row][column] > target) {
--column;
}else{
++row;
}
}
}
return found;
}
};
5.替换空格为%20(双指针)
class Solution {
public:
void replaceSpace(char *str,int length) {
if(str == nullptr || length <= 0)
return;
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(str[i] != '\0'){
++originalLength;
if(str[i] == ' ')
++numberOfBlank;
++i;
}
int newLength = originalLength + 2*numberOfBlank;
if(newLength > length)
return;
int Old = originalLength;
int New = newLength;
while(Old >= 0 && Old < New){
if(str[Old]==' '){
str[New--]='0';
str[New--]='2';
str[New--]='%';
}
else{
str[New--] = str[Old];
}
Old--;
}
}
};
6 从尾到头打印链表
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
if(head==NULL)
return res;
stack<ListNode*> help;
ListNode* node=head;
while(node!=NULL) {
help.push(node);
node = node -> next;
}
while(!help.empty()){
res.push_back((help.top())->val);
help.pop();
}
return res;
}
};
7 重建二叉树
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin)
{
if (pre.empty() || vin.empty())
return NULL;
//在前序遍历中prev中找根节点,并确定其在vin的位置
size_t index = 0;
for (; index<vin.size(); index++)
{
if (vin[index] == pre[0])
break;
}
//已经找到根节点,并构造根节点
TreeNode* root = new TreeNode(pre[0]);
//Print(root);
//根据中序遍历将根节点左右两侧一分为二,根节点的左侧为左子树,右侧为右子树
vector<int> prev_left, prev_right;
vector<int> vin_left, vin_right;
//先将前序、中序中根节点的左右子树记录下来
for (size_t j = 0; j<index; j++)
{
prev_left.push_back(pre[j + 1]);
vin_left.push_back(vin[j]);
}
for (size_t j = index + 1; j<vin.size(); j++)
{
prev_right.push_back(pre[j]);
vin_right.push_back(vin[j]);
}
//递归构造左右子树
root->left = reConstructBinaryTree(prev_left, vin_left);
root->right = reConstructBinaryTree(prev_right, vin_right);
return root;
}
};
8 二叉树下一个节点
题目描述
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。
注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
/*
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
}
};
*/
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode == NULL)
return NULL;
TreeLinkNode* res=nullptr;
if(pNode->right != NULL){//有右子树,右子树中最左节点为下一节点
pNode=pNode->right;
while(pNode->left!=NULL){
pNode=pNode->left;
}
return pNode;
}
while(pNode->next!=NULL){
TreeLinkNode *father = pNode->next;
if(father->left == pNode)//当前节点是父节点的左孩子,则下一节点就是父节点
return father;
pNode=pNode->next;//不是左孩子,继续往上寻找
}
return NULL;
}
};
9 两个栈实现队列
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
}
int res=stack2.top();
stack2.pop();
return res;
}
private:
stack<int> stack1;
stack<int> stack2;
};
10 斐波那契数列
class Solution {
public:
//递归
int Fibonacci(int n) {
if(n<=0) return 0;
if(n=1) return 1;
return Fibonacci(n-1)+Fibonacci(n-2);
}
//非递归
int Fibonacci2(int n) {
vector<int> res={0,1};
if(n<2)
return res[n];
int fn=0;
int fn_1=1;
int fn_2=0;
for(int i=2;i <= n;i++){
fn=fn_1+fn_2;
fn_2=fn_1;
fn_1=fn;
}
return fn;
}
//跳台阶
int jumpFloor(int n) {
vector<int> res={0,1,2};
if(n<=2)
return res[n];
int fn=0;
int fn_1=2;
int fn_2=1;
for(int i=3;i <= n;i++){
fn=fn_1+fn_2;
fn_2=fn_1;
fn_1=fn;
}
return fn;
}
};
11 旋转数组的最小数字
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution {
public:
int minNumberInRotateArray(vector<int> nums) {
if (nums.size() == 0) {
return 0;
}
int start = 0, end = nums.size() - 1;
int mid;
if (nums[start] < nums[end])
return nums[start];
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (nums[start] < nums[mid]) {
start=mid;
}
else if(nums[start]>nums[mid]){
end=mid;
}else{
++start;
}
}
if(nums[start]>nums[end]){
return nums[end];
}else{
return nums[start];
}
return 0;
}
};