循环旋转编码c ++解决方案

时间:2017-08-08 19:59:19

标签: c++ vector solution

我正在努力解决这个问题in Codility ...
这是代码:

 #include <iostream>    
 #include <vector>     
 #include <algorithm>
 using namespace std;

 vector<int> solution(vector<int> &A, int k);
 vector<int> A;   
 A.push_back(3);    
 A.push_back(5);   
 A.push_back(7);   
 A.push_back(9);   
 A.push_back(2);
 int k;
 rotate(A.rbegin(),A.rbegin()+k, A.rend());

虽然我的编译器编译并运行没有问题,但是codility告诉我&#34;错误:&#39; A&#39;没有命名类型&#34;。 这是用于我的编译器检查它的代码:

  #include <iostream>
  #include <vector>
  #include <algorithm>
  using namespace std;

  int main()
  {
   vector<int> myVector;
   myVector.push_back(3);
   myVector.push_back(5);
   myVector.push_back(7);
   myVector.push_back(9);
   myVector.push_back(2);
   for(unsigned i=0;i<myVector.size();i++)
   {
     cout<<myVector[i]<<" ";
   }
   cout<<endl;
   int k;
   cout<<"Insert the times of right rotation:";
   cin>>k;
   rotate(myVector.rbegin(),myVector.rbegin()+k, myVector.rend());
   for(unsigned i=0;i<myVector.size();i++)
   {
     cout<<myVector[i]<<" ";
   }
  }

编译器输出:

func.cpp:9:3: error: 'A' does not name a type
   A.push_back(3);
   ^
func.cpp:10:3: error: 'A' does not name a type
   A.push_back(5);
   ^
func.cpp:11:3: error: 'A' does not name a type
   A.push_back(7);
   ^
func.cpp:12:3: error: 'A' does not name a type
   A.push_back(9);
   ^
func.cpp:13:3: error: 'A' does not name a type
   A.push_back(2);
   ^
func.cpp:16:9: error: expected constructor, destructor, or type conversion before '(' token
   rotate(A.rbegin(),A.rbegin()+k, A.rend());
         ^
func.cpp:18:1: error: expected declaration before '}' token
 }
 ^

Detected some errors.

6 个答案:

答案 0 :(得分:1)

您只能在函数外定义或声明符号。您的A等不是定义或声明声明。这就是为什么你得到有趣的错误信息:定义和声明以一个类型开头,并且由于这些行在函数之外,编译器希望看到一个类型,#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> solution(vector<int> &A, int k) { //added //vector<int> A; // this is function argument, not local variable A.push_back(3); A.push_back(5); A.push_back(7); A.push_back(9); A.push_back(2); //int k; // this is function argument, not local variable rotate(A.rbegin(),A.rbegin()+k, A.rend()); return A; // Added since the function needs to return a vector of int... } //added 不是。

所以你需要一个功能,比如:

solution

那应该编译并做一些事情。所以我所做的就是你可能想做的事情:代码现在在arr函数中。

答案 1 :(得分:1)

如果您不想使用<algorithm>中的旋转功能。

Codility给出的结果:

Programming : C++
Task Score: 100%
Correctness: 100%
Performance: Not assesed

解决方案:

vector<int> solution(vector<int> &A, int K)
{
    vector <int> shift;
    if (A.empty()) // check for empty array
        return {};
    if (K > A.size()) //if K bigger then size of array 
        K = K%A.size();
    if (K<A.size())
        K=A.size()-K; //normalize K to the position to start the shifted array
    if (K == A.size()) //if K= size of array, avoid any computation.
        return A;
    for (unsigned int i=K; i<A.size(); i++)
    {
        shift.push_back(A[i]);
    }
    for (unsigned int i=0; i<K; i++)
    {
        shift.push_back(A[i]);
    }
    return shift;
}

答案 2 :(得分:0)

我认为你有很多问题并做了一些假设 这是工作代码

1.您不需要创建新的向量,因为函数已经有一个引用的向量&amp; A所以任何更改都会直接反映到原始向量

2.value K是已输入函数的旋转点(因此不需要cin)

现在100%获得

// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include<algorithm>


vector<int> solution(vector<int> &A, int K)
{
    if (A.empty() )
    {
        return A;
    }
    K = K % A.size();

    if (A.size() == 0 || A.size() == 1 ||  K == 0)
    {
        return A;

    }
    else
    {
        std::rotate(A.rbegin(), A.rbegin() + K, A.rend());
        return A;

    }

}

输出

enter image description here

答案 3 :(得分:0)

目标C解决方案O(n * k)-一对一方法

Codility提供的结果

任务得分:100%
正确性:100%
效果:未评估

时间复杂度

最坏情况下的时间复杂度是O(n * k)

Xcode Solution Here

+(NSMutableArray*)byByOneSolution:(NSMutableArray*)array rotations:(int)k {
    // Checking for edge cases in wich the array doesn't change.
    if (k == 0 || array.count <= 1) {
        return array;
    }

    // Calculate the effective number of rotations
    // -> "k % length" removes the abs(k) > n edge case
    // -> "(length + k % length)" deals with the k < 0 edge case
    // -> if k > 0 the final "% length" removes the k > n edge case
    NSInteger n = array.count;
    NSInteger rotations = (n  + k % n ) % n;


    /******** Algorithm Explanation: Naive Method ********/
    // Rotate one by one based on the efective rotations
    for (int i = 0; i < rotations; i++) {
        id last = array[n-1];
        [array removeLastObject];
        [array insertObject:last atIndex:0];
    }
    return array;
}

答案 4 :(得分:0)

目标C解决方案O(n)-基于逆向的解决方案

Codility提供的结果

任务得分:100%
正确性:100%
效果:未评估

时间复杂度

最差的时间复杂度是O(3n)=> O(n)

Xcode Solution Here

var url = require('url');

const options = {};
options.jwtFromRequest = (request) => {
  var token = null;
  var param_name = 'secret_token' //parameter name 
  var parsed_url = url.parse(request.url, true);

  if (request.headers[param_name]) {
    token = request.headers[param_name];
  }
  else if (parsed_url.query && Object.prototype.hasOwnProperty.call(parsed_url.query, param_name)) {
    token = parsed_url.query[param_name];
  }
  return token;
}
options.secretOrKey = process.env.AUTH_SECRET;

passport.use(new JWTstrategy(options, async (token, done) => {
  try {
    //Pass the user details to the next middleware
    return done(null, token.user);
  } catch (error) {
    done(error);
  }
}));

答案 5 :(得分:0)

一种可能的总得分为100%的C ++实现:

#include <algorithm>
#include <iterator>

vector<int> solution(vector<int> &A, int K) {
    vector<int> ret;
    
    if (!A.empty()) {
        int kReal = K % A.size();
        std::copy(A.begin()+A.size()-kReal, A.end(), std::back_inserter(ret));
        std::copy(A.begin(), A.begin()+A.size()-kReal, std::back_inserter(ret));
    }
    return ret;
}