![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Two Pointers
USC_ECE
这个作者很懒,什么都没留下…
展开
-
JZ2 替换空格
public class Solution { public String replaceSpace(StringBuffer str) { int count = 0; int oldLength = str.length(); for(int i = 0; i < oldLength; i++){ if(str.charAt(i) == ' '){ count++; .原创 2020-07-21 17:16:26 · 84 阅读 · 0 评论 -
LeetCode 415. Add Strings
从后往前遍历两个String,把对应位的和放入StringBuilder。循环结束后检查是否还有进位,有的话再添加一位1。将StringBuilder反转后再转化成String输出。代码如下:class Solution { public String addStrings(String num1, String num2) { int i = num1.length() - 1, j = num2.length() - 1; StringBuilder res原创 2020-07-19 14:47:49 · 81 阅读 · 0 评论 -
LeetCode 125. Valid Palindrome
这道题就是用left和right两个指针去遍历String就好了。还有就是记住Character的几个API:isLetterOrDigit() 和toLowerCase()。代码如下:class Solution { public boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while(left < right){ while(le.原创 2020-07-18 07:51:38 · 75 阅读 · 0 评论 -
LaiCode 397. Right Shift By N Characters
public class Solution { public String rightShift(String input, int n) { // Write your solution here if(input.length() == 0){ return input; } char[] array = input.toCharArray(); int k = n % array.length; reverse(array, 0, ar.原创 2020-07-12 09:08:30 · 225 阅读 · 0 评论 -
LeetCode 15. 3Sum
首先对数组进行排序,排序后固定一个数 nums[i],再使用左右指针指向 nums[i]后面的两端,数字分别为 nums[L] 和 nums[R],计算三个数的和 sum 判断是否满足为 0,满足则添加进结果集。如果 nums[i]大于 0,则三数之和必然无法等于 0,结束循环如果 nums[i] == nums[i-1],则说明该数字重复,会导致结果重复,所以应该跳过当 sum == 0 时,nums[L] == nums[L+1] 则会导致结果重复,应该跳过,L++当 sum == 0时,nu.原创 2020-07-12 08:43:44 · 102 阅读 · 0 评论 -
LaiCode 197. ReOrder Array
public class Solution { public int[] reorder(int[] array) { // Write your solution here if (array.length % 2 == 1) { reorder(array, 0, array.length - 2); } else { reorder(array, 0, array.length - 1); } return array; } private void reo.原创 2020-07-09 09:10:22 · 239 阅读 · 0 评论 -
LeetCode 186. Reverse Words in a String II
这道题其实是151的基础。代码如下:class Solution { public void reverseWords(char[] s) { reverse(s, 0, s.length - 1); int head = 0, tail = 0; for(int i = 0; i < s.length; i++){ if(s[i] != ' ' && (i == 0 || s[i - 1] == '原创 2020-07-08 14:07:11 · 101 阅读 · 0 评论 -
LeetCode 151. Reverse Words in a String
class Solution { public String reverseWords(String s) { // Remove the spaces String str = removeSpace(s); char[] words = str.toCharArray(); // Reverse the whole sentence reverse(words, 0, words.length - 1); ...原创 2020-07-08 10:49:13 · 116 阅读 · 0 评论 -
LeetCode 344. Reverse String
Iterative Approach class Solution { public void reverseString(char[] s) { int left = 0, right = s.length - 1; while(left < right){ char temp = s[left]; s[left] = s[right]; s[right] = temp;原创 2020-07-08 09:37:25 · 107 阅读 · 0 评论 -
LeetCode 1119. Remove Vowels from a String
class Solution { public String removeVowels(String S) { StringBuilder sb = new StringBuilder(); for(char c: S.toCharArray()){ if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){ continue; .原创 2020-07-07 09:13:51 · 91 阅读 · 0 评论 -
LeetCode 83. Remove Duplicates from Sorted List
这道题虽然是简单题,但是有些启发价值的。他的解法和第203题非常类似,唯一区别在于没有用dummyHead。排除linkedlist为空或者只有一个node的两种corner case; 再设置两个prev和curr两个指针分别指向head和head.next; 当curr不为空时:如果curr.val == prev.val, 则perv.next = curr.next; 否则 prev = curr; 两种情况都需要curr = curr.next;我自己的理解是prev的物理意义是无重复v原创 2020-06-12 06:01:25 · 101 阅读 · 0 评论 -
LeetCode 82. Remove Duplicates from Sorted List II
首先加一个假头,然后双指针prev和curr。主要考虑两种情况:1 -> 2, 1 -> 1 -> 2。https://leetcode.wang/leetCode-82-Remove-Duplicates-from-Sorted-ListII.html/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * L原创 2020-07-07 08:24:37 · 63 阅读 · 0 评论 -
LeetCode 80. Remove Duplicates from Sorted Array II
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-i-2/class Solution { public int removeDuplicates(int[] nums) { int slow = 0; for(int fast = 0; fast <原创 2020-07-07 07:35:30 · 81 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array
class Solution { public int removeDuplicates(int[] nums) { int slow = 0; for(int fast = 0; fast < nums.length; fast++){ if(slow == 0 || nums[fast] > nums[slow - 1]){ nums[slow++] = nums[fast]; .原创 2020-07-06 16:26:18 · 54 阅读 · 0 评论