【1】各类简单算法(斐波那契、数组排列、链表、图像旋转、数组运算)

  • 领域

斐波那契(跳台阶之类)、数组数据排列、数组内部数据处理、图像旋转、链表等。

  • 应用

借鉴学习总结

  • 代码
/*
*  main.cpp
*  TestAlgorithm
*  Copyright © 2020 yancey. All rights reserved.
*/
#include 
#include 
#include 
using namespace std;

int main(int argc, const char * argv[]) {
    //
    int numberWay(int n);
    int n = 10;
    int number = numberWay(n);
    cout<<"value:"< twoSum(vector&nums, int target);
    vector nums = {1,3,5,8,11,13,15,18};
    int target = 23;
    vector value = twoSum(nums,target);
    for(vector::iterator it = value.begin(); it != value.end(); it++){
        cout<<"value:"<<*it<&nums);
    vector x = { 1, 1, 2};
    vector y = { 0,0,1,1,1,2,2,3,3,4 };
    cout<<"len:"<::iterator it = x.begin(); it != x.end(); it++){
        cout<<"x[i]"<<*it<::iterator it = y.begin(); it != y.end(); it++){
           cout<<"y[i]"<<*it< twoSum(vector&nums, int target)
{
    vector vec;
    map dic;
    for(int i=0;i上
            *(martrix + first*n + i) = *(martrix + (last-offset)*n + first);
            //下->左
            *(martrix + (last-offset)*n + first) = *(martrix + last*n + (last-offset));
            //右->下
            *(martrix + last*n + (last-offset)) = *(martrix + i*n + last);
            //上->右
            *(martrix + i*n + last) = top;
        }
    }
}
void rotate2 (int martrix[][4], int n)
{
    for(int i = 0; i < (n+1)/2; i++){
        for(int j = 0; j < n/2; j++){
            /*
                     4->3
                     |  |
                     2<-1
             */
            int temp = martrix[n-1-j][i];
            //右下->左下
            martrix[n-1-j][i] = martrix[n-1-i][n-1-j];
            //右上->右下
            martrix[n-1-i][n-1-j] = martrix[j][n-1-i];
            //左上->右上
            martrix[j][n-1-i] = martrix[i][j];
            //左下->左上
            martrix[i][j] = temp;
        }
    }
}
/*
 *问题属性:数组运算
 *问题描述:给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.
 */
int removeDuplicates(vector&nums)
{
    int len = 0 ;
    if(nums.size() == 0){
        return 0;
    }
    else{
        for(int i = 1; i < nums.size(); i++){
            if(nums[len] != nums[i]){
                nums[++len] = nums[i];
            }
        }
        return len+1;
    }
}
/*
 *问题属性:排列、冒泡、快速、插入、选择
 *问题描述:给定乱序数组,进行升序排列
 */
 void bubbleSort(int nums[], int n)
{
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n-i-1; j++){
            if(nums[j] > nums[j+1]){
                int temp = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;
            }
        }
    }
}
/*
 *
 *
 */

 

你可能感兴趣的:(algorithm,C++)