编程题训练4
来源:互联网 发布:输入框获取焦点js 编辑:程序博客网 时间:2024/06/01 16:24
编程题训练4
主元素
- 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
样例
给出数组[1,1,1,1,2,2,2],返回 1
public class Solution { /** * @param nums: a list of integers * @return: find a majority number */ public int majorityNumber(ArrayList<Integer> nums) { // write your code HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0;i< nums.size();i++){ int num = nums.get(i); if(map.containsKey(num)){ map.put(num,map.get(num) + 1); }else{ map.put(num,1); } if(map.get(num) > nums.size()/2) return num; } return 0; }}
加一
- 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。
该数字按照大小进行排列,最大的数在列表的最前面。
样例
给定 [1,2,3] 表示 123, 返回 [1,2,4].
给定 [9,9,9] 表示 999, 返回 [1,0,0,0]
public class Solution { /** * @param digits a number represented as an array of digits * @return the result */ public int[] plusOne(int[] digits) { // Write your code here while(digits == null || digits.length ==0){ return digits; } int carryVal = 0; int carry = 10; for(int i = digits.length - 1; i >= 0; i--){ int num = digits[i]; if(num +1 == 10){ digits[i] = 0; if(i == 0){ int[] a = new int[digits.length +1]; for(int j = 0; j < digits.length +1; j++){ if(j ==0){ a[j] = digits[j] +1; }else{ a[j] = 0; } } return a; } }else{ digits[i] = num+1; return digits; } } return digits; }}
将两个排序链表合并为一个新的排序链表
样例
给出 1->3->8->11->15->null,2->null, 返回 1->2->3->8->11->15->null。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param ListNode l1 is the head of the linked list * @param ListNode l2 is the head of the linked list * @return: ListNode head of linked list */ public ListNode mergeTwoLists(ListNode head1, ListNode head2) { // write your code here if (head1 == null) { return head2; } if (head2 == null) { return head1; } ListNode root = new ListNode(0); ListNode pointer = root; // 当两个链表都不为空就进行合并操作 while (head1 != null && head2 != null) { // 下面的操作合并较小的元素 if (head1.val < head2.val) { pointer.next = head1; head1 = head1.next; } else { pointer.next = head2; head2 = head2.next; } // 将指针移动到合并后的链表的末尾 pointer = pointer.next; } // 下面的两个if有且只一个if会内的内容会执行 // 如果第一个链表的元素未处理完将其,接到合并链表的最后一个结点之后 if (head1 != null) { pointer.next = head1; } // 如果第二个链表的元素未处理完将其,接到合并链表的最后一个结点之后 if (head2 != null) { pointer.next = head2; } // 返回处理结果 return root.next; }}
0 0
- 编程题训练4
- 编程题训练1
- 编程题训练3
- 编程题训练5
- JS编程训练 | 题4:数组中添加元素
- 编程训练
- 编程训练
- 有趣的逻辑思维训练编程题
- JS编程训练 | 题2:数组求和
- JS编程训练 | 题8:数组合并
- JS编程训练 | 题9:添加元素
- JS编程训练 | 题10:计数
- JS编程训练 | 题16:计时器
- Java集合编程题训练(一)
- OpenCV编程->Haar训练(4)
- 训练2 4题
- C++编程训练资源
- 编程训练矩阵问题
- 设计模式笔记十:装饰器模式
- PAT 1098-Insertion or Heap Sort (25)
- 编程题训练3
- MongoDB文档翻译-聚集-聚集管道优化
- 【解题报告】Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)(先占个位置,里面暂时什么都没有)
- 编程题训练4
- mvc:annotation-driven 元素,HttpMessageConverter,RequestMappingHandlerAdapter
- winform中treeview绑定数据库的数
- sklearn学习-SVM例程总结2(特征选择——单因素方差分析(方差分析anova ))
- Hibernate与 MyBatis的比较
- CSS3 Box-sizing
- Unity开发小技巧介绍
- linux(ubuntu) vi输入方向键会变成ABCD的解决方法
- MongoDB文档翻译-聚集-聚集管道的限制