网站建设 应该付多少维护费呢,wordpress导入文件太大,网页制作基础教程内容,做三个月网站广告收入题目3
无重复字符的最长子串
思路
滑动窗口#xff0c;设定当前窗口左侧为i#xff0c;右侧为right#xff0c;当到达右侧边界时#xff0c;记录长度#xff0c; 然后删掉最左侧的字符#xff0c;即i1#xff1b;right则继续向后搜。
代码
public int lengthOfLong…题目3
无重复字符的最长子串
思路
滑动窗口设定当前窗口左侧为i右侧为right当到达右侧边界时记录长度 然后删掉最左侧的字符即i1right则继续向后搜。
代码
public int lengthOfLongestSubstring(String s) {// 哈希集合记录每个字符是否出现过SetCharacter occ new HashSetCharacter();int n s.length();// 右指针初始值为 -1相当于我们在字符串的左边界的左侧还没有开始移动int rk -1, ans 0;for (int i 0; i n; i) {if (i ! 0) {// 左指针向右移动一格移除一个字符occ.remove(s.charAt(i - 1));}while (rk 1 n !occ.contains(s.charAt(rk 1))) {// 不断地移动右指针occ.add(s.charAt(rk 1));rk;}// 第 i 到 rk 个字符是一个极长的无重复字符子串ans Math.max(ans, rk - i 1);}return ans;}题目128
最长连续序列
思路
先把序列遍历到set当中设以k为起点的序列如果有k1k2存在则直接长度响应增加。为了防止重复记录如果有k-1存在则条件2必然满足所以只有在k-1不存在时才会去执行2条件
代码
public int longestConsecutive(int[] nums) {SetInteger numsetnew HashSetInteger();for(int num:nums){numset.add(num);}int longestStreak0;for(int num:nums){if(!numset.contains(num-1)){int currentnum;int curlength1;while(numset.contains(current1)){current;curlength;}longestStreakMath.max(longestStreak,curlength);}}return longestStreak;}
题目102
二叉树的层次遍历
思路
使用队列。同层结点会一起输出。每次循环时把队列里的所有元素出队并且把各自的孩子结点进队如此这般就是一层一层的层次遍历。
代码 class Solution {public ListListInteger levelOrder(TreeNode root) {ListListInteger resultnew LinkedList();QueueTreeNode queuenew LinkedList();if(rootnull){return result;}queue.add(root);while(!queue.isEmpty()){int sizequeue.size();ListInteger tempnew LinkedList();//全部出队保证这层全部输出while(size0){TreeNode tqueue.poll();temp.add(t.val);if(t.left!null)queue.add(t.left);if(t.right!null)queue.add(t.right);size--;}result.add(temp);}return result;}
}