1.合并n个有序集合
思路:两两合并
public static List<Integer> merge(List<List<Integer>> list){
List<Integer> result = list.get(0);
for (int i = 1; i <list.size() ; i++) {
result=MergeTwoList(result,list.get(i));
}
return result;
}
private static List<Integer> MergeTwoList(List<Integer> list1, List<Integer> list2) {
//合并两个有序列表,双指针
int p=0,q=0;
List<Integer> result = new ArrayList<>();
while (p< list1.size()&& q<list2.size()){
if(list1.get(p)<list2.get(q)){
result.add((list1.get(p)));
p++;
}else{
result.add((list2.get(q)));
q++;
}
}
while(q<list2.size()){
result.add((list2.get(q++)));
}
while( p<list1.size()){
result.add((list1.get(p++)));
}
return result;
}
2.有序的数组 找出两个数的和等于给定的数字。要求时间O(n) 空间O(1)。
//双指针
public int[] twoSum(int[] nums, int target) {
int p=0,q= nums.length-1;
while (p<q){
if(nums[p]+nums[q]==target){
return new int[]{nums[p],nums[q]};
}else if(nums[p]+nums[q]<target){
p++;
}else{
q--;
}
}
return new int[2];
}
//使用哈希表,空间为O(n)
public int[] twoSum2(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if(map.containsKey(target-nums[i])){
return new int[]{map.get(target-nums[i]),i};
}
map.put(nums[i],i);
}
return new int[2];
}
3.n
阶楼梯。每次你可以爬 1
或 2
个台阶。你有多少种不同的方法可以爬到楼顶呢?
//迭代代码,递归代码如裴波那切一样,容易超时(30)
public int climbStairs(int n) {
if(n==1){
return 1;
}
if(n==2){
return 2;
}
int f1=1,f2=2,res=0;
for(int i=3;i<=n;i++){
res=f1+f2;
f1=f2;
f2=res;
}
return res;
}
//递归
public int climbStairs(int n) {
if(n==1){
return 1;
}
if(n==2){
return 2;
}
return climbStairs(n-1)+climbStairs(n-2);
}
4.有x*y的方格,从左上角走到右下角,每次只能向右或者向下走一格,不能后退,问一共多少种走法?
// 一维数组
public int uniquePaths(int m, int n) {
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[j] += dp[j - 1];
}
}
return dp[n - 1];
}
//二维数组
public int uniquePaths(int m, int n) {
int[][] dp=new int[m][n];
Arrays.fill(dp[0],1);
for(int k=1;k<m;k++){
dp[k][0]=1;
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
//递归代码略
5.给出一个数字n,如果n是偶数就除以2,如果是奇数就选择+1或者-1,求最少几次能够使n变成1
/**
* 偶数如果是4的倍数最好(凑+1为4的倍数),能被4整除那么肯定被2整除还是偶数
*如果要计算除以2的次数则把count++放在偶数的if语句里边,如果奇数+1次数也算,那么放在最后
* @param n
*/
private static int f(int n) {
int count=0;
while (n!=1){
if(n%2==0){
n/=2;
count++;
}else{
if((n+1)%4==0){
n++;
}else {
n--;
}
}
}
return count;
}
6.加一:给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 输入:digits = [4,3,2,1] 输出:[4,3,2,2]。
public static int[] plusOne(int[] nums) {
int carry=0;
for(int i=nums.length-1;i>=0;i--){
int sum=i==nums.length-1?(nums[i]+1+carry):(nums[i]+carry);
carry=sum-10>=0?1:0;
sum=sum-10>=0?sum-10:sum;
nums[i]=sum;
}
int[] result=new int[nums.length+1];
if(carry>0){
for(int i=0;i<nums.length;i++){
result[i+1]=nums[i];
result[0]=1;
return result;
}
}
return nums;
}
7.寻找重复数:给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数(可能出现多次),找出这个重复的数,求使用O(1)空间复杂度。
/**
*
* @param nums int整型一维数组
* @return int整型
*/
//二分法的灵活运用
public int findDuplicate(int[] nums) {
int n=nums.length-1;
//对1到n区间进行二分,二分法最重要的思路就是对什么进行二分,不一定是原数组
//数组:双指针,哈希表,二分法
int left=1;
int right=n;
while(left<right){
int mid=(left+right)/2;
int count=0; //计算大于中间值的数量
for(int i=0;i<nums.length;i++){
if(nums[i]<=mid){
count++;
}
}
if(count>mid){ //证明再区间[mid,n]有重复
right=mid;
}else{
//当 count <= mid,重复的元素当然有可能出现在 [left..mid]
//但是,在 [mid + 1..right] 里一定会出现重复元素,1-10中6,7,8,9值总个数为5个
left=mid+1;
}
}
return left;
}
8.链表排序。在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
//* 链表排序(自顶而下),在 O(n log n) 时间复杂度和LogN空间复杂度下,对链表进行排序。
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode head1 = head;
ListNode head2 = split(head);
head1 = sortList(head1);
head2 = sortList(head2);
return merge(head1, head2);
}
private ListNode split(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode next = slow.next;
slow.next = null;
return next;
}
private ListNode merge(ListNode l1,ListNode l2){
ListNode dummy=new ListNode(0);
ListNode cur=dummy;
while(l1!=null && l2!=null){
if(l1.val<l2.val){cur.next=l1;
l1=l1.next;
cur=cur.next;
}else {
cur.next=l2;l2=l2.next;
cur=cur.next;
} }
cur.next=l1==null?l2:l1;
return dummy.next;
}
//自下而上
/**
* nlogN和常数空间复杂度的方法
* @param head
* @return
*/
public ListNode sortList(ListNode head) {
if (head == null) {
return head;
}
int length = 0;
ListNode node = head;
while (node != null) {
length++;
node = node.next;
}
ListNode dummyHead = new ListNode(0, head);
for (int subLength = 1; subLength < length; subLength <<= 1) {
ListNode prev = dummyHead, curr = dummyHead.next;
while (curr != null) {
ListNode head1 = curr;
for (int i = 1; i < subLength && curr.next != null; i++) {
curr = curr.next;
}
ListNode head2 = curr.next;
curr.next = null;
curr = head2;
for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {
curr = curr.next;
}
ListNode next = null;
if (curr != null) {
next = curr.next;
curr.next = null;
}
ListNode merged = merge(head1, head2);
prev.next = merged;
while (prev.next != null) {
prev = prev.next;
}
curr = next;
}
}
return dummyHead.next;
}
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}