n2

inorder of two BST

北慕城南 提交于 2020-02-07 08:03:49
给两个bst,把它们的值按照从小到大打印。 1 public static void print2BSTInorder(TreeNode n1, TreeNode n2, List<Integer> result) { 2 Stack<TreeNode> stack1 = new Stack<>(); 3 Stack<TreeNode> stack2 = new Stack<>(); 4 5 do { 6 while (n1 != null) { 7 stack1.push(n1); 8 n1 = n1.left; 9 } 10 while (n2 != null) { 11 stack2.push(n2); 12 n2 = n2.left; 13 } 14 if (stack2.isEmpty() || stack1.peek().value < stack2.peek().value) { 15 n1 = stack1.pop(); 16 result.add(n1.value); 17 n1 = n1.right; 18 } else { 19 n2 = stack2.pop(); 20 result.add(n2.value); 21 n2 = n2.right; 22 } 23 } while (!stack1.isEmpty() || n1 != null || n2

PAT Advanced 1088 Rational Arithmetic (20分)

你说的曾经没有我的故事 提交于 2020-02-05 19:10:36
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2 . The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers. Output Specification: For each test case, print in 4 lines the sum, difference, product and quotient of the two

Introduction to Mathematical Thinking-Problem 3

霸气de小男生 提交于 2020-02-03 05:39:04
3.Say whether the following is true or false and support your answer by a proof. For any integer n n n , the number n 2 + n + 1 n^2+n+1 n 2 + n + 1 is odd. Proof: n 2 + n + 1 = n ( n + 1 ) + 1 n^2+n+1=n(n+1)+1 n 2 + n + 1 = n ( n + 1 ) + 1 . As n n n and n + 1 n+1 n + 1 are two consecutive integers, one of them must be even, so their product is even. Therefore, n 2 + n + 1 = n ( n + 1 ) + 1 n^2+n+1=n(n+1)+1 n 2 + n + 1 = n ( n + 1 ) + 1 is odd. 来源: CSDN 作者: yuh_yeet 链接: https://blog.csdn.net/yuh_yeet/article/details/104143896

黑格覆盖(前缀和)

时光总嘲笑我的痴心妄想 提交于 2020-02-01 09:23:49
黑格覆盖 在一张由 M * N 个小正方形格子组成的矩形纸张上,有 k 个格子被涂成了黑色。给你一张由 m * n 个同样小正方形组成的矩形卡片,请问该卡片最多能一次性覆盖多少个黑格子? 输入 输入共 k+1 行: 第 1 行为 5 个整数 M、N、m、n、k,其含义如题目所述。 接下来 k 行,每行 2 个整数,分别表示被涂成黑色的格子的行、列坐标。 输出 输出共 1 行,1 个整数,表示卡片一次性最多能覆盖的黑格子数。 样例输入 Copy 3 5 2 2 3 1 1 2 2 3 5 样例输出 Copy 2 提示 根据样例数据所得到的涂完黑格的矩形和用于覆盖的矩形如下图所示: 对于 40%的数据:m=n; 对于 100%的数据:M、N、m、n、k 均小于等于 1000,所有黑格不重复出现。 1 分别计算每行的黑格数的前缀和,和每列的黑格数的前缀和 2 找 m2=min(m1,m); n2=min(n1,n);让卡片正放,暴力找最大值。根据容斥定理可知 ans=max(tu[i][j]-tu[i-m2][j]-tu[i][j-n2]+tu[i-m2][j-n2],ans); 3.将卡片倒放,再次暴力计算; 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e3+10; 4 5 int tu[N][N]

Java算法——排序算法

梦想的初衷 提交于 2020-01-31 22:47:45
文章目录 排序的分类 测试代码 冒泡排序(Bubble Sorting) 选择排序(Select Sorting) 插入排序(Insert Sorting) 希尔排序(Shell Sorting) 快速排序(Quick Sorting) 排序的分类 内部排序 将需要处理的所有数据都加载到内部存储器中进行排序。 外部排序 数据量过大,无法全部加载到内存中,需要借助外部存储进行排序。 内部排序分类: 插入排序(直接插入排序、希尔排序) 选择排序(简单选择排序、堆排序) 交换排序(冒泡排序、快速排序) 归并排序 基数排序 排序法 平均时间 最差情况 稳定度 额外空间 备注 冒泡 O(n2) O(n2) 稳定 O(1) n小时较好 交换 O(n2) O(n2) 不稳定 O(1) n小时较好 选择 O(n2) O(n2) 不稳定 O(1) n小时较好 插入 O(n2) O(n2) 稳定 O(1) 大部分已排序时较好 基数 O(logRB) O(logRB) 稳定 O(n) B是真数(0-9)R是基数(个十百) shell O(nlogn) O(n^s)1<s<2 不稳定 O(1) s是所选分组 快排 O(nlogn) O(n2) 不稳定 O(nlogn) n大时较好 归并 O(nlogn) O(nlogn) 稳定 O(1) n大时较好 堆 O(nlogn) O(nlogn) 不稳定 O(1)

26.方法

安稳与你 提交于 2020-01-31 08:27:37
方法就是一段用来完成特定功能的代码片段,类似于其它语言的函数。 方法用于定义该类或该类的实例的行为特征和功能实现。 方法是类和对象行为特征的抽象。方法很类似于面向过程中的函数。面向过程中,函数是最基本单位,整个程序由一个个函数调用组成。面向对象中,整个程序的基本单位是类,方法是从属于类和对象的。 public class Test20 { /** main方法:程序的入口 */ public static void main ( String [ ] args ) { int num1 = 10 ; int num2 = 20 ; //调用求和的方法:将num1与num2的值传给add方法中的n1与n2 // 求完和后将结果返回,用sum接收结果 int sum = add ( num1 , num2 ) ; System . out . println ( "sum = " + sum ) ; //输出:sum = 30 //调用打印的方法:该方法没有返回值 print ( ) ; } /** 求和的方法 */ public static int add ( int n1 , int n2 ) { int sum = n1 + n2 ; return sum ; //使用return返回计算的结果 } /** 打印的方法 */ public static void print (

[Algorithm] 88. Merge Sorted Array

一曲冷凌霜 提交于 2020-01-30 04:55:35
Given two sorted integer arrays nums1 and nums2 , merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is greater or equal to m + n ) to hold additional elements from nums2 . Example: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] /** * @param {number[]} nums1 * @param {number} m * @param {number[]} nums2 * @param {number} n * @return {void} Do not return anything, modify nums1 in-place instead. */ var merge = function(nums1, m,

LeetCode 466 - Count The Repetitions - Hard ( Python)

冷暖自知 提交于 2020-01-30 03:52:30
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”. You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2] . Find the maximum integer M such

多点求值与快速插值

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 01:32:19
多点求值 给出 n n n 次多项式 A ( x ) A(x) A ( x ) ,求出 m m m 个 x i x_i x i ​ 对应的 A ( x i ) A(x_i) A ( x i ​ ) 考虑分治,设 L ( x ) = ∏ i = 1 n 2 ( x − x i ) L(x)=\prod_{i=1}^{\frac{n}{2}}(x-x_i) L ( x ) = ∏ i = 1 2 n ​ ​ ( x − x i ​ ) , R ( x ) = ∏ i = n 2 + 1 n ( x − x i ) R(x)=\prod_{i=\frac{n}{2}+1}^n(x-x_i) R ( x ) = ∏ i = 2 n ​ + 1 n ​ ( x − x i ​ ) 对于 i ∈ [ 1 , n 2 ] , F ( x i ) = ( F m o d    L ) ( x i ) i \in [1,\frac{n}{2}],F(x_i)=(F \mod L)(x_i) i ∈ [ 1 , 2 n ​ ] , F ( x i ​ ) = ( F m o d L ) ( x i ​ ) , 对于 i ∈ ( n 2 , n ] , F ( x i ) = ( F m o d    R ) ( x i ) i \in (\frac{n}{2},n],F(x_i)=(F \mod R)

Leetcode初学——实现strStr()

烈酒焚心 提交于 2020-01-27 12:04:40
题目 这道题其实很简单,但是我们要注意的是,不要调用系统原有的函数,这样会使得这道题变的毫无意义 奉上代码: class Solution { public int strStr(String haystack, String needle) { if(needle.equals("")) return 0; int n1=haystack.length(); int n2=needle.length(); for(int i=0;i<=n1-n2;i++){ int j=0; if(haystack.charAt(i)==needle.charAt(j)){ while(j<n2){ if(haystack.charAt(i+j)==needle.charAt(j)){ j++; }else { break; } } if(j==n2) return i; } } return -1; } } 运行结果如下: 来源: CSDN 作者: 艾姆鸥 链接: https://blog.csdn.net/qq_39377543/article/details/104091207