【leetcode】Contains Duplicate & Rectangle Area(easy)

Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

思路:简单题用set

bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> myset;
        for(int i = 0; i < nums.size(); ++i)
        {
            if(myset.find(nums[i]) != myset.end())
                return true;
            myset.insert(nums[i]);
        }
        return false;
    }

Rectangle Area

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 computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int up = min(D, H);
        int down = max(B, F);
        int left = max(A, E);
        int right = min(C, G);
        int In = (up > down && right > left) ? (up - down) * (right - left) : 0;
        int area1 = (C - A) * (D - B);
        int area2 = (G - E) * (H - F);

        return area1 + area2 - In;
    }
时间: 2024-10-06 00:16:21

【leetcode】Contains Duplicate & Rectangle Area(easy)的相关文章

【leetcode】Merge Two Sorted Lists(easy)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:使用伪头部 class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode fakehead(0)

【leetcode】Remove Linked List Elements(easy)

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 思路:简单题. //Definition for singly-linked list. struct ListNod

【leetcode】Linked List Cycle II (middle)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离

【leetcode】Reverse Linked List II (middle)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 思路: 好困啊,脑子晕晕的. 转了半天AC了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

【leetcode】Container With Most Water(middle)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

【leetcode】Evaluate Reverse Polish Notation(middle)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

【leetcode】Merge k Sorted Lists (归并排序)

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个有序链表,最后返回一个总的有序链表,分析并描述其复杂度.该题的实质为归并排序,平均时间复杂度为O(NlogN). Java AC代码: public class Solution { public ListNode mergeKLists(List<ListNode> list

【leetcode】Binary Search Tree Iterator(middle)

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses