leetcode-410. 分割数组的最大值
leetcode-410. 分割数组的最大值
题解思路
123456789101112131415161718192021222324252627282930class Solution { public int splitArray(int[] nums, int m) { int n = nums.length; // 将前i个数分为m断 int[][] dp = new int[n + 1][m + 1]; int[] s = new int[n + 1]; for (int i = 0; i <= n; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE); } dp[0][0] = 0; //前缀和 for (int i = 1; i <= n; i++) { s[i] = s[i - 1] + nums ...
leetcode-925. 长按键入
leetcode-925. 长按键入
1234567891011121314151617181920212223242526272829303132333435363738394041424344class Solution { public boolean isLongPressedName(String name, String typed) { int j = 0; if (typed.length() < name.length()) { return false; } Character pre = null; for (int i = 0; i < name.length(); i++) { if (j >= typed.length()) { return false; } if (name. ...
acwing-282. 石子合并
acwing-282. 石子合并
123456789101112131415161718192021222324252627282930313233343536373839import java.util.*;class Main{ static int[] arr; static int[] s; static int n ; public static void main(String[] args){ Scanner sc = new Scanner(System.in); n = sc.nextInt(); arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } s = new int[n+1]; System.out.println(getCost()); } public ...
acwing-835. Trie字符串统计
AcWing 835. Trie字符串统计
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import java.util.Scanner;public class Main { private static int N=100010; private static int son[][]=new int[N][26];//Trie树中每个节点的所有儿子 private static int cnt[]=new int[N];//以当前这个点结尾的单词有多少个 private static char str[]=new char[N]; private static int idx;//当前用的的哪个下标,下标0:既是根节点又是空节点 public static void main(String[] args){ Scanner scanner=new Scanner(System.in ...
leetcode-692. 前K个高频单词
leetcode-692. 前K个高频单词
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758class Solution { int n = 100010; int[][] st = new int[n][26]; int[] count = new int[n]; int idx = 0; Map<String, Integer> map = new HashMap<>(); public List<String> topKFrequent(String[] words, int k) { for (String str : words) { insert(str); } PriorityQueue<String> queue = n ...
leetcode-143. 重排链表
leetcode143. 重排链表
123456789101112131415161718192021222324class Solution { public void reorderList(ListNode head) { LinkedList<ListNode> queue = new LinkedList<ListNode>(); ListNode tmp = head; while (tmp != null){ queue.offer(tmp); tmp = tmp.next; } ListNode cur = null; while (!queue.isEmpty()){ ListNode frist = queue.pollFirst(); ListNode last = queue.pollLast(); ...
leetcode-剑指 Offer 24. 反转链表
leetcode-剑指 Offer 24. 反转链表
双指针
1234567891011121314class Solution { public ListNode reverseList(ListNode head) { if(head.next == null){ return head; } ListNode cur = head; ListNode pre = null; while(cur != null){ cur.next = pre; pre = cur; } }}
递归
1234567891011class Solution { public ListNode reverseList(ListNode head) { if(head == null || head.next == null ...
leetcode-61. 旋转链表
leetcode-61. 旋转链表
1234567891011121314151617181920212223242526272829303132333435class Solution { public ListNode rotateRight(ListNode head, int k) { ListNode tmp = head; int count = 0; ListNode end = null; while(tmp != null){ if(tmp.next == null){ end = tmp; } tmp = tmp.next; count++; } if(count == 0){ return head; } int i = k % ...
leetcode-1624. 两个相同字符之间的最长子字符串
leetcode-1624. 两个相同字符之间的最长子字符串
1234567891011121314class Solution { public int maxLengthBetweenEqualCharacters(String s) { int n = s.length(); int max = -1; for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { if (s.charAt(i) == s.charAt(j)){ max = Math.max(max, j - i - 1); } } } return max; }}
leetcode-844. 比较含退格的字符串
leetcode-844. 比较含退格的字符串
123456789101112131415161718192021222324252627282930class Solution { public boolean backspaceCompare(String S, String T) { Stack<Character> stack1 = getStack(S); Stack<Character> stack2 = getStack(T); if (stack1.size() != stack2.size()) { return false; } while (!stack1.isEmpty()) { if (stack1.pop() != stack2.pop()) { return false; } & ...