[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

9.palindrome Number

Uploaded by

s0618614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

9.palindrome Number

Uploaded by

s0618614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

9.

PALINDROME NUMBER

class Solution {

public:

bool isPalindrome(int x) {

if(x<0)

return false;

long int x_reverse{0}, n=x, rem{0};

while(n){

rem = n%10;

x_reverse = x_reverse*10+rem;

n/=10;

if(x_reverse == x)

return true;

return false;

};

67.ADD BINARY

class Solution {

public:

string addBinary(string a, string b) {

string result = "";

int i = a.length() - 1, j = b.length() - 1, carry = 0;

while (i >= 0 || j >= 0 || carry) {

int total = carry;

if (i >= 0) total += a[i--] - '0';

if (j >= 0) total += b[j--] - '0';

result += (total % 2) + '0';

carry = total / 2;

}
reverse(result.begin(), result.end());

return result;

};

415. Add Strings

class Solution {

public:

int charToInt(char s){

return s - '0';

char intToChar(int a){

return a + '0';

string solve(string &num1, string &num2){

if(num1.length() > num2.length()) swap(num1, num2);

reverse(num1.begin(), num1.end());

reverse(num2.begin(), num2.end());

string result = "";

int carry = 0;

for(int i=0;i<num1.length();i++){

int a = charToInt(num1[i]);

int b = charToInt(num2[i]);

int sum = a+b + carry;

int ans = sum %10;

carry = sum/10;

result += intToChar(ans);

for(int i= num1.length();i<num2.length();i++){

int a = charToInt(num2[i]);

int sum = a + carry;


int ans = sum %10;

carry = sum/10;

result += intToChar(ans);

if(carry) result += "1";

reverse(result.begin(),result.end());

return result;

string addStrings(string num1, string num2) {

return solve(num1, num2);

};

700. Search in a Binary Search Tree

class Solution {

public:

TreeNode* searchBST(TreeNode* root, int val) {

if(root==NULL) return NULL;

if(root->val==val) return root;

if(root->val>val) return searchBST(root->left , val);

return searchBST(root->right , val);

};

You might also like