leecode更新
This commit is contained in:
parent
1412b12408
commit
9c1737f0bf
|
|
@ -5,7 +5,7 @@ package com.markilue.leecode.array;
|
|||
* 描述:给定一个n个元素有序的(升序)整型数组nums 和一个目标值target ,写一个函数搜索nums中的 target,如果目标值存在返回下标,否则返回 -1。
|
||||
*
|
||||
*/
|
||||
public class BinarySearch {
|
||||
public class T01_BinarySearch {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = {-1,0,3,5,9,12};
|
||||
|
|
@ -11,7 +11,7 @@ import java.util.Arrays;
|
|||
* 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
|
||||
* 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
|
||||
*/
|
||||
public class RemoveElement {
|
||||
public class T02_RemoveElement {
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -7,7 +7,7 @@ import javax.management.remote.rmi._RMIConnection_Stub;
|
|||
/**
|
||||
* 寻找长度最小的子数组
|
||||
*/
|
||||
public class MinSubArrayLen {
|
||||
public class T03_MinSubArrayLen {
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -8,7 +8,7 @@ import java.util.Arrays;
|
|||
* 螺旋矩阵:
|
||||
* 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix
|
||||
*/
|
||||
public class generateMatrix {
|
||||
public class T04_generateMatrix {
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
package com.markilue.leecode.array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.array
|
||||
*@Author: dingjiawen
|
||||
*@CreateTime: 2022-12-22 11:02
|
||||
*@Description:
|
||||
* TODO 力扣977题 有序数组的平方:
|
||||
* 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T05_SortedSquares {
|
||||
@Test
|
||||
public void test(){
|
||||
int[] nums = {-4, -1, 0, 3, 10};
|
||||
System.out.println(Arrays.toString(sortedSquares(nums)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1(){
|
||||
int[] nums = {-7,-3,2,3,11};
|
||||
System.out.println(Arrays.toString(sortedSquares1(nums)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2(){
|
||||
int[] nums = {-10000,-3,2,3,10000};
|
||||
System.out.println(Arrays.toString(sortedSquares(nums)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路:先找到最接近0的数,然后左右比较进行遍历
|
||||
* 时间复杂度应该在O(N),空间复杂度也是O(N)
|
||||
* 速度击败100%,内存击败43.93%
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public int[] sortedSquares(int[] nums) {
|
||||
//寻找第一个大于0的数
|
||||
int targetIndex = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] <= 0) targetIndex++;
|
||||
}
|
||||
//在第一个大于0的数旁边左右遍历
|
||||
int left = targetIndex - 1;
|
||||
int index = 0;
|
||||
int[] result = new int[nums.length];
|
||||
while (left >= 0 && targetIndex <= nums.length - 1) {
|
||||
if(-nums[left]>nums[targetIndex]){
|
||||
result[index++]=nums[targetIndex]*nums[targetIndex];
|
||||
targetIndex++;
|
||||
}else {
|
||||
result[index++]=nums[left]*nums[left];
|
||||
left--;
|
||||
}
|
||||
}
|
||||
//后面的用完了
|
||||
while (left>=0){
|
||||
result[index++]=nums[left]*nums[left];
|
||||
left--;
|
||||
}
|
||||
//前面的用完了
|
||||
while (targetIndex<= nums.length - 1){
|
||||
result[index++]=nums[targetIndex]*nums[targetIndex];
|
||||
targetIndex++;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 代码随想录思路:虽然最小的数不知道,但最大的数一定是在数组的两边产生;避免找0的过程
|
||||
* 时间复杂度在O(N),空间复杂度也是O(N)
|
||||
* 速度击败100%,内存击败63.68%
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public int[] sortedSquares1(int[] nums) {
|
||||
|
||||
//从两边开始双向奔赴遍历,两边的最大值一定是最大值
|
||||
int right=nums.length-1;
|
||||
int left = 0;
|
||||
int index = nums.length-1;
|
||||
int[] result = new int[nums.length];
|
||||
while (left <=right) {
|
||||
if(-nums[left]>nums[right]){
|
||||
result[index--]=nums[left]*nums[left];
|
||||
left++;
|
||||
}else {
|
||||
result[index--]=nums[right]*nums[right];
|
||||
right--;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.markilue.leecode.array.second;
|
||||
|
||||
/**
|
||||
* 二刷
|
||||
* 力扣题号704:二分查找
|
||||
* 描述:给定一个n个元素有序的(升序)整型数组nums 和一个目标值target ,写一个函数搜索nums中的 target,如果目标值存在返回下标,否则返回 -1。
|
||||
*
|
||||
*/
|
||||
public class T01_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-1;
|
||||
|
||||
while (left<=right){
|
||||
int mid=left+((right-left)>>1);
|
||||
|
||||
if(nums[mid]<target){
|
||||
left=mid+1;
|
||||
}else if(nums[mid]>target){
|
||||
right=mid-1;
|
||||
}else {
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
//出来了还没找到
|
||||
return -1;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归法
|
||||
* @param nums
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
public static int search1(int[] nums, int target) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int find(int[] nums,int start,int stop, int target){
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package com.markilue.leecode.listnode;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.listnode
|
||||
*@Author: dingjiawen
|
||||
*@CreateTime: 2022-12-22 11:55
|
||||
*@Description: TODO 链表的使用工具
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class ListNodeUtils {
|
||||
|
||||
@Test
|
||||
public void testPrint(){
|
||||
int[] nodeList= {2, 4, 3};
|
||||
ListNode root = build(nodeList);
|
||||
print(root);
|
||||
// print();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过一个val数组建立链表
|
||||
* @param nodeList
|
||||
* @return
|
||||
*/
|
||||
public static ListNode build(int[] nodeList){
|
||||
ListNode root =new ListNode(nodeList[0]);
|
||||
ListNode temp=root;
|
||||
for (int i = 1; i < nodeList.length; i++) {
|
||||
temp.next= new ListNode(nodeList[i]);
|
||||
temp=temp.next;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印链表
|
||||
* @param root
|
||||
*/
|
||||
public static void print(ListNode root){
|
||||
StringBuilder builder=new StringBuilder("[");
|
||||
ListNode temp=root;
|
||||
while (temp!=null){
|
||||
builder.append(temp.val);
|
||||
builder.append(",");
|
||||
temp=temp.next;
|
||||
}
|
||||
builder.append("]");
|
||||
|
||||
System.out.println(builder.toString());
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue