leetcode题解日练--2016.6.25

编程日记,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。标红题为之后还需要再看的题目。

今日题目:1、用队列实现栈;2、Bulls and Cows (猜数字,找出完全猜中的数字和只猜中值未猜中位置的数字);3、同构字符串;4、求矩形面积;5、合并两个链表。

225. Implement Stack using Queues | Difficulty: Easy

Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
题意:用队列去实现栈
思路:

代码:
C++

class Stack {
public:
    queue<int> Q;
    // Push element x onto stack.
    void push(int x) {
        Q.push(x);
        for(int i=0;i<Q.size()-1;i++)
        {
            Q.push(Q.front());
            Q.pop();
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        Q.pop();
    }

    // Get the top element.
    int top() {
        return Q.front();
    }

    // Return whether the stack is empty.
    bool empty() {
        return Q.size()==0;
    }
};

结果:0ms

299. Bulls and Cows | Difficulty: Easy

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:

Secret number: “1807”
Friend’s guess: “7810”
Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.

Please note that both secret number and friend’s guess may contain duplicate digits, for example:

Secret number: “1123”
Friend’s guess: “0111”
In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.
题意:判断一个数独是否合法。
思路:
1、找a+b的和与找到a,然后相减就是b
代码:
C++

class Solution {
public:
    string getHint(string secret, string guess) {
        int a[10] = {0};
        int count = 0;
        int Cnt_a=0,Cnt_b=0;
        //先遍历一遍找a的个数
        for(int i=0;i<secret.length();i++)
        {
            if(secret[i]==guess[i])
                Cnt_a++;
        }
        //遍历第二遍找a+b的个数
        for(int i=0;i<secret.length();i++)
        {
            a[secret[i]-'0']++;
            a[guess[i]-'0']--;
        }
        for(int i=0;i<10;i++)
        {
            if(a[i]>0)
                count+=a[i];
        }

        Cnt_b = secret.length()-count-Cnt_a;

        return to_string(Cnt_a)+'A'+to_string(Cnt_b)+'B';

    }
};

结果:4ms

2、用一个数组存没猜中的数字,另一个数字存猜中的数字。
C++

class Solution {
public:
    string getHint(string secret, string guess) {
        int a[10] = {0};
        int count = 0;
        int Cnt_a=0,Cnt_b=0;
        //先遍历一遍找a的个数
        for(int i=0;i<secret.length();i++)
        {
            if(secret[i]==guess[i])
                Cnt_a++;
            else
            {
                a[secret[i]-'0']++;
                a[guess[i]-'0']--;
            }
        }
        for(int i=0;i<10;i++)
        {
            if(a[i]>0)
                count+=a[i];
        }

        Cnt_b = secret.length()-count-Cnt_a;

        return to_string(Cnt_a)+'A'+to_string(Cnt_b)+'B';

    }
};

结果:4ms

205. Isomorphic Strings | Difficulty: Easy

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given “egg”, “add”, return true.

Given “foo”, “bar”, return false.

Given “paper”, “title”, return true.
题意:同构字符串,就是做一些映射之后相等的字符串
思路:

C++

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int map1[256] = {0},map2[256] = {0};
        if(s.size()==0) return true;
        for(int i=0;i<s.size();i++)
        {
                if(map1[s[i]]!=map2[t[i]]) return false;
                else
                    {
                        map1[s[i]] =i+1;
                        map2[t[i]] =i+1;
                    }
        }
        return true;
    }
};

结果:8ms

223. Rectangle Area | Difficulty: Easy

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.
题意:给定坐标,求矩形面积。
思路:
1、找到公共部分的面积
代码:
C++

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    //左是两个矩形的左边更靠右边的值
    int left = max(A,E);
    //右是两个矩形更靠右边的边的更小的值但是它至少应该大于等于左
    int right = max(left,min(C,G));
    //下是两个矩形下边更大的
    int bottom = max(B,F);
    //上是两个矩形上边更小的那个,但它的值至少应该大于等于bottom
    int top = max(bottom,min(D,H));
    return (C-A)*(D-B)+(G-E)*(H-F)-(top-bottom)*(right-left);
}
};

结果:48ms

2、

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int l = min(max(A, E), C);
        int r = min(max(A, G), C);
        int t = min(max(B, H), D);
        int b = min(max(B, F), D);
        return (C-A)*(D-B) + (G-E)*(H-F) - (r-l)*(t-b);
    }
};

结果:32ms

160. Intersection of Two Linked Lists | Difficulty: Easy

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A: a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3
begin to intersect at node c1.

题意:合并两个链表
思路:
1、一个链表走到头就直接赋给下一个链表的头
代码:
C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* p1 = headA;
        ListNode* p2 = headB;
        if(p1==NULL||p2==NULL)  return NULL;
        while(p1!=p2)
        {
            p1 = p1->next;
            p2 = p2->next;
            if(p1==p2)    return p1;
            if(p1==NULL)    p1=headB;
            if(p2==NULL)    p2=headA;
        }
        return p1;
    }
};

结果:52ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值