Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
TreeNode* invertTree(TreeNode* root)
{
if(NULL == root)
return NULL;
else
{
TreeNode* newleft=invertTree(root->right);
TreeNode* newright=invertTree(root->left);
root->left=newleft;
root->right=newright;
return root;
}
}
非递归的方法
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Assume that the total area is never beyond the maximum possible value of int.
int Solution::computeArea(int A,int B,int C,int D,int E,int F,int G,int H)
{
int area1=(C-A)*(D-B);
int area2=(G-E)*(H-F);
int over_area=overlap(A,B,C,D,E,F,G,H);
int area=area1+area2-over_area;
return area;
}
int Solution::overlap(int A,int B,int C,int D,int E,int F,int G,int H)
{
int h1=Math.max(A,E);
int h2=Math.min(C,G);
int h=h2>h1?h2-h1:0;
int w1=Math.max(B,F);
int w2=Math.min(D,H);
int w=w2>w1?w2-w1:0;
return w*h;
}
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
editorial solution
bool isPowerOfThree(int n)
{
int e=3;//e=4是以4为指数
double x=log10(n)/log10(3);
return x-int(x) == 0;// 判断double x 为整数
}
Given an integer, write a function to determine if it is a power of two.
bool isPowerOfTwo(int n)
{
int remainder;
bool flag=true;
if(n<=0)
return false;
while(n != 1)
{
remainder=n%2;
flag=true;
if(remainder !=0)
{
flag=false;
break;
}
n=n/2;
}
return flag;
}
bool Solution::isPowerOfTwo(int n)
{
if(n<=0)
return false;
if(n == 1)
return true;
else
{
int remainder=n%2;
if(remainder == 0)
{
n=n/2;
return isPowerOfTwo(n);
}
else
return false;
}
}
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
int Solution::majorityElement(vector<int>& nums)
{
unordered_map<int,int> freqs;
for(int i=0;iint majorE=0;
for(auto it:freqs)
{
if(it.second>nums.size()/2)
{
majorE= it.first;
break;
}
}
return majorE;
}
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
int Solution::titleToNumber(string s)
{
if(s.empty())
return 0;
reverse(s.begin(),s.end());
int n=0;
for(int i=0;i<s.size();i++)
{
int a=(s[i]-'A'+1)*pow(26,i);
n=n+a;
}
return n;
}
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.
char Solution::findTheDifference(string s,string t)
{
bool isDif=false;
if(s=="")
return t[0];
char a;
for(int i=0;itrue;
for(int j=0;j0;j++)
{
if(s[j]==t[i])
{
isDif=false;
s.erase(j,1);
break;
}
}
if(isDif==true)
{
a= t[i];
break;
}
}
return a;
}
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
// 错误的算法
string Solution::convertToTitle(int n)
{
string titleStr;
int remainder=n%26;
while(n/26 != 0)
{
remainder=n%26;
if(remainder==0)
{
titleStr.push_back('Z');
}
else
{
titleStr.push_back('A'+remainder-1);
}
n=n-remainder*26;
}
if(remainder !=0 )
{
titleStr.push_back('A'+n-1);
}
reverse(titleStr.begin(),titleStr.end());
return titleStr;
}
string Solution::convertToTitle(int n)
{
string titleStr="";
while(n !=0)
{
int remainder=(n-1)%26;
titleStr.push_back('A'+remainder);
n=(n-1)/26;
}
//反转字符串
reverse(titleStr.begin(),titleStr.end());
return titleStr;
}
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
// definition for singly-linked list
struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};
void Solution::delectNode(ListNode* node)
{
if(node == NULL || node->next == NULL) return;
node->val=node->next->val;
node->next=node->next->next;
}
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
// Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//wrong code
bool Solution::isSameTree_wrong(TreeNode* p,TreeNode* q)
{
if(p->val != q->val)
return false;
else if((p->left == NULL && q->left != NULL) || (p->left != NULL && q->left == NULL)||
(p->right == NULL && q->right != NULL) || (p->right != NULL && q->right == NULL))
return false;
else
{
bool left=isSameTree_wrong(p->left,q->left);
bool right=isSameTree_wrong(p->right,q->right);
if(left && right)
return true;
else
return false;
}
}
bool isSameTree(TreeNode* p,TreeNode* q)
{
if(p ==NULL && q != NULL)
return false;
else if(p !=NULL && q ==NULL)
return false;
else if(p == NULL && q == NULL)
return true;
else if((p != NULL && q != NULL)&& (p->val !=q->val))
return false;
else
{
return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
}
}
struct TreeNode p1(1),p2(2),p3(3);
p1.left=&p2;
p1.right=&p3;
struct TreeNode q1(1),q2(2),q3(3);
q1.left=&q2;
q1.right=&q3;
Solution Sol;
bool x=false;
x=Sol.isSameTree(&p1,&q1);
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
bool isUgly(int num)
{
if(num <= 0)
return false;
else if(num==1)
return true;
while(num%2 == 0) num=num/2;
while(num%3 == 0) num=num/3;
while(num%5 == 0) num=num/5;
if(num == 1)
return true;
else
return false;
}
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
bool isHappy(int n)
{
int n_src=n;//保存原来的n
int loopnum=0;
while(n != 1)
{
loopnum++;
vector<int> digitsVec;
while(n/10 !=0)
{
digitsVec.push_back(n%10);
n=n/10;
}
digitsVec.push_back(n);
n=0;
for(int i=0;iif(n == n_src || loopnum >100)
return false;
}
return true;
}