上一页 1 2 3 4 5 6 7 8 9 10 ··· 20 下一页
摘要: 这道题容易想到的思路是:遍历链表存入list中,判断list是否回文 但综合类解法为:用快慢指针找到链表的中间位置,然后将链表从中间断开,将后半部分翻转,与前半部分比较。 class Solution { public boolean isPalindrome(ListNode head) { Li 阅读全文
posted @ 2020-04-10 17:10 yawenw 阅读(178) 评论(0) 推荐(0) 编辑
摘要: 两种解法: 1.用stack存储,重新构建字符串,比较是否相等。 2.双指针,有后向前遍历,此解法空间复杂度为O(1)。 JAVA class Solution { public boolean backspaceCompare(String S, String T) { return helpba 阅读全文
posted @ 2020-04-09 16:33 yawenw 阅读(143) 评论(0) 推荐(0) 编辑
摘要: 解法一:水平扫描 int indexOf(String str): 在字符串中检索str,返回其第一出现的位置,如果找不到则返回-1 class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length 阅读全文
posted @ 2020-04-08 23:52 yawenw 阅读(544) 评论(0) 推荐(0) 编辑
摘要: 两种方法:时间复杂度都为O(N) 1.转为数组保存,求出总长度的一半 空间复杂度O(n) 2.快慢指针法,输出慢指针 空间复杂度O(1) JAVA class Solution { public ListNode middleNode(ListNode head) { ListNode[] res 阅读全文
posted @ 2020-04-08 15:51 yawenw 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 三种解法: 第一种是在数组中查找O(n^2) 第二种是在HashSet中查找O(n) 第三种是在有序数组中查找O(nlogn) JAVA class Solution { public int countElements(int[] arr) { int res = 0; for(int i = 0 阅读全文
posted @ 2020-04-08 11:58 yawenw 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 本题有两种思路: 第一种是将key-value中的key通过排序存储到set集合,value添加该字符串 第二种是将key-value中的key通过数组计数方式存储,value添加该字符串 JAVA class Solution { public List<List<String>> groupAn 阅读全文
posted @ 2020-04-07 00:03 yawenw 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 本题涉及判断是否有loop 两种思路:1.用set存储路径 2.快慢指针法 JAVA:set()集合法 class Solution { public boolean isHappy(int n) { Set<Integer> process = new HashSet<>(); while(pro 阅读全文
posted @ 2020-04-03 00:00 yawenw 阅读(145) 评论(0) 推荐(0) 编辑
摘要: 1 """ 2 A peak element is an element that is greater than its neighbors. 3 Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element a 阅读全文
posted @ 2020-03-14 21:10 yawenw 阅读(166) 评论(0) 推荐(0) 编辑
摘要: 1 """ 2 Given an array of non-negative integers, you are initially positioned at the first index of the array. 3 Each element in the array represents 阅读全文
posted @ 2020-03-13 22:32 yawenw 阅读(137) 评论(0) 推荐(0) 编辑
摘要: 1 """ 2 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 3 Example 1: 4 Input: 5 [ 6 [ 1, 2, 3 阅读全文
posted @ 2020-03-13 21:11 yawenw 阅读(113) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 20 下一页