双指针技巧秒杀七道数组题目 :: labuladong的算法小抄 #1016
Replies: 61 comments 10 replies
-
寻找链表的倒数第 n 个元素那个最后还要判断slow.next是不是Null |
Beta Was this translation helpful? Give feedback.
-
奥 前面已经返回了。。 |
Beta Was this translation helpful? Give feedback.
-
142.环形链表II https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/kuai-man-zhi-zhen-shu-xue-tui-dao-by-yuhhen/ 这个数学证明比较好。 |
Beta Was this translation helpful? Give feedback.
-
反转数组c++一行代码{狗头}{狗头} void reverseString(vector<char>& s) {
for(int i = 0, j = s.size()-1; i <= j; ++i, --j) swap(s[i], s[j]);
} |
Beta Was this translation helpful? Give feedback.
-
167题,还能使用 二分查找+双指针,更优 class Solution {
public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length - 1;
while (i < j) {
int m = (i + j) >>> 1;
if (numbers[i] + numbers[m] > target) {
j = m - 1;
} else if (numbers[m] + numbers[j] < target) {
i = m + 1;
} else if (numbers[i] + numbers[j] > target) {
j--;
} else if (numbers[i] + numbers[j] < target) {
i++;
} else {
return new int[]{i + 1, j + 1};
}
}
return new int[]{0, 0};
}
} |
Beta Was this translation helpful? Give feedback.
-
第五题的代码是不是有问题啊 |
Beta Was this translation helpful? Give feedback.
-
27题,左右指针解法 class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
curr, tail = 0, len(nums) - 1
while curr <= tail:
if nums[curr] == val:
nums[curr], nums[tail] = nums[tail], nums[curr]
tail -= 1
else:
curr += 1
return curr |
Beta Was this translation helpful? Give feedback.
-
2022.4.2 mark |
Beta Was this translation helpful? Give feedback.
-
第五题代码 是 有问题 |
Beta Was this translation helpful? Give feedback.
-
@King-Eternal @buptcaicai76 第五题有啥问题,我把代码复制过去提交通过了啊 |
Beta Was this translation helpful? Give feedback.
-
打卡2022.4.4 |
Beta Was this translation helpful? Give feedback.
-
这里好像有点问题:”高效解决这道题就要用到快慢指针技巧:我们让慢指针 slow 走在后面,快指针 fast 走在前面探路,找到一个不重复的元素就赋值给 slow 并让 slow 前进一步。“ 应该是先让slow前进一步,再赋值吧 |
Beta Was this translation helpful? Give feedback.
-
@gzz0204 嗯,你说的有道理,我改下表述 |
Beta Was this translation helpful? Give feedback.
-
2022.4.17 打卡 |
Beta Was this translation helpful? Give feedback.
-
厉害呀,跟着刷完了双指针的数组题目,感觉越来越得心应手 |
Beta Was this translation helpful? Give feedback.
-
c++翻转数组一行代码实现 void reverseString(vector<char>& s) {
for (int left = 0, right = s.size() - 1; left < right; swap(s[left++], s[right--]));
} |
Beta Was this translation helpful? Give feedback.
-
第五题为什么是返回s[l+1,r]而不是s[l,r+1]呢?字符串索引不是从0开始的嘛? |
Beta Was this translation helpful? Give feedback.
-
牛逼,赞了 |
Beta Was this translation helpful? Give feedback.
-
LeetCode80,相较于给的题解 好理解一些
} |
Beta Was this translation helpful? Give feedback.
-
打卡 |
Beta Was this translation helpful? Give feedback.
-
删除链表重复节点那不要轻易直接delect,没有new的游离节点会自动回收的,记着new和delete配对就行 |
Beta Was this translation helpful? Give feedback.
-
滴,求职卡! |
Beta Was this translation helpful? Give feedback.
-
关于『5.最长回文串』的优化 根据labuladong的solution(记为S2),最快只能跑到:
但我在leetcode上发现有人用同样的思路,号称是java fastest solution(记为S3,https://leetcode.com/problems/longest-palindromic-substring/solutions/4069931/java-fastest-solution-easy-to-understand-9ms-o-n-memory-o-1/ ),但跑出来的结果是:
为什么同样的思路,跑出来的结果居然可以相差这么远? 比较之下发现差别在于:
但S3已经在判断回文函数里面进行了比较:
用于判断是否提前结束,非常有用。这段code所提升的性能:
|
Beta Was this translation helpful? Give feedback.
-
一刷打卡 |
Beta Was this translation helpful? Give feedback.
-
为什么第五题中,可以substring(l+1,r),当遍历到string的最后一位时,执行的是substring(string.length(),string.length()),为什么这里不会报越界错误? |
Beta Was this translation helpful? Give feedback.
-
快慢指针相等时不用交换和其它操作 |
Beta Was this translation helpful? Give feedback.
-
fast 可以初始化为1,无需和slow一样初始化为0进行比较 |
Beta Was this translation helpful? Give feedback.
-
golang中没有三元表达式,用if就好,gpt直接翻译了三元表达式..... func longestPalindrome(s string) string {
var res=""
for i:=0;i<len(s);i++{
s1:=palindrome(s,i,i)
s2:=palindrome(s,i,i+1)
if len(res)<len(s1){
res=s1
}
if len(res)<len(s2){
res=s2
}
}
return res
}
func palindrome(s string,l,r int)string{
for l >= 0 && r < len(s) && s[l]==s[r]{
l--
r++
}
return s[l+1:r]
} |
Beta Was this translation helpful? Give feedback.
-
分享一个Leetcode5 最长回文子序列的C++解法,可使用std::function |
Beta Was this translation helpful? Give feedback.
-
专题结束🔚 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
文章链接点这里:双指针技巧秒杀七道数组题目
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions