// Date : 2024/01/15 ~ 2024/01/21
013 | #LeetCode.27. | #北岸计划 | 2024/01/15 |
---|
// 略
// 重做
014 | #LeetCode.160. | #北岸计划 | 2024/01/16 |
---|
Version1.0
class Solution {
public int removeElement(int[] nums, int val) {
int length = nums.length;
for(int i=0 ;i<length ; i++){
while(nums[length-1] == val){
length--;
if(length == i){
return length;
}
}
if(nums[i] == val){
nums[i] = nums[length-1];
length--;
}
}
return length;
}
}
015 | #LeetCode.977. | #代码随想录004 | 2024/01/17 |
---|
给你一个按 非递减顺序 排序的整数数组 nums
,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
// 重做
016 | #LeetCode.59. | #北岸计划 | 2024/01/17 |
---|
给你一个正整数 n
,生成一个包含 1
到 n2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int sup = 0;
int sub = n-1;
int left = 0;
int right = n-1;
int count = 1;
while(sup <= sub && left <= right){
for(int i=left ; i<=right ; i++){
matrix[sup][i] = count;
count++;
}
sup++;
for(int i=sup ; i<=sub ; i++){
matrix[i][right] = count;
count++;
}
right--;
for(int i=right ; i>=left ; i--){
matrix[sub][i] = count;
count++;
}
sub--;
for(int i=sub ; i>=sup ; i--){
matrix[i][left] = count;
count++;
}
left++;
}
return matrix;
}
}
017 | #LeetCode.203. | #北岸计划 | 2024/01/17 |
---|
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode prev = dummy;
ListNode curr = head;
while(curr != null){
if(curr.val == val){
prev.next = curr.next;
}else{
prev = prev.next;
}
curr = curr.next;
}
return dummy.next;
}
}
018 | #LeetCode.209. | #北岸计划 | 2024/01/18 |
---|
给定一个含有 n
个正整数的数组和一个正整数 target
。
找出该数组中满足其总和大于等于 target
的长度最小的 连续子数组 [nums_l, nums_l+1, ..., nums_r-1, nums_r]
,并返回其长度**。**
如果不存在符合条件的子数组,返回 0
。
// 重做
019 | #LeetCode.707. | #北岸计划 | 2024/01/19 |
---|
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:val
和 next
。val
是当前节点的值,next
是指向下一个节点的指针/引用。
如果是双向链表,则还需要属性 prev
以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。
实现 MyLinkedList
类:
MyLinkedList()
初始化 MyLinkedList
对象。int get(int index)
获取链表中下标为 index
的节点的值。如果下标无效,则返回 -1
。void addAtHead(int val)
将一个值为 val
的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。void addAtTail(int val)
将一个值为 val
的节点追加到链表中作为链表的最后一个元素。void addAtIndex(int index, int val)
将一个值为 val
的节点插入到链表中下标为 index
的节点之前。如果 index
等于链表的长度,那么该节点会被追加到链表的末尾。如果 index
比长度更大,该节点将 不会插入 到链表中。void deleteAtIndex(int index)
如果下标有效,则删除链表中下标为 index
的节点。public class MySingleLinkedList {
ListNode head;
int length;
public MySingleLinkedList () {
head = new ListNode(-Integer.MAX_VALUE);
length = 0;
}
public int get(int index) {
if(length == 0 || index > length-1 || index < 0){
//索引有效性检查
return -1;
}else{
ListNode cur = head;
for(int i=0 ; i<index ; i++){
cur = cur.next;
}
return cur.val;
}
}
public void addAtHead(int val) {
if(length == 0){
head.val = val;
}else{
int hVal = head.val;
ListNode node = new ListNode(hVal);
head.val = val;
node.next = head.next;
head.next = node;
}
length++;
}
public void addAtTail(int val) {
if(length == 0){
head.val = val;
}else{
ListNode node = new ListNode(val);
ListNode cur = head;
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
length++;
}
public void addAtIndex(int index, int val) {
if(length == 0 && index == length){
head.val = val;
length++;
}else if(index <= length){
if(index == 0){
addAtHead(val);
length--;
}else if(index == length){
addAtTail(val);
length--;
}else if(index < length){
ListNode node = new ListNode(val);
ListNode cur = head;
for(int i=0 ; i<index-1 ; i++){
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
length++;
}else if(index > length){
}
}
public void deleteAtIndex(int index) {
ListNode cur = head;
if(index == 0 || index == length-1){
if(index == 0) head = head.next;
if(index == length-1){
for(int i=0 ; i<index-1 ; i++){
cur = cur.next;
}
cur.next = null;
}
}else if(index > 0 && index < length){
for(int i=0 ; i<index-1 ; i++){
cur = cur.next;
}
cur.next = cur.next.next;
}else{
// index < 0 || index > length-1
length++;
}
length--;
}
public void print(){
ListNode cur = head;
while(cur != null){
if(cur != head){
System.out.print("->");
}
System.out.print("【"+ cur.val +"】");
cur = cur.next;
}
System.out.println();
}
public void manualCreatList(){
// 手动建表
ListNode node1 = new ListNode(2);
ListNode node2 = new ListNode(4);
ListNode node3 = new ListNode(6);
head.val = 0;
head.next = node1;
node1.next = node2;
node2.next = node3;
length = 4;
}
}
020 | #LeetCode.35. | #北岸计划 | 2024/01/20 |
---|
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n)
的算法。
class Solution {
public int searchInsert(int[] nums, int target) {
int length = nums.length;
int left = 0;
int right = length-1;
int result;
while(right - left > 1){
int mid = (left + right)/2;
if(nums[mid] > target){
right = mid;
}else if(nums[mid] < target){
left = mid;
}else{
return mid;
}
}
boolean found = nums[left] == target || nums[right] == target;
boolean internal = nums[left] < target && nums[right] > target;
if(found){
result = nums[left] == target ? left : right;
}else{
if(internal){
result = right;
}else{
result = nums[right] < target ? right+1 : left;
}
}
return result;
}
}
021 | #LeetCode.707. | #北岸计划 | 2024/01/21 |
---|
// 略
// 优化整理