leetcode题解日练--2016.6.25

2024-03-06 10:48
文章标签 leetcode 25 题解 日练 2016.6

本文主要是介绍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);//上是两个矩形上边更小的那个,但它的值至少应该大于等于bottomint 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

这篇关于leetcode题解日练--2016.6.25的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779820

相关文章

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划