【PAT 乙级 1039.到底买不买 C++/Java】教程文章相关的互联网学习教程文章

LeetCode 57. Insert Interval 插入区间 (C++/Java)【代码】

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]]Example 2: Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16...

LeetCode 56. Merge Intervals 合并区间 (C++/Java)【代码】

题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. NOTE: input types have been changed on April 15, 2019. Pl...

LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)【代码】

题目: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents. The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed edge a...

LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)【代码】【图】

题目: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1). A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Each...

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)【代码】

题目: Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of path between two nodes is represented by the number of edges between them. Example 1: Input:5/ 4 5/ \ 1 1 5Output: 2 Example 2: Input:1/ 4 5/ \ 4 4 5Output: 2 N...

LeetCode 684. Redundant Connection 冗余连接(C++/Java)【代码】【图】

题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed. The resulting graph is given as a 2D-array of edges. Each element of edge...

iOS,C#,Java 生成随机数

C#写法//生成随机数范围1-6 int num001 = new Random().Next(1, 7); Console.WriteLine("生成随机 num001 is . {0}", num001); //生成随机数范围36.0~37.0 double num002 = new Random().Next(1, 10)/10.0f+36; Console.WriteLine("生成随机 num002 is . {0}", num002);

C++和Java的语法区别整理【代码】

前言: 多种语言同时使用时,总是会出现不适应的情况,所以标记一下他们的差异,方便参考。 正文: (1)创建和销毁对象: 差异:C++中的“A a;”在栈中创建了对象,可直接使用,Java中的这种形式则不会创建对象。 C++:A a; //对象占用栈空间,可以直接使用,生命周期为所在作用域,不需要手动销毁。 B* b = new B(); //对象占用堆空间,可以直接使用,生命周期无限,需要手动“delete b;”。Java:A a; //对象没有生成,不可直...

PAT 甲级 1046.Shortest Distance C++/Java【代码】

题目来源 The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For each case, the first line contains an integer N (in [3]), followed by N integer distances D?1?? D?2?? ? D?N??, where D?i?? is the distance between the i-th and the (-st e...

PAT 乙级 1055.集体照 C++/Java【代码】

题目来源 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下:每排人数为 /(向下取整),多出来的人全部站在最后一排;后排所有人的个子都不比前排任何人矮;每排中最高者站中间(中间位置为 /,其中 m 为该排人数,除法向下取整);每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者...

剑指Offer-54.字符流中第一个不重复的字符(C++/Java)【代码】

题目: 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 输出描述: 如果当前字符流没有存在出现一次的字符,返回#字符。 分析: 使用map将字符流中的每一个字符出现的次数记录下来,然后当调用FirstAppearingOnce()时,按字符流的顺序查找在map中出现的次数,如果为1,返...

剑指Offer-47.求1+2+3+...+n(C++/Java)【代码】

题目: 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 分析: 利用短路与来判断n是否大于0,从而实现递归求和。 程序: C++class Solution { public:int Sum_Solution(int n) {int res = n;bool flag = (n > 0) && (res += Sum_Solution(n-1));return res;} };Javapublic class Solution {public int Sum_Solution(int n) {int res = n;boolean flag = (n > 0) && (re...

剑指Offer-46.孩子们的游戏(圆圈中最后剩下的数)(C++/Java)【代码】【图】

题目: 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演...

剑指Offer-42.和为S的两个数字(C++/Java)【代码】

题目: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 输出描述: 对应每个测试案例,输出两个数,小的先输出。 分析: 因为数组时有序的,我们可以使用两个指针,分别指向数组的第一个元素和最后一个元素,判断两个数的和与S的关系。 如果相等,那么这两个数字便是最后的答案,按序输出即可。 如果两数和大于S的话,我们需要调整后面的指针所指...

如何将网络音频流保存到文件(c / java)

是否有任何lib或众所周知的方法来保存音频网络流(网络广播,mp3流)以编程方式归档?解决方法:您可以使用VLC项目中的libvlc.虽然现在wiki似乎下跌了.代码在c中. 编辑:通过谷歌缓存找到this和this.