Блог пользователя abhinav700

Автор abhinav700, история, 19 месяцев назад, По-английски

While declaring a link list, why do we create a new Node using Node* node = new Node(value) ? Can't we simply use Node node(val) if not, then why? and if we can, what are changes to our code we will have to make while using later syntax, for basic operations on link lists?

I was revising link list, and although I can solve the questions, I was confused with this syntax.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

Автор abhinav700, история, 2 года назад, По-английски

I was doing a question on leetcode and the part of it's solution required to find the rightmost set bit of an integer(it can also be negative).It is given that integer is a 32 bit number in constraints.

While finding out the position of right most bit using below function

int findPos(int allXor){
        int flag=1;
        int i;
        for( i=0;i<=32;i++)
            if((1<<i) & allXor )
                break;
        return i;
    }

i am unable to understand that how 1<<32 is going to work. It is because we are given a 32 bit number and 1<<32 would represent the integer which has it's 33rd bit set right? when i use i<=31 in this question i get the wrong answer.

Can someone pls explain what is happening here?

QUESTION LINK: https://leetcode.com/problems/single-number-iii/

FULL CODE:

class Solution {
public:
    int findPos(int allXor){
        int flag=1;
        int i;
        for( i=0;i<=32;i++)
            if((1<<i) & allXor )
                break;
        return i;
    }
    
    vector<int> singleNumber(vector<int>& nums) {
        if(nums.size()==2)
            return nums;
        vector<int> ans;
        int allXor=0;
        for(auto it: nums)
            allXor^=it;
        int bitPos=findPos(allXor);
        // cout<<allXor<<" "<<bitPos<<endl;
        vector<int> part1,part2;
        
        for(auto it: nums){
            if((1<<bitPos)&it)
                part1.push_back(it);
            else
                part2.push_back(it);
        }
        
        int element1=0,element2=0;
        for(auto it : part1)
        {
              element1^=it;
              cout<<it<<" ";
        }
        cout<<endl;
        for(auto it : part2)
        {
               element2^=it;
                cout<<it<<" ";
        }
        return {element1,element2};
    }
};

Полный текст и комментарии »

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

Автор abhinav700, история, 2 года назад, По-английски

Earlier, i was solving a problem. When i went to discuss section to see the solutions, there was a person who was following a syntax of for loop which i was not able to understand.

it was as follows: for (char &c : word) where word is of type std::string. can someone explain me how is it different from for (char c : word)? because i was getting run time error when i tried to remove & from c.

i tried running it my console, but still was not able to understand it's working properly.

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор abhinav700, история, 2 года назад, По-английски

i am doing a leetCode question which requires a 3d array for memorisation. i have followed every rule of using the memset function and creating the arrays still the array is not getting initialised with the value specified in memset function. I have been stuck on this from yesterday. Can someone pls help? Problem Link: https://leetcode.com/problems/knight-probability-in-chessboard/

CODE:


class Solution { public: int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{2,-1},{2,1},{1,2},{1,-2}}; double solve(int n,int k,int r,int c,double dp[101][26][26]){ if(r<0 || r>=n || c<0 || c>=n) return 0; if(k==0) return 1.0; if(dp[k][r][c]!=2.0){ cout<<"k r,c : "<<k<<" "<<r<<" "<<c<<endl; cout<<"value : "<<dp[k][r][c]<<endl<<endl; return dp[k][r][c]; } double rate=0; for(int i=0;i<8;i++){ rate+=(solve(n,k-1,r+dir[i][0],c+dir[i][1],dp)*0.125); } return dp[k][r][c]= rate; } double knightProbability(int n, int k, int row, int column) { double dp[101][26][26]; memset(dp,2.0,sizeof(dp[0][0][0])); cout<<dp[0][0][0]; solve(n,k,row,column,dp); return dp[k][row][column]; } };

Полный текст и комментарии »

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

Автор abhinav700, история, 3 года назад, По-английски
#include "bits/stdc++.h"
using namespace std;
 
int main()
{
 long long int q;
 cin>>q;
 while(q--)
 {
     long long int n,k;
     cin>>n>>k;
     vector<long long int> pos(k);
     for(long long int i=1;i<=k;i++)
     {
         cin>>pos[i];
     }
     vector<long long int> temp(k);
     for(long long int i=1;i<=k;i++)
     cin>>temp[i];

    vector<long long int> output(n,1e8);
    
   for(long long int i=1;i<=n;i++)
   {
       for(long long int j=1;j<=k;j++)
       {
           output[i]=min(output[i],temp[j]+abs(pos[j]-i));
       }
   }

    for(long long int i=1;i<=n;i++)
    cout<<output[i]<<" ";

    cout<<endl;
     
 }   
 return 0;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится