【LeetCode--026--删除排序数组中的重复项(java)】教程文章相关的互联网学习教程文章

【Leetcode】【简单】【189. 旋转数组】【JavaScript】【代码】

题目描述189. 旋转数组给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。示例 1:输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释:向右旋转 1 步: [7,1,2,3,4,5,6]向右旋转 2 步: [6,7,1,2,3,4,5]向右旋转 3 步: [5,6,7,1,2,3,4]示例 2:输入: [-1,-100,3,99] 和 k = 2输出: [3,99,-1,-100]解释: 向右旋转 1 步: [99,-1,-100,3]向右旋转 2 步: [3,99,-1,-100]说明:尽可能想出更多的解决方案,至少有三种不...

[LeetCode][Java] ZigZag Conversion【代码】

题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given numberof rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows:string convert(string text, int nRows);convert("PAYP...

[LeetCode-JAVA] Partition List【代码】

题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2->4->3->5. 思路:找出以x为划分的两段链表,并连接在一起,注意维护链表头。 代码:publicclass Solution {public ListNode pa...

Java for LeetCode 148 Sort List【代码】

Sort a linked list in O(n log n) time using constant space complexity.解题思路:归并排序、快速排序、堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下: public ListNode sortList(ListNode head) {if(head==null||head.next==null)return head;Queue<ListNode> pQueue = new PriorityQueue<ListNode>(0,new Comparator<ListNode>() {//提交的时候不能加初始容量(第一...

Valid Palindrome LeetCode Java【代码】

描述Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoringcases.For example,”A man, a plan, a canal: Panama” is a palindrome.”race a car” is not a palindrome.Note: Have you consider that the string might be empty? is is a good question to ask during aninterview.For the purpose of this problem, we define empty string as valid palindrome. 分析去标...

[LeetCode][Java] Minimum Depth of Binary Tree

题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.题意:给定一棵二叉树。返回它的最小高度。最小高度是指从根节点到近期的叶子节点的最短路径中的节点的数目。算法分析: * 借助堆 * 类似《Binary Tree Level Order Traversal》中的算法 * 出现下一层无自带的情况,马上退出,返回该层的层数AC代码:/...

leetcode 1145. Binary Tree Postorder Traversal ----- java【代码】

Given a binary tree, return the postorder traversal of its nodes‘ values.For example:Given binary tree {1,#,2,3}, 12/3 return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively? 求后序遍历,要求不使用递归。 使用栈,从后向前添加。/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { va...

Leetcode 263. Ugly Number JAVA语言【代码】

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is typically treated as an ugly number.题意:判断一个数是不是丑数。只能被2,3,5因子的。public class Solution { public boolean isUgly(int num) { ...

Java for LeetCode 221 Maximal Square【代码】

Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest square containing all 1‘s and return its area.For example, given the following matrix:1 0 1 0 0 1 0 11 1 1 1 11 1 1 0 0 1 0 Return 4.解题思路:dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长,JAVA实现如下: public int maximalSquare(char[][] matrix) {if (matrix.length == 0 || matrix[0].length == 0)retu...

LeetCode-21 Merge Two Sorted Lists Solution (with Java)【代码】【图】

1. Description:2. Examples:3.Solutions: 1/** 2 * Created by sheepcore on 2019-05-073 * Definition for singly-linked list.4 * public class ListNode {5 * int val;6 * ListNode next;7 * ListNode(int x) { val = x; }8 * }9*/10class Solution { 11public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 12 ListNode head = new ListNode(-1); 13 ListNode p = l1, q = l2, tail = hea...

Java [Leetcode 204]Count Primes【代码】【图】

题目描述:Description:Count the number of prime numbers less than a non-negative number, n.解题思路:Let‘s start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrimefunction would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better?As we know the nu...

Java [Leetcode 326]Power of Three【代码】

题目描述:Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?解题思路:对给定的数求以3为底的对数,然后再将结果用于3的次幂,看是否与原来的数相同。代码如下:public class Solution {public boolean isPowerOfThree(int n) {return n <= 0 ? false : n == Math.pow(3, Math.round(Math.log(n) / Math.log(3)));} } 原文:http://www...

LeetCode229 MajorityElementII java题解

<p>题目:</p><p><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Given an integer array of size </span><span style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">n</span><span style="color: rgb(51, 51, 51); font-fa...

LeetCode 147. Insertion Sort List 链表插入排序 C++/Java【代码】【图】

Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-place into the sorted listAlgorithm of Insertion Sort:1Insertion sort iterates, consuming one input element each repetition, and growing a sorted outp...

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配【代码】【图】

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?‘ and ‘*‘.‘?‘ Matches any single character. ‘*‘ Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).Note:s could be empty and contains only lowercase letters a-z.p could be empty and contains only lowercase letters...