首页 > 其他分享 >HJ80 整型数组合并

HJ80 整型数组合并

时间:2023-07-11 22:22:58浏览次数:35  
标签:last 数组 int ++ 整型 arr1 sb HJ80 arr2

1. 题目

读题

HJ80 整型数组合并

 

 

考查点

 

2. 解法

思路

 

代码逻辑

 

具体实现

 

public class HJ080 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) {
arr1[i] = sc.nextInt();
}
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) {
arr2[i] = sc.nextInt();
}
System.out.println(mergeArray(arr1, arr2));
}

public static String mergeArray(int[] arr1, int[] arr2) {

Arrays.sort(arr1);
Arrays.sort(arr2);

StringBuffer sb = new StringBuffer();
int i = 0, j = 0;
int last = Integer.MIN_VALUE;
while (i < arr1.length && j < arr2.length) {

if (arr1[i] <= arr2[j]) {
if (arr1[i] == last) {
i++;
continue;
}
sb.append(arr1[i]);
last = arr1[i];
i++;
} else {
if (arr2[j] == last) {
j++;
continue;
}
sb.append(arr2[j]);
last = arr2[j];
j++;
}
}
while (i < arr1.length) {
if (last == arr1[i]) {
i++;
continue;
}
sb.append(arr1[i]);
last = arr1[i];
i++;
}
while (j < arr2.length) {
if (last == arr2[j]) {
j++;
continue;
}
sb.append(arr2[j]);
last = arr2[j];
j++;
}


return sb.toString();
}
}

3. 总结

标签:last,数组,int,++,整型,arr1,sb,HJ80,arr2
From: https://www.cnblogs.com/shoshana-kong/p/17542170.html

相关文章

  • 「学习笔记」后缀数组
    感谢LB学长的博文!前置知识后缀是指从某个位置\(i\)开始到整个串末尾结束的一个特殊子串,也就是\(S[i\dots|S|-1]\)。计数排序-OIWiki(oi-wiki.org)基数排序-OIWiki(oi-wiki.org)变量后缀数组最主要的两个数组是sa和rk。sa表示将所有后缀排序后第\(i\)小......
  • 「模板」树状数组
    引入题目描述给定\(n\)个数\(a[1],a[2],a[3]...a[n]\),现在又下面两种操作:1.询问区间\([x,y]\)的和,并输出。2.将下标为\(x\)的数增加\(val\)。一共\(x\)此操作\(1\len,m\le100000\),保证在\(int\)范围内。方法一:暴力枚举定义数组\(a\)储存\(n\)个元素。求区间和的时间复......
  • 动态数组和C++ std::vector详解
    目录1.std::vector2.vector的用法    2.1vector的定义和声明    2.2成员函数        2.2.1基本函数            operator=            assign            get_allocator        2.2.2元素访问   ......
  • 代码随想录算法训练营第二十九天| 1005.K次取反后最大化的数组和 134. 加油站 135. 分
      860.柠檬水找零 思路:遇到20,先给10和5,再给三个5代码:1boollemonadeChange(vector<int>&bills){2if(bills.size()==0)returntrue;34map<int,int>currentMoney;5for(inti=0;i<bills.size();i++)6{7if......
  • vue3中父组件与组件之间参数传递,使用(defineProps/defineEmits),涉及属性传递,对象传递,
    Vue3中子父组件之间的通信一、父组件传递参数到子组件采用defineProps传递属性父组件:<template><div><h1>这是父组件</h1><h1>父组件像子组件传递参数</h1><h2>传递属性值</h2><HH:fatherMessage="fatherMessage":valNum="valNum":valBool=......
  • 挑战程序竞赛系列(70):4.7后缀数组(2)
    挑战程序竞赛系列(70):4.7后缀数组(2)传送门:POJ1509:GlassBeads题意:ThedescriptionofthenecklaceisastringA=a1a2…amspecifyingsizesoftheparticularbeads,wherethelastcharacteramisconsideredtoprecedecharactera1incircularfashion.Thedisjoin......
  • LeetCode 215. 数组中的第K个最大元素
    小根堆classSolution{public:intfindKthLargest(vector<int>&nums,intk){priority_queue<int,vector<int>,greater<int>>q;for(autox:nums){if(q.size()<k)q.push(x);......
  • python练手项目——给数组中的每个字段加上双引号
    前言工作中经常会遇到一种场景:复制值时,会复制出来几个甚至十几个字段。把这些字段放入SQL语句或者接口里面时,需要手动给每个字段加上引号,很浪费时间。因此我想要写一个python脚本,给字段自动加上引号。测试数据1:上海武汉广州深圳北京内蒙古呼和浩特2:张三,李四,王五,......
  • #yyds干货盘点# LeetCode程序员面试金典:区域和检索 - 数组不可变
    1.简述:给定一个整数数组 nums,处理以下类型的多个查询:计算索引 left 和 right (包含left和right)之间的nums元素的和,其中 left<=right实现NumArray类:NumArray(int[]nums)使用数组nums初始化对象intsumRange(inti,intj)返回数组nums 中索引 left 和 r......
  • 【java】数组的常用操作
    sortstaticvoidsort(int[]a):将a数组按照从小到大进行排序staticvoidsort(int[]a,intfromIndex,inttoIndex):将a数组的[fromIndex,toIndex)部分按照升序排列staticvoidsort(Object[]a):根据元素的自然顺序对指定对象数组按升序进行排序。static<T>voidsort(T......