diff --git a/src/leecode/122.买卖股票的最佳时机-ii.java b/src/leecode/122.买卖股票的最佳时机-ii.java index 0d72364..da01235 100644 --- a/src/leecode/122.买卖股票的最佳时机-ii.java +++ b/src/leecode/122.买卖股票的最佳时机-ii.java @@ -5,7 +5,7 @@ package leecode;/* */ // @lc code=start -class Solution { +class No122 { //贪心算法 public int maxProfit(int[] prices) { int len = prices.length; @@ -94,7 +94,7 @@ class Solution { public static void main(String[] args) { int[] price = new int[]{1, 2, 3}; - Solution solution = new Solution(); + No122 solution = new No122(); System.out.println(solution.maxProfit(price)); } diff --git a/src/leecode/19.删除链表的倒数第-n-个结点.java b/src/leecode/19.删除链表的倒数第-n-个结点.java new file mode 100644 index 0000000..b8b8b1b --- /dev/null +++ b/src/leecode/19.删除链表的倒数第-n-个结点.java @@ -0,0 +1,39 @@ +package leecode;/* + * @lc app=leetcode.cn id=19 lang=java + * + * [19] 删除链表的倒数第 N 个结点 + */ + +// @lc code=start + +// Definition for singly-linked list. + + +import java.util.ArrayList; +import java.util.List; + +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + List cache =new ArrayList<>(); + ListNode ret = head; + while (head != null) { + cache.add(head); + head = head.next; + } + if (n == cache.size()) { + return ret.next; + } + cache.get(cache.size() - n - 1).next = n == 1 ? null : cache.get(cache.size() - n + 1); + return ret; + } + +// private static class ListNode { +// int val; +// ListNode next; +// ListNode() {} +// ListNode(int val) { this.val = val; } +// ListNode(int val, ListNode next) { this.val = val; this.next = next; } +// } +} +// @lc code=end + diff --git a/src/leecode/python/14.最长公共前缀.py b/src/leecode/python/14.最长公共前缀.py index 92c70dd..6db47d8 100644 --- a/src/leecode/python/14.最长公共前缀.py +++ b/src/leecode/python/14.最长公共前缀.py @@ -9,7 +9,7 @@ from typing import List -class Solution: +class leecode.Solution: def longestCommonPrefix(self, strs: List[str]) -> str: # 遍历 # if not strs: diff --git a/src/leecode/python/21.合并两个有序链表.py b/src/leecode/python/21.合并两个有序链表.py index 69c4396..7759f63 100644 --- a/src/leecode/python/21.合并两个有序链表.py +++ b/src/leecode/python/21.合并两个有序链表.py @@ -15,7 +15,7 @@ class ListNode: self.next = next -class Solution: +class leecode.Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if not list1: return list2 # 终止条件,直到两个链表都空 @@ -29,5 +29,5 @@ class Solution: return list2 -sol = Solution() +sol = leecode.Solution() # @lc code=end diff --git a/src/leecode/python/9.回文数.py b/src/leecode/python/9.回文数.py index 6e500ae..049188f 100644 --- a/src/leecode/python/9.回文数.py +++ b/src/leecode/python/9.回文数.py @@ -13,7 +13,7 @@ # 奇数位 121 # 12 1 # 1 12 -class Solution: +class leecode.Solution: def isPalindrome(self, x: int) -> bool: if x < 0 or (x % 10 == 0 and x != 0): return False