LC.P2679[矩阵中的和]
LC.P2679[矩阵中的和]
方法一:暴力123456789101112131415161718192021222324class Solution { public int matrixSum(int[][] nums) { int ans = 0, m = nums.length, n = nums[0].length; int[] score = new int[m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int maxIndex = 0; for (int k = 0; k < n; ++k) { if (nums[j][maxIndex] < nums[j][k]) { maxIndex = k; ...
LC.P393[UTF-8编码验证]
LC.P393[UTF-8编码验证]
方法一:模拟12345678910111213141516171819class Solution { public boolean validUtf8(int[] data) { int n = data.length; for (int i = 0; i < n; ) { int t = data[i], j = 7; while (j >= 0 && (((t >> j) & 1) == 1)) --j; int cnt = 7 - j; if (cnt == 1 || cnt > 4) return false; if (i + cnt - 1 >= n) return false; for (int k = i + 1; k < i + cnt; ++k) { ...
LC.P385[迷你语法分析器]
LC.P385[迷你语法分析器]
方法一:栈123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * // Constructor initializes an empty nested list. * public NestedInteger(); * * // Constructor initializes a single integer. * public NestedInte ...
LC.P284[顶端迭代器]
LC.P284[顶端迭代器]
方法一:模拟12345678910111213141516171819202122232425262728293031// Java Iterator interface reference:// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.htmlclass PeekingIterator implements Iterator<Integer> { Iterator<Integer> iterator; Integer next; public PeekingIterator(Iterator<Integer> iterator) { // initialize any member here. this.iterator = iterator; if (iterator.hasNext()) next = iterator.next(); } ...
LC.P445[两数相加II]
LC.P445[两数相加II]
方法一:栈+迭代1234567891011121314151617181920212223242526272829303132333435/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Deque<Integer> s1 = new ...
LC.P400[第N位数字]
LC.P400[第N位数字]
方法一:模拟12345678910111213141516171819202122232425class Solution { // 基本规律: // [1, 9] 有9 * 1个数字 => 9 * 1 * 1 => 9 * 10^0 * 1 // [10, 99] 有90 * 2个数字 => 9 * 10 * 2 => 9 * 10^1 * 2 // [100, 999] 有900 * 3个数字 => 9 * 100 * 3 => 9 * 10^2 * 3 // [1000, 9999] 有9000 * 4个数字 => 9 * 1000 * 4 => 9 * 10^3 * 4 // => 9 * 10^(n-1) * n // ... public int findNthDigi ...
LC.P166[分数到小数]
LC.P166[分数到小数]
方法一:模拟1234567891011121314151617181920212223242526272829class Solution { public String fractionToDecimal(int numerator, int denominator) { long a = numerator, b = denominator; if (a % b == 0) return String.valueOf(a / b); StringBuilder builder = new StringBuilder(); // 其中一个数为负数,先加负号 if (a * b < 0) { builder.append('-'); a = Math.abs(a); b = Math.abs(b); } // 计算小数点前的 ...
LC.P2[两数相加]
LC.P2[两数相加]
方法一:递归123456789101112131415161718192021222324252627282930/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return dfs(l1, l2, 0); } private Li ...
LC.P306[累加数]
LC.P306[累加数]
方法一:回溯1234567891011121314151617181920212223242526272829303132333435363738394041424344454647class Solution { String num; int n; List<List<Integer>> list = new ArrayList<>(); public boolean isAdditiveNumber(String num) { this.num = num; n = num.length(); return dfs(0); } private boolean dfs(int u) { int m = list.size(); if (u == n) return m >= 3; int max = num.charAt(u) == '0' ...
LC.P2490[回环句]
LC.P2490[回环句]
方法一:模拟12345678910class Solution { public boolean isCircularSentence(String sentence) { String[] s = sentence.trim().split(" "); int n = s.length; for (int i = 0; i < n; ++i) { if (s[i].charAt(s[i].length() - 1) != s[(i + 1) % n].charAt(0)) return false; } return true; }}
时间复杂度:$O(n)$
空间复杂度:$O(n)$
方法二:模拟(空间优化)123456789101112class Solution { public boolean isCircularSentence(Stri ...