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;
}
326. Power of Three && 342. Power of Four
问题描述
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?
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.
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
思路
进制为26,起始值从开始
先将string s反转,然后从低位开始计算到高位
代码
int Solution::titleToNumber(string s)
{
if(s.empty())
return0;
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;
}
389. Find the Difference
问题描述
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.
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
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(intx):val(x),next(NULL){}
};
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) {}
};
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.
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