leetcode-19. 删除链表的倒数第N个节点
leetcode-19. 删除链表的倒数第N个节点
12345678910111213141516171819202122232425262728class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { int count = 0; ListNode cur = head; while(cur.next != null){ cur = cur.next; count ++; } cur = head; ListNode pre = null; int k = count - n + 1; while(k > 0){ pre = cur; cur = cur.next; k--; } ...
leetcode-1529. 灯泡开关 IV
leetcode-1529. 灯泡开关 IV
原始思路
123456789101112131415161718192021222324class Solution { public int minFlips(String target) { int count = 0; int n = target.length(); char[] chars = new char[n]; Arrays.fill(chars,'0'); for (int i = 0; i < n; i++) { char ch = chars[i]; if ((count & 1) == 1) { if (ch == '0'){ ch = '1'; }else ...
leetcode-977. 有序数组的平方
leetcode-977. 有序数组的平方
12345678910class Solution { public int[] sortedSquares(int[] A) { int n = A.length; for(int i = 0; i < n; i++){ A[i] = A[i]*A[i]; } Arrays.sort(A); return A; }}
leetcode-86. 分隔链表
leetcode-86. 分隔链表
123456789101112131415161718192021222324252627282930313233343536class Solution { public ListNode partition(ListNode head, int x) { ListNode tmp = head; ListNode rootMin = null; ListNode rootMax = null; ListNode min = null; ListNode max = null; while (tmp != null) { if (tmp.val < x){ if (min == null) { min = new ListNode(tmp.val); rootMin = min ...
leetcode-剑指 Offer 26. 树的子结构
leetcode-剑指 Offer 26. 树的子结构
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { if (A != null && B == null){ return false; } return dfs(A, B); } public boolean dfs(TreeNode root, TreeNode B){ if (root == null){ return false; } if (root.val == B.val){ ...
leetcode-1282. 用户分组
leetcode-1282. 用户分组
123456789101112131415161718192021222324252627282930313233class Solution { public List<List<Integer>> groupThePeople(int[] groupSizes) { Map<Integer, List<Integer>> map = new HashMap<>(); for(int i = 0; i < groupSizes.length; i++){ if(map.containsKey(groupSizes[i])){ map.get(groupSizes[i]).add(i); }else{ List<Integer> tmp = new ArrayList< ...
leetcode-116. 填充每个节点的下一个右侧节点指针
leetcode-116. 填充每个节点的下一个右侧节点指针
12345678910111213141516171819202122232425262728293031class Solution { LinkedList<Node> queue = new LinkedList<>(); public Node connect(Node root) { if(root == null){ return root; } queue.offer(root); while(!queue.isEmpty()){ int n = queue.size(); Node pre = null; for(int i = 0; i < n; i++){ Node node = queue.poll(); ...
leetcode-1486. 数组异或操作
leetcode-1486. 数组异或操作
12345678910111213141516class Solution { public int xorOperation(int n, int start) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { int k = start + 2 * i; nums[i] = k; } int ans = nums[0]; for (int i = 1; i < n; i++) { ans = (ans ^ nums[i]); } return ans; }}
leetcode-1431. 拥有最多糖果的孩子
leetcode-1431. 拥有最多糖果的孩子
12345678910111213141516171819class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max = Integer.MIN_VALUE; int n = candies.length; for (int i = 0; i < n; i++) { max = Math.max(max, candies[i]); } List<Boolean> list = new ArrayList<>(); for (int i = 0; i < n; i++) { if (candies[i] + extraCandies >= max) { ...
leetcode-1470. 重新排列数组
leetcode-1470. 重新排列数组
1234567891011class Solution { public int[] shuffle(int[] nums, int n) { int[] ans = new int[2*n]; int idx = 0; for (int i = 0; i < n; i++) { ans[idx++] = nums[i]; ans[idx++] = nums[n + i]; } return ans; }}