当前位置:网站首页>leetcode hot 100(刷题篇8)(232/88/451/offer10/offer22/344/)
leetcode hot 100(刷题篇8)(232/88/451/offer10/offer22/344/)
2022-07-24 03:19:00 【武田】
1.leetcode232用栈实现队列
/**
时间复杂度均为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合并两个有序数组
/**
时间复杂度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根据字符出现频率排序
/**
时间复杂度O( M + N) 一个字符串的长 一个字符串不一样的字符数
*/
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.剑指offer10青蛙跳台阶问题
/**
时间复杂度O(N)
大数越界: 随着 n 增大, f(n) 会超过 Int32 甚至 Int64 的取值范围,导致最终的返回值错误。
最小的十位质数1000000007
1000000007 是最小的十位质数。模1000000007,可以保证值永远在int的范围内。
*/
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.剑指offer22链表中环的入口节点
/**
时间复杂度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反转字符串
/**
时间复杂度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.
边栏推荐
- LCD1602 - binge 51
- FTP服务与配置
- How does the small program mall refine the operation of members?
- Exttestngireporterlistener all codes
- Ugui source code analysis - stencilmaterial
- Mobile keyboard (day 73)
- JIRA automation experience sharing for 2 years
- Xiaodi and Xiaohui
- Open source quantum development framework cirq
- Acwing 4498. pointer (DFS)
猜你喜欢

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架

What is IMU?
![SSM based blog system [with background management]](/img/6b/6a488f5d6926de07c8b1b365362ff6.png)
SSM based blog system [with background management]

Secondary development of ArcGIS JS API -- loading national sky map

Data Lake: introduction to Apache Hudi

Take you into the world of MySQL mvcc

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"?

Detailed explanation of wechat official account online customer service access methods and functions

FTP服务与配置

实现两个页面之前的通信(使用localStorage)
随机推荐
如何在 pyqt 中实现桌面歌词
Express内置的中间件
[AMC] federal quantification
Example of producer consumer code implemented by the destructor framework without lock
Is the reverse repurchase of treasury bonds safe? How to open an account online?
Daily gossip (I)
The next stop of data visualization platform | gifts from domestic open source data visualization datart "super iron powder"
FTP service and configuration
CMT 注册——Google Scholar Id,Semantic Scholar Id,和 DBLP Id
Read and understand the advantages of the LAAS scheme of elephant swap
In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!
Keras deep learning practice (15) -- realize Yolo target detection from scratch
JS array isaarray() typeof
How to realize software function safety
Connected graph (day 72)
Ue5 split screen (small map) solution
uva1344
Advantages, disadvantages and summary of sequence list and linked list
WPS前骨干历时10年打造新型软件,Excel用户:我为此改用WPS
How does the small program mall refine the operation of members?