leecode更新
This commit is contained in:
parent
bc796b7885
commit
107c69cdf7
|
|
@ -8,6 +8,34 @@
|
||||||
<artifactId>Leecode</artifactId>
|
<artifactId>Leecode</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.projectlombok</groupId>-->
|
||||||
|
<!-- <artifactId>lombok</artifactId>-->
|
||||||
|
<!-- <version>RELEASE</version>-->
|
||||||
|
<!--<!– <scope>compile</scope>–>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.12</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,98 @@
|
||||||
|
package com.markilue.leecode.array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 力扣题号704:二分查找
|
||||||
|
* 描述:给定一个n个元素有序的(升序)整型数组nums 和一个目标值target ,写一个函数搜索nums中的 target,如果目标值存在返回下标,否则返回 -1。
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BinarySearch {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] nums = {-1,0,3,5,9,12};
|
||||||
|
int target = 9;
|
||||||
|
|
||||||
|
System.out.println(search(nums,target));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 非递归法
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int search(int[] nums, int target) {
|
||||||
|
|
||||||
|
int left=0;
|
||||||
|
int right = nums.length;
|
||||||
|
|
||||||
|
|
||||||
|
while (left<right){
|
||||||
|
//位运算加快速度,同时不用(right+left)>>1避免right+left超过了int最大值
|
||||||
|
//由于舍入问题,mid永远小于right,因此 可以写right = nums.length
|
||||||
|
int mid=left+((right-left)>>1);
|
||||||
|
if(nums[mid]<target){
|
||||||
|
left=mid+1;
|
||||||
|
}else if(nums[mid]>target){
|
||||||
|
right=mid;
|
||||||
|
}else {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归法
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int search1(int[] nums, int target) {
|
||||||
|
|
||||||
|
//记录数组长度
|
||||||
|
return find(nums,0,nums.length-1,target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int find(int[] nums,int start,int stop, int target){
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解决N.5只能舍不能入,最后可能无限循环的问题
|
||||||
|
*/
|
||||||
|
if(nums[start]==target){
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
if(nums[stop]==target){
|
||||||
|
return stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(start==stop-1&&nums[stop]!=target){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(start==stop){
|
||||||
|
if(nums[start]==target){
|
||||||
|
return start;
|
||||||
|
}else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int mid = start+(stop-start)/2;
|
||||||
|
if(nums[mid]<target){
|
||||||
|
return find(nums,mid,stop,target);
|
||||||
|
}else if(nums[mid]>target){
|
||||||
|
return find(nums,start,mid,target);
|
||||||
|
}else {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.markilue.leecode.array;
|
||||||
|
|
||||||
|
public class RemoveElement {
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode;
|
package com.markilue.leecode.listnode;
|
||||||
/**
|
/**
|
||||||
* Definition for singly-linked list.
|
* Definition for singly-linked list.
|
||||||
* public class ListNode {
|
* public class ListNode {
|
||||||
|
|
@ -10,8 +10,6 @@ package listnode;
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 2.两数相加
|
* 2.两数相加
|
||||||
*/
|
*/
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode;
|
package com.markilue.leecode.listnode;
|
||||||
|
|
||||||
public class mergeKLists {
|
public class mergeKLists {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode;
|
package com.markilue.leecode.listnode;
|
||||||
|
|
||||||
public class mergeTwoLists {
|
public class mergeTwoLists {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode;
|
package com.markilue.leecode.listnode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除链表的倒数第N个节点
|
* 删除链表的倒数第N个节点
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
package listnode.selftry;
|
package com.markilue.leecode.listnode.selftry;
|
||||||
|
|
||||||
import listnode.mergeTwoLists;
|
|
||||||
|
|
||||||
public class mergeKLists {
|
public class mergeKLists {
|
||||||
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
package listnode.selftry;
|
package com.markilue.leecode.listnode.selftry;
|
||||||
|
|
||||||
import listnode.addTwoNumbers;
|
|
||||||
import listnode.removeNthFromEnd;
|
|
||||||
|
|
||||||
public class mergeTwoLists {
|
public class mergeTwoLists {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode.selftry;
|
package com.markilue.leecode.listnode.selftry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除链表的倒数第N个节点
|
* 删除链表的倒数第N个节点
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package listnode.selftry;
|
package com.markilue.leecode.listnode.selftry;
|
||||||
|
|
||||||
import listnode.swapPairs;
|
import com.markilue.leecode.listnode.swapPairs;
|
||||||
|
|
||||||
public class reverseKGroup {
|
public class reverseKGroup {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode.selftry;
|
package com.markilue.leecode.listnode.selftry;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package listnode;
|
package com.markilue.leecode.listnode;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
package sort;
|
package com.markilue.leecode.sort;
|
||||||
|
|
||||||
import com.sun.corba.se.impl.ior.FreezableList;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
@ -34,7 +32,7 @@ public class ThreeSum {
|
||||||
|
|
||||||
public static List<List<Integer>> threeSum(int[] nums) {
|
public static List<List<Integer>> threeSum(int[] nums) {
|
||||||
|
|
||||||
List<List<Integer>> parentList = new ArrayList<>();
|
List<List<Integer>> parentList = new ArrayList<List<Integer>>();
|
||||||
|
|
||||||
if (nums.length < 3) {
|
if (nums.length < 3) {
|
||||||
return parentList;
|
return parentList;
|
||||||
|
|
@ -78,7 +76,7 @@ public class ThreeSum {
|
||||||
}
|
}
|
||||||
//如果是第二个条件跳出的循环
|
//如果是第二个条件跳出的循环
|
||||||
if (nums[j] + nums[k] == need) {
|
if (nums[j] + nums[k] == need) {
|
||||||
List<Integer> list = new ArrayList<>();
|
List<Integer> list = new ArrayList<Integer>();
|
||||||
list.add(nums[i]);
|
list.add(nums[i]);
|
||||||
list.add(nums[j]);
|
list.add(nums[j]);
|
||||||
list.add(nums[k]);
|
list.add(nums[k]);
|
||||||
|
|
@ -99,7 +97,7 @@ public class ThreeSum {
|
||||||
//更简洁一点的思路
|
//更简洁一点的思路
|
||||||
public static List<List<Integer>> threeSum1(int[] nums) {
|
public static List<List<Integer>> threeSum1(int[] nums) {
|
||||||
|
|
||||||
List<List<Integer>> parentList = new ArrayList<>();
|
List<List<Integer>> parentList = new ArrayList<List<Integer>>();
|
||||||
|
|
||||||
if (nums.length < 3) {
|
if (nums.length < 3) {
|
||||||
return parentList;
|
return parentList;
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.markilue.leecode.test;
|
||||||
|
|
||||||
|
public class Fibonaqi {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试使用时间复杂度为n的斐波那契数列递归法
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
// @Test
|
||||||
|
// public static void testFibonaqi(){
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n=5;
|
||||||
|
System.out.println(fibonacci(1,1,10));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int fibonacci(int first,int second,int n){
|
||||||
|
if(n<=0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(n <3){
|
||||||
|
return 1;
|
||||||
|
}else if(n==3){
|
||||||
|
return first+second;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return fibonacci(second,first+second,n-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
package tree;
|
package com.markilue.leecode.tree;
|
||||||
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static tree.InorderTraversal.inorderTraversal1;
|
import static com.markilue.leecode.tree.InorderTraversal.inorderTraversal1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除二叉排序树的某个节点
|
* 删除二叉排序树的某个节点
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package tree;
|
package com.markilue.leecode.tree;
|
||||||
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package tree;
|
package com.markilue.leecode.tree;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
Loading…
Reference in New Issue