当前位置:网站首页>Leetcode Hot 100 (Brush Topic 8) (232 / 88 / 451 / offer10 / offer22 / 344 /)
Leetcode Hot 100 (Brush Topic 8) (232 / 88 / 451 / offer10 / offer22 / 344 /)
2022-07-24 03:20:00 【Takeda】
1.leetcode232Mise en file d'attente avec pile
/**
La complexité temporelle estO(1)
*/
class MyQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public MyQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void in2out() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
2.leetcode88Fusionner deux tableaux ordonnés
/**
Complexité temporelleO( M * N )
*/
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = 0, p2 = 0;
int[] sorted = new int[m + n];
int cur;
while (p1 < m || p2 < n) {
if (p1 == m) {
cur = nums2[p2++];
} else if (p2 == n) {
cur = nums1[p1++];
} else if (nums1[p1] < nums2[p2]) {
cur = nums1[p1++];
} else {
cur = nums2[p2++];
}
sorted[p1 + p2 - 1] = cur;
}
for (int i = 0; i != m + n; ++i) {
nums1[i] = sorted[i];
}
}
}
3.leetcode451Trier par fréquence d'occurrence des caractères
/**
Complexité temporelleO( M + N) La longueur d'une chaîne Le nombre de caractères d'une chaîne est différent
*/
class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
int maxFreq = 0;
int length = s.length();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
int frequency = map.getOrDefault(c, 0) + 1;
map.put(c, frequency);
maxFreq = Math.max(maxFreq, frequency);
}
StringBuffer[] buckets = new StringBuffer[maxFreq + 1];
for (int i = 0; i <= maxFreq; i++) {
buckets[i] = new StringBuffer();
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
char c = entry.getKey();
int frequency = entry.getValue();
buckets[frequency].append(c);
}
StringBuffer sb = new StringBuffer();
for (int i = maxFreq; i > 0; i--) {
StringBuffer bucket = buckets[i];
int size = bucket.length();
for (int j = 0; j < size; j++) {
for (int k = 0; k < i; k++) {
sb.append(bucket.charAt(j));
}
}
}
return sb.toString();
}
}
4.Un doigt d'épée.offer10Les grenouilles sautent sur les marches
/**
Complexité temporelleO(N)
Grand nombre hors de portée: Avec n Augmentation, f(n) Va dépasser Int32 Et même Int64 Plage de valeurs pour,Provoque une erreur dans la valeur de retour finale.
Nombre minimum de dix premiers chiffres 1000000007
1000000007 Est le plus petit nombre premier à dix chiffres.Module1000000007,Vous pouvez vous assurer que la valeur est toujoursintDans les limites de.
*/
class Solution {
public int numWays(int n) {
if (n == 0 || n == 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = (dp[i - 2] + dp[i - 1]) % 1000000007;
}
return dp[n];
}
}5.Un doigt d'épée.offer22Noeud d'entrée de l'anneau dans la liste des liens
/**
Complexité temporelleO(N)
*/
class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next != null) {
fast = fast.next.next;
} else {
return null;
}
if (fast == slow) {
ListNode ptr = head;
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
6.leetcode344Inverser la chaîne
/**
Complexité temporelleO(N)
*/
class Solution {
public void reverseString(char[] s) {
int n = s.length;
for (int left = 0, right = n - 1; left < right; ++left, --right) {
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
}
}
}
7.
边栏推荐
- Simulink代码生成: 可变子系统及其代码
- Ugui source code analysis - maskutilities
- C dynamic memory management details
- Ugui source code analysis - maskablegraphic
- 轮播图van-swipe的报错:cannot read a properties of null(reading width)
- Outlook client outlook.com mailbox setting method
- 21st day of written test mandatory training
- Regular expression \b \b understand word boundary matching in simple terms
- JS Array isaarray () Type of
- I developed an app similar to wechat runnable applet with fluent
猜你喜欢

A series of problems of dp+ backtracking segmentation palindrome string

实现两个页面之前的通信(使用localStorage)

322. Change

The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)

Ugui source code analysis - Mask

Ugui source code analysis - imaskable

Outlook client outlook.com mailbox setting method

MySQL学习——MySQL软件的安装及环境配置(Windows)详细!

Lagrange polynomial

OSPF comprehensive experimental configuration
随机推荐
String.split() the most detailed source code interpretation and precautions
JVM initial
08 reptile project
JIRA automation experience sharing for 2 years
Simulink代码生成: 可变子系统及其代码
Cannot resolve symbol 'override' of idea clone‘
IDEA Clone的项目报Cannot resolve symbol ‘Override‘
Qt自定义类使用自定义含参信号与槽
uva1344
198. House raiding
Insist on accompanying study
OSPF comprehensive experimental configuration
Microsoft win11/10 package manager Winget will support the installation of applications from zip files
Is it safe for qiniu to open an account? Is the Commission of 30000 reliable?
It's solved with a cry
JVM初始
FTP服務與配置
Minimum exchange times
在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
SSM based blog system [with background management]