Exemplo n.º 1
0
int main(){
	// int arr[] = {-1,2,1,-4};
	int arr[] = {0,1,2};
	vector<int> vecs (arr, arr+ sizeof(arr)/ sizeof(int));
	cout<< threeSumClosest(vecs, 3)<<endl;
	return 0;
}
Exemplo n.º 2
0
string convert(string s, int numRows) {
	if( numRows == 1)
		return s;
	vector<string> vecs(numRows, "");
	int num = numRows + (numRows-2) +1;
		
	bool from_head = true;
	int nums_pos = -1;
	for(int i=0; i < s.size(); i ++ ){
		if( from_head == true){
			nums_pos += 1;
		}else if( from_head == false){
			nums_pos -= 1;
		}
		vecs[nums_pos] += s[i];
		if(nums_pos == 0)
			from_head = true;
		else if( nums_pos == numRows -1)
			from_head = false;
	}

	string result ="";
	for(int i=0; i< vecs.size(); i ++ )
		result += vecs[i];
	return result;
}
Exemplo n.º 3
0
      //the following three functions are identical to read/readfile except that they read from
      //a block of memory instead of from a file
      void readFromMemoryBlock(vector<char>& data, Mesh* mesh,
                               DOFVector<double>* vec0,
                               DOFVector<double>* vec1,
                               DOFVector<double>* vec2,
                               bool writeParallel,
                               int nProcs)
      {
        uint32_t nValueVectors;
        memcpy(reinterpret_cast<char*>(&nValueVectors), &data[8], 4);
        vector<DOFVector<double>*> vecs(0);
        if (nValueVectors > 0)
          vecs.push_back(vec0);
        if (nValueVectors > 1)
          vecs.push_back(vec1);
        if (nValueVectors > 2)
          vecs.push_back(vec2);
        for (uint32_t i = 3; i < nValueVectors; i++)
          vecs.push_back(NULL);

        readFromMemoryBlock(data, mesh, vecs, writeParallel, nProcs);
      }
Exemplo n.º 4
0
  // Wrapper for elem::HermitianGenDefiniteEig sorts vals ascending
  ElemEigenSystem
  HermitianGenEigensolver(const TAMatrix &F, const TAMatrix &S){
    // Get mats from TA
    EMatrix EF = TiledArray::array_to_elem(F, elem::DefaultGrid()); // Fock
    EMatrix ES = TiledArray::array_to_elem(S, elem::DefaultGrid()); // Overlap

    // Vec and value storage
    elem::DistMatrix<double , elem::VR, elem::STAR> vals(elem::DefaultGrid());
    EMatrix vecs(elem::DefaultGrid());

    // Compute vecs and values
    elem::mpi::Barrier(elem::mpi::COMM_WORLD);
    // Compute evals evals
    sc::Timer tim("Solving HermitianGenEig:");
    elem::HermitianGenDefiniteEig(elem::AXBX, elem::LOWER,EF,ES,vals,vecs,
                                  elem::ASCENDING);
    elem::mpi::Barrier(elem::mpi::COMM_WORLD);
    tim.exit("Solving HermitianGenEig:");

    return std::make_pair(vals, vecs);
  }
Exemplo n.º 5
0
void
ConcatenatedVectorRealizer<V,M>::realization(V& nextValues) const
{
  std::vector<V*> vecs(m_realizers.size(),(V*)NULL);
  for (unsigned int i = 0; i < vecs.size(); ++i) {
    vecs[i] = new V(m_realizers[i]->unifiedImageSet().vectorSpace().zeroVector());
    //std::cout << "In ConcatenatedVectorRealizer<V,M>::realization: v[i]->sizeLocal() = " << v[i]->sizeLocal() << std::endl;
    m_realizers[i]->realization(*(vecs[i]));
  }

  //std::cout << "In ConcatenatedVectorRealizer<V,M>::realization: nextValues.sizeLocal() = " << nextValues.sizeLocal() << std::endl;
  std::vector<const V*> constVecs(m_realizers.size(),(V*)NULL);
  for (unsigned int i = 0; i < vecs.size(); ++i) {
    constVecs[i] = vecs[i];
  }
  nextValues.cwSetConcatenated(constVecs);
  //std::cout << "In ConcatenatedVectorRealizer<V,M>::realization: succeeded" << std::endl;

  for (unsigned int i = 0; i < vecs.size(); ++i) {
    delete vecs[i];
  }

  return;
}