当前位置:网站首页>Leetcode Hot 100 (topic 8) (232/88/451/offer10/offer22/344/)
Leetcode Hot 100 (topic 8) (232/88/451/offer10/offer22/344/)
2022-07-24 03:20:00 【Takeda】
1.leetcode232 Using stack to realize queue
/**
Time complexity is O(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.leetcode88 Merge two ordered arrays
/**
Time complexity O( 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.leetcode451 Sort the characters according to their frequency of occurrence
/**
Time complexity O( M + N) The length of a string Different number of characters in a string
*/
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. The finger of the sword offer10 The problem of frog jumping on the steps
/**
Time complexity O(N)
Large number out of bounds : With n increase , f(n) Will surpass Int32 even to the extent that Int64 Value range of , Cause the final return value error .
Minimum ten digit prime number 1000000007
1000000007 Is the smallest ten digit prime number . model 1000000007, It can be guaranteed that the value will always be int Within the scope of .
*/
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. The finger of the sword offer22 The entry node of a link in a list
/**
Time complexity O(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.leetcode344 Reverse string
/**
Time complexity O(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.
边栏推荐
- Huawei then responded to the rumor of "cleaning up employees over the age of 34". How can programmers cope with the "35 year old crisis"?
- Connected graph (day 72)
- The new idea 2022.2 was officially released, and the new features are nice
- Do you know how to do interface testing well?
- Rules for generating 13 digit barcode EAN-13 Code: the thirteenth digit is the verification code obtained by calculating the first twelve digits.
- String.split() the most detailed source code interpretation and precautions
- 正則錶達式 \b \B 深入淺出理解單詞邊界的匹配
- Basic knowledge of trigger (Part 2)
- The process of solving a bug at work
- An introductory example of structure and combinatorial ideas in go language
猜你喜欢

Microsoft win11/10 package manager Winget will support the installation of applications from zip files

Industrial controller, do you really know your five insurances and one fund?

Expressions régulières \ \ B \ \ b compréhension de l'appariement des limites des mots

正则表达式 \b \B 深入浅出理解单词边界的匹配

IO流分类整理

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

String.split()最详细源码解读及注意事项

CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID

Lagrange polynomial

什么是IMU?
随机推荐
Programmers may still be programmers, and coders may only be coders
What is IMU?
Unity 消息推送
Is it safe for qiniu to open an account? Is the Commission of 30000 reliable?
[C language] file operation
Unity message push
198. House raiding
Data Lake: introduction to Apache Hudi
Ugui source code analysis - maskutilities
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)
Basic use of Pinia
【无标题】
Mobile keyboard (day 73)
B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
水题: 接雨水
CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID
IO流分类整理
SolidWorks cannot reference references
错误代码0x80004005