1、最大间距(leetcode164)
归并排序后,进行遍历找出最大值
class Solution {
public int maximumGap(int[] nums) {
int len = nums.length, max = 0;
if(len < 2)return 0;
merge(nums, 0, len - 1 );
for(int i = 0, j = i + 1 ; i < len && j < len; i ++, j ++)
{
int num = Math.abs(nums[i] - nums[j]);
if(num > max )max = num;
}
return max;
}
public void merge(int []m, int l, int r)
{
if(l >= r)return;
int mid = (l + r) / 2;
merge(m, l, mid);
merge(m, mid + 1, r);
List<Integer> li = new ArrayList<>();
int i = l, j = mid + 1;
while(i <= mid && j <= r)
{
if(m[i] < m[j])
{
li.add(m[i++]);
}
else
{
li.add(m[j++]);
}
}
while(i <= mid)li.add(m[i++]);
while(j <= r)li.add(m[j++]);
for(int k = l,z = 0; k <= r; k++,z++)
{
m[k] = li.get(z);
}
}
}
2、排序数组(leetcode912)
归并排序
class Solution {
public int[] sortArray(int[] nums) {
merge(nums, 0, nums.length - 1);
return nums;
}
public void merge(int []m, int l, int r)
{
if(l >= r)return;
int mid = (l + r) / 2;
merge(m, l, mid);
merge(m, mid + 1, r);
List<Integer> li = new ArrayList<>();
int i = l, j = mid + 1;
while(i <= mid && j <= r)
{
if(m[i] < m[j])
{
li.add(m[i++]);
}
else
{
li.add(m[j++]);
}
}
while(i <= mid)li.add(m[i++]);
while(j <= r)li.add(m[j++]);
for(int k = l,z = 0; k <= r; k++,z++)
{
m[k] = li.get(z);
}
}
}
3、存在重复元素(leetcode217)
可以使用HashSet,从头到尾遍历数组,判断哈希表中是否有该元素,如果没有则加入,否则直接返回true。结果遍历完该数组后还是没有返回true就证明数组中出现的元素都是唯一的,返回false。
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
int len = nums.length;
for(int i = 0 ; i < len ; i ++)
{
if(set.contains(nums[i]))return true;
else set.add(nums[i]);
}
return false;
}
}
4、多数元素(leetcode169)
使用HashMap进行遍历存储
class Solution {
public int majorityElement(int[] nums) {
int len = nums.length;
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0 ; i < len ; i ++)
{
if(map.containsKey(nums[i]))
{
int num1 = map.get(nums[i]);
map.put(nums[i], ++num1);
if(map.get(nums[i]) > len / 2)return nums[i];
}
else map.put(nums[i], 1);
}
return nums[0];
}
}
5、合并排序的数组(leetcode题)
将B数组的数放进A中,然后利用快排将A排序就可以了
class Solution {
public void quick(int []num , int l, int r)
{
if(l >= r)return ;
int mid = num[ (l + r) / 2];
int i = l - 1 , j = r + 1;
while(i < j)
{
do i ++;while(num[i] < mid);
do j --;while(num[j] > mid);
if(i < j)
{
int n = num[i];
num[i] = num[j];
num[j] = n;
}
}
quick(num,l,j);
quick(num,j + 1, r);
}
public void merge(int[] A, int m, int[] B, int n) {
for(int i = m , k = 0 ; k < n ; i ++, k ++)
{
A[i] = B[k];
}
quick(A,0, n + m - 1);
}
}
6、排序链表
一个比较拉的做法,先将链表中的数取出来,然后进行排序,再放回去;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head == null)return null;
ListNode h1 = head;
List<Integer> li = new ArrayList<>();
while(h1 != null)
{
li.add(h1.val);
h1 = h1.next;
}
li.sort(new Comparator<Integer>() {
@Override
public int compare(Integer integer, Integer t1) {
return integer - t1;
}
});
h1 = head;
for(int i = 0 ; i < li.size() ; i ++)
{
h1.val = li.get(i);
h1 = h1.next;
}
return head;
}
}