LC.P828[统计子串中的唯一字符]
LC.P828[统计子串中的唯一字符]
方法一:模拟+数学1234567891011121314151617181920212223242526272829class Solution { public int uniqueLetterString(String s) { // 存储last字符前一个字符所在位置 int[] lastIndexMap = new int[26]; // 存储cur字符当前所处位置 int[] curIndexMap = new int[26]; Arrays.fill(lastIndexMap, -1); Arrays.fill(curIndexMap, -1); char[] cs = s.toCharArray(); int ans = 0, length = cs.length; for (int i = 0; i < length; i++) { // ne ...
LC.P1706[球会落何处]
LC.P1706[球会落何处]
方法一:模拟12345678910111213141516171819class Solution { public int[] findBall(int[][] grid) { int n = grid[0].length; int[] ans = new int[n]; for (int j = 0; j < n; ++j) { int col = j; for (int[] row : grid) { int point = row[col]; col += point; // 移动 if (col < 0 || col == n || row[col] != point) { col = -1; break; ...
LC.P2569[更新数组后处理求和查询]
LC.P2569[更新数组后处理求和查询]
方法一:线段树123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109class Solution { public long[] handleQuery(int[] nums1, int[] nums2, int[][] queries) { SegmentTree tree = new SegmentTree(nums1); long s = 0; for (int x : nums2) s += x; int m = 0; for (int[] query : que ...
Netty01-nio
一. NIO 基础non-blocking io 非阻塞 IO
1. 三大组件1.1 Channel & Bufferchannel 有一点类似于 stream,它就是读写数据的双向通道,可以从 channel 将数据读入 buffer,也可以将 buffer 的数据写入 channel,而之前的 stream 要么是输入,要么是输出,channel 比 stream 更为底层
123graph LRchannel --> bufferbuffer --> channel
常见的 Channel 有
FileChannel
DatagramChannel
SocketChannel
ServerSocketChannel
buffer 则用来缓冲读写数据,常见的 buffer 有
ByteBuffer
MappedByteBuffer
DirectByteBuffer
HeapByteBuffer
ShortBuffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer
CharBuffer
1.2 Selec ...
LC.P794[有效的井字游戏]
LC.P794[有效的井字游戏]
方法一:模拟1234567891011121314151617181920212223242526272829303132333435363738394041class Solution { public boolean validTicTacToe(String[] board) { StringBuilder all = new StringBuilder(); int cntX = 0, cntO = 0; for (String b : board) { all.append(b); for (int i = 0; i < b.length(); i++) { char c = b.charAt(i); if (c == 'X') cntX++; else if (c == 'O' ...
LC.P791[自定义字符串排序]
LC.P791[自定义字符串排序]
方法一:计数模拟1234567891011121314151617class Solution { public String customSortString(String order, String s) { int[] cnt = new int[26]; for (int i = 0; i < s.length(); ++i) { ++cnt[s.charAt(i) - 'a']; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < order.length(); ++i) { char c = order.charAt(i); while (cnt[c - 'a']-- > 0) builder.append(c) ...
LC.P2208[将数组和减半的最少操作次数]
LC.P2208[将数组和减半的最少操作次数]
方法一:贪心+优先队列123456789101112131415161718class Solution { public int halveArray(int[] nums) { int ans = 0; PriorityQueue<Double> q = new PriorityQueue<>((a, b) -> b.compareTo(a)); double sum = 0, newSum = 0; for (int num : nums) { q.offer((double) num); sum += num; } while (newSum < sum / 2) { double x = q.poll(); newSum += x / 2; q.o ...
LC.P771[宝石与石头]
LC.P771[宝石与石头]
方法一:哈希表12345678910111213class Solution { public int numJewelsInStones(String jewels, String stones) { Set<Character> set = new HashSet<>(); for (int i = 0; i < jewels.length(); ++i) { set.add(jewels.charAt(i)); } int ans = 0; for (int i = 0; i < stones.length(); ++i) { if (set.contains(stones.charAt(i))) ++ans; } return ans; }}
时间复杂度:$O(m + n)$
空 ...
LC.P42[接雨水]
LC.P42[接雨水]
方法一:单调栈1234567891011121314151617class Solution { public int trap(int[] height) { Deque<Integer> stack = new ArrayDeque<>(); int ans = 0, n = height.length; for (int i = 0; i < n; ++i) { while (!stack.isEmpty() && height[i] > height[stack.peek()]) { int h = height[stack.pop()]; if (stack.isEmpty()) break; int length = i - stack.peek() - 1; int mi ...
LC.P860[柠檬水找零]
LC.P860[柠檬水找零]
方法一:贪心+模拟123456789101112131415161718192021222324class Solution { public boolean lemonadeChange(int[] bills) { int cnt5 = 0, cnt10 = 0; for (int bill : bills) { switch (bill) { case 5 -> ++cnt5; case 10 -> { --cnt5; ++cnt10; } case 20 -> { if (cnt10 > 0) { --cnt10; ...