07 2019 档案
摘要:给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.与 target 最接近的三个数
阅读全文
摘要:15 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。 class Solution { public List<List<Integer>> thr
阅读全文
摘要:解法1,利用二分查找,对第一个i,去剩下的有序数组中查找target-numbers[i] class Solution { public int[] twoSum(int[] numbers, int target) { int[] res=new int[2]; for(int i=0;i<nu
阅读全文
摘要:package BST;import java.util.LinkedList;import java.util.Queue;public class BST<Key extends Comparable<Key>,Value>{ private class Node{ //节点的构造是key,va
阅读全文
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
阅读全文
摘要:package heap;public class IndexMaxHeap { private int[] data; private int count; private int capacity; private int[] indexes; public IndexMaxHeap(int c
阅读全文
摘要:class Solution { public int search(int[] nums, int target) { int l=0; int r=nums.length-1; while(l<=r) { int mid=l+(r-l)/2; if(nums[mid]==target) retu
阅读全文
摘要://先排序,将左区间小的放在前面,然后如果前一个的右区间大于下一个的左区间,则可以合并,分别用两个下标指向当前的大区间和将要考察的小区间 class Solution { public int[][] merge(int[][] intervals) { if(intervals.length<=1
阅读全文
摘要:class Solution { public int lastStoneWeight(int[] stones) { MaxHeap s=new MaxHeap(stones.length+1); for(int i=0;i<stones.length;i++) { s.insert(stones
阅读全文
摘要:public class MaxHeap { private int[] data; private int count; private int capacity; public MaxHeap(int capacity){ this.capacity=capacity; data=new int
阅读全文
摘要://利用归并排序来完成,归并排序可参考前面代码,归并排序可用来完成这类逆序对之类的问题,采用分治的思想,对于归并排序的代码不需要多改动,只需要在归并之前进行一次寻找操作,找出count的数量 class Solution { private int count=0; public int rever
阅读全文
摘要://这是三路快速排序的实现,最重要的是要注意边界值的问题,尤其是lt,gt和i的取值,千万不要弄错了 //下面实现的lt代表的是包括最右边的那个,整个区间是【l,lt】是个闭区间,右边也是个闭区间,所以写代码时要注意,在算法这本书中的实现与我的区间有些不同,请注意。 class Solution {
阅读全文
摘要:class Solution { //quickSort函数 public static void quickSort(int[] arr,int l,int r){ if(l>=r) { return ; } int p=partition(arr, l, r); //找到分界点的下标位置 qui
阅读全文
摘要:class Solution { //quickSort函数 public static void quickSort(int[] arr,int l,int r){ if(l>=r) { return ; } int p=partition(arr, l, r); //找到分界点的下标位置 qui
阅读全文
摘要:class Solution { public int[] sortArray(int[] nums) { int n=nums.length; mergeSort(nums,0,n-1); return nums; } //进行归并操作,对于两个数组分别为arr[l,mid]和arr[mid+1,
阅读全文
摘要:希尔排序是在插入排序的基础上进行的一中改进的算法,希尔排序是将一个原序列分成几个子序列,对于每个子序列来所都进行一次插入排序,而依据不同的子序列划分大小,最后子序列为1时,进行的那一次插入排序跟原来的插入排序就是一模一样的了,只不过现在的队列比原来的要有序的多。 所以希尔排序就是将原序列进行了一些整
阅读全文