int solution1(TreeNode *root){ if(root == NULL) return 0; int left = solution1(root->left); int right = 0; if(chk){ right = solution1(root->right); if(abs(left - right) > 1) chk = false; } return max(left, right) + 1; }
void table(int argc, char **argv) { int arcount; int compteur; int cnt_line; int tab_nbr[81]; int solved[81]; arcount = 1; compteur = 0; cnt_line = 0; while (arcount < argc) { while (argv[arcount][compteur] != '\0') { if (argv[arcount][compteur++] == '.') tab_nbr[cnt_line++] = 0; else tab_nbr[cnt_line++] = argv[arcount][compteur - 1] - '0'; } arcount++; compteur = 0; } if (!solve(0, tab_nbr, solved) && *solved) solution1(solved); else write(1, "Erreur\n", 7); }
string solution1(int n){ string s; if(!n) return s; int rem = (n - 1) % 26 + 1; s = solution1((n - rem) / 26) + (char)(rem + 64); return s; }
bool isPalindrome(int x) { // int orgX = x; // int reverseNum = x % 10; // x /= 10; // int tmpNum = 0; // while (x) // { // tmpNum = x % 10; // reverseNum = reverseNum * 10 + tmpNum; // x /= 10; // } // return reverseNum == orgX; return solution1(x); }
/* 'Data' Matrix of data containing observation data in rows, one * row per observation and complying with this format: * x y z P * Where x,y,z are satellite coordinates in an ECEF system * and P is pseudorange (corrected as much as possible, * specially from satellite clock errors), all expresed * in meters. * * 'X' Vector of position solution, in meters. There may be * another solution, that may be accessed with vector * "SecondSolution" if "ChooseOne" is set to "false". * * Return values: * 0 Ok * -1 Not enough good data * -2 Singular problem */ int Bancroft::Compute( Matrix<double>& Data, Vector<double>& X ) throw(Exception) { try { int N = Data.rows(); Matrix<double> B(0,4); // Working matrix // Let's test the input data if( testInput ) { double satRadius = 0.0; // Check each row of B Matrix for( int i=0; i < N; i++ ) { // If Data(i,3) -> Pseudorange is NOT between the allowed // range, then drop line immediately if( !( (Data(i,3) >= minPRange) && (Data(i,3) <= maxPRange) ) ) { continue; } // Let's compute distance between Earth center and // satellite position satRadius = RSS(Data(i,0), Data(i,1) , Data(i,2)); // If satRadius is NOT between the allowed range, then drop // line immediately if( !( (satRadius >= minRadius) && (satRadius <= maxRadius) ) ) { continue; } // If everything is ok so far, then extract the good // data row and add it to working matrix MatrixRowSlice<double> goodRow(Data,i); B = B && goodRow; } // Let's redefine "N" and check if we have enough data rows // left in a single step if( (N = B.rows()) < 4 ) { return -1; // We need at least 4 data rows } } // End of 'if( testInput )...' else { // No input filtering. Working matrix (B) and // input matrix (Data) are equal B = Data; } Matrix<double> BT=transpose(B); Matrix<double> BTBI(4,4), M(4,4,0.0); Vector<double> aux(4), alpha(N), solution1(4), solution2(4); // Temporary storage for BT*B. It will be inverted later BTBI = BT * B; // Let's try to invert BTB matrix try { BTBI = inverseChol( BTBI ); } catch(...) { return -2; } // Now, let's compute alpha vector for( int i=0; i < N; i++ ) { // First, fill auxiliar vector with corresponding satellite // position and pseudorange aux(0) = B(i,0); aux(1) = B(i,1); aux(2) = B(i,2); aux(3) = B(i,3); alpha(i) = 0.5 * Minkowski(aux, aux); } Vector<double> tau(N,1.0), BTBIBTtau(4), BTBIBTalpha(4); BTBIBTtau = BTBI * BT * tau; BTBIBTalpha = BTBI * BT * alpha; // Now, let's find the coeficients of the second order-equation double a(Minkowski(BTBIBTtau, BTBIBTtau)); double b(2.0 * (Minkowski(BTBIBTtau, BTBIBTalpha) - 1.0)); double c(Minkowski(BTBIBTalpha, BTBIBTalpha)); // Calculate discriminant and exit if negative double discriminant = b*b - 4.0 * a * c; if (discriminant < 0.0) { return -2; } // Find possible DELTA values double DELTA1 = ( -b + SQRT(discriminant) ) / ( 2.0 * a ); double DELTA2 = ( -b - SQRT(discriminant) ) / ( 2.0 * a ); // We need to define M matrix M(0,0) = 1.0; M(1,1) = 1.0; M(2,2) = 1.0; M(3,3) = - 1.0; // Find possible position solutions with their implicit radii solution1 = M * BTBI * ( BT * DELTA1 * tau + BT * alpha ); double radius1(RSS(solution1(0), solution1(1), solution1(2))); solution2 = M * BTBI * ( BT * DELTA2 * tau + BT * alpha ); double radius2(RSS(solution2(0), solution2(1), solution2(2))); // Let's choose the right solution if ( ChooseOne ) { if ( ABS(CloseTo-radius1) < ABS(CloseTo-radius2) ) { X = solution1; } else { X = solution2; } } else { // Both solutions will be reported X = solution1; SecondSolution = solution2; } return 0; } // end of first "try" catch(Exception& e) { GPSTK_RETHROW(e); } } // end Bancroft::Compute()
// ----------------------------------------------------------------------------- double Solution::findMedianSortedArrays(std::vector<int>& nums1, std::vector<int>& nums2) { return solution1(nums1, nums2); }
int solution(int N, int T, int value[50]) { if (rand() % 2) return solution1(N, T, value); else return solution2(N, T, value); }
bool isBalanced(TreeNode* root) { solution1(root); return chk; //return solution2(root, height); }
int main(int argc, char **argv) { int limit = 1000000; solution1(limit); //solution2(limit); // slow }
void TestApplyToLinearSystem3Unknowns() { const int SIZE = 10; DistributedVectorFactory factory(SIZE); Vec template_vec = factory.CreateVec(3); LinearSystem linear_system(template_vec, 3*SIZE); PetscTools::Destroy(template_vec); for (int i = 0; i < 3*SIZE; i++) { for (int j = 0; j < 3*SIZE; j++) { // LHS matrix is all 1s linear_system.SetMatrixElement(i,j,1); } // RHS vector is all 2s linear_system.SetRhsVectorElement(i,2); } linear_system.AssembleIntermediateLinearSystem(); Node<3>* nodes_array[SIZE]; BoundaryConditionsContainer<3,3,3> bcc33; // Apply dirichlet boundary conditions to all but last node for (int i = 0; i < SIZE-1; i++) { nodes_array[i] = new Node<3>(i,true); ConstBoundaryCondition<3>* p_boundary_condition0 = new ConstBoundaryCondition<3>(-1); ConstBoundaryCondition<3>* p_boundary_condition1 = new ConstBoundaryCondition<3>(-2); ConstBoundaryCondition<3>* p_boundary_condition2 = new ConstBoundaryCondition<3>( 0); bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition0, 0); bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition1, 1); bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition2, 2); } bcc33.ApplyDirichletToLinearProblem(linear_system); linear_system.AssembleFinalLinearSystem(); Vec solution = linear_system.Solve(); DistributedVector d_solution = factory.CreateDistributedVector(solution); DistributedVector::Stripe solution0(d_solution,0); DistributedVector::Stripe solution1(d_solution,1); DistributedVector::Stripe solution2(d_solution,2); for (DistributedVector::Iterator index = d_solution.Begin(); index != d_solution.End(); ++index) { if (index.Global!=SIZE-1) { TS_ASSERT_DELTA(solution0[index], -1.0, 0.000001); TS_ASSERT_DELTA(solution1[index], -2.0, 0.000001); TS_ASSERT_DELTA(solution2[index], 0.0, 0.000001); } } for (int i = 0; i < SIZE-1; i++) { delete nodes_array[i]; } PetscTools::Destroy(solution); }
void TestApplyToLinearSystem2Unknowns() { const int SIZE = 10; DistributedVectorFactory factory(SIZE); Vec template_vec = factory.CreateVec(2); LinearSystem linear_system(template_vec, 2*SIZE); PetscTools::Destroy(template_vec); for (int i = 0; i < 2*SIZE; i++) { for (int j = 0; j < 2*SIZE; j++) { // LHS matrix is all 1s linear_system.SetMatrixElement(i,j,1); } // RHS vector is all 2s linear_system.SetRhsVectorElement(i,2); } linear_system.AssembleIntermediateLinearSystem(); Node<3>* nodes_array[SIZE]; BoundaryConditionsContainer<3,3,2> bcc32; // Apply dirichlet boundary conditions to all but last node for (int i = 0; i < SIZE-1; i++) { nodes_array[i] = new Node<3>(i,true); ConstBoundaryCondition<3>* p_boundary_condition0 = new ConstBoundaryCondition<3>(-1); ConstBoundaryCondition<3>* p_boundary_condition1 = new ConstBoundaryCondition<3>(-2); bcc32.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition0, 0); bcc32.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition1, 1); } bcc32.ApplyDirichletToLinearProblem(linear_system); linear_system.AssembleFinalLinearSystem(); /* * Matrix should now look like * A = (1 0 0 0 .. 0) * (0 1 0 0 .. 0) * ( .. ) * (1 1 .. 1) * (1 1 .. 1) * and rhs vector looks like b=(-1, -2, -1, -2, ..., -1, -2, 2, 2) * so solution of Ax = b is x=(-1, -2, -1, -2, ..., -1, -2, ?, ?). */ Vec solution = linear_system.Solve(); DistributedVector d_solution = factory.CreateDistributedVector(solution); DistributedVector::Stripe solution0(d_solution,0); DistributedVector::Stripe solution1(d_solution,1); for (DistributedVector::Iterator index = d_solution.Begin(); index != d_solution.End(); ++index) { if (index.Global!=SIZE-1) // last element of each stripe is not tested -- see ? in previous comment { TS_ASSERT_DELTA(solution0[index], -1.0, 0.000001); TS_ASSERT_DELTA(solution1[index], -2.0, 0.000001); } } for (int i = 0; i < SIZE-1; i++) { delete nodes_array[i]; } PetscTools::Destroy(solution); }
int solution(int n) { if (rand() % 2) return solution1(n, n); else return solution2(n, n); }
//递归 int solution1(int n, int m) { if (n == 1 || m == 1) return 1; else if (m == n) return solution1(n, m - 1) + 1; else if (m < n) return solution1(n - m, m) + solution1(n, m - 1); else return solution1(n, n); }
int solution1(TreeNode* root) { if(root == NULL) return 0; return 1 + max(solution1(root->left), solution1(root->right)); }