5304. subarray XOR inquiry
analysis:
Method 1: violence to solve: each cycle, from the Li到Ri
XOR and, into vector and returns; this approach will undoubtedly overtime;
1 class Solution { 2 public: 3 vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) { 4 vector<int> res; 5 vector<vector<int>> dp; 6 vector<int> level; 7 int len=arr.size(); 8 int count; 9 //循环找 10 for(int i=0;i<queries.size();i++) 11 { 12 count=0; 13 int x = queries[i][0]>queries[i][1]?queries[i][1]:queries[i][0]; 14 int y = queries[i][0]>queries[i][1]?queries[i][0]:queries[i][1]; 15 for(int i=x;i<=y;i++) 16 { 17 count^=arr[i]; 18 } 19 res.push_back(count); 20 } 21 22 return res; 23 } 24 };
Method 2: two-dimensional array: DP [i] [j] denotes the i to j and XOR, dp [i] [j] = dp [i] [j-1] ^ arr [j]; when the number when the number is n, n * n open space required, and 1/2 of wasted space, because dp [i] [j] = dp [j] [i];
1 class Solution { 2 public: 3 vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) { 4 vector<int> res; 5 vector<vector<int>> dp; 6 int len=arr.size(); 7 dp.resize(len,arr); //初始化 8 for(int i=0;i<len;i++) 9 { 10 for( Int j = i; j <len; j ++ ) . 11 { 12 is // fill the array dp dp [i] [j] from i to j represents the result of the exclusive OR 13 is IF (i == j) 14 dp [i] [ J] = ARR [I]; 15 the else 16 DP [I] [J] DP = [I] [J- . 1 ] ^ ARR [J]; . 17 } 18 is } . 19 20 is // cycle find 21 is for ( int I = 0 ; I <queries.size (); I ++ ) 22 is { 23 is //int x = queries[i][0]>queries[i][1]?queries[i][1]:queries[i][0]; 24 //int y = queries[i][0]>queries[i][1]?queries[i][0]:queries[i][1]; 25 res.push_back(dp[queries[i][0]]queries[i][1]]); 26 } 27 28 return res; 29 } 30 };
Method 3: 2 on the basis of the method, cut half the space, but the results still exceeded the maximum space-consuming;
1 class Solution { 2 public: 3 vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) { 4 vector<int> res; 5 vector<vector<int>> dp; 6 vector<int> level; 7 int len=arr.size(); 8 for(int i=0;i<len;i++) 9 { 10 level.resize (len-i, 0 ); . 11 for ( int j = 0 ; j <len-i; j ++ ) 12 is { 13 is // fill the array dp dp [i] [j] represents from i to j XOR results 14 IF (J == 0 ) 15 Level [J] = ARR [J + I]; 16 the else . 17 Level [J] = Level [J- . 1 ] ^ ARR [J + I]; 18 is } . 19 dp.push_back (Level ); 20 } 21 is 22 is // cycle find 23 is for(int i=0;i<queries.size();i++) 24 { 25 int x = queries[i][0]>queries[i][1]?queries[i][1]:queries[i][0]; 26 int y = queries[i][0]>queries[i][1]?queries[i][0]:queries[i][1]; 27 res.push_back(dp[x][y-x]); 28 } 29 30 return res; 31 } 32 };
Method 4: read the solution to a problem, but also know that such a calculation, direct reservation before Ri term XOR and before and before Li item XOR and XOR operation, the same item before Li was offset by, space consumption directly into the n.
1 class Solution { 2 public: 3 vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) { 4 vector<int> dp; 5 dp.resize(arr.size(),0); 6 dp[0]=arr[0]; 7 for(int i=1;i<arr.size();i++) 8 { 9 dp[i]=dp[i-1]^arr[i]; 10 } 11 12 vector<int> res; 13 for(int i=0;i<queries.size();i++) 14 { 15 int l=queries[i][0]; 16 int r=queries[i][1]; 17 int num=(l==0)?dp[r]:(dp[r]^dp[l-1]); 18 res.push_back(num); 19 } 20 return res; 21 } 22 };