每日刷题 五月集训 第 5 天 - Two-Pointer
917. Reverse Only Letters
题目介绍:
Given a string s, reverse the string according to the following rules:
All the characters that are not English letters remain in the same position.
All the English letters (lowercase or uppercase) should be reversed.
Return s after reversing it.
.
解题思路:
- 这题没什么好说的。
注意的地方 & 须记住的地方:
- Character.isLetter
代码部分:
class Solution {
public String reverseOnlyLetters(String s) {
int left = 0;
int right = s.length()-1;
StringBuilder sb = new StringBuilder(s);
while(left < right){
int valLeft = (int)sb.charAt(left);
int valRight = (int)sb.charAt(right);
if( !Character.isLetter(sb.charAt(left)) ){
left++;
}else if(!Character.isLetter(sb.charAt(right)) ){
right--;
}else{
char temp = sb.charAt(left);
sb.setCharAt(left, sb.charAt(right));
sb.setCharAt(right, temp);
left++;
right--;
}
}
s = sb.toString();
return s;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(n) (sol)
167. Two Sum II - Input Array Is Sorted
题目介绍:
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
解题思路:
- 这题没什么好说的,应该一看就要知道用双指针的。
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers.length == 0){
return null;
}
int left = 0;
int right = numbers.length -1;
int[] res = new int[2];
while(left < right){
int sum = numbers[left] + numbers[right];
if(sum == target){
res[0] = left+1;
res[1] = right+1;
return res;
}
if(sum < target){
left++;
}else{
right--;
}
}
return res;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(1)
165. Compare Version Numbers
题目介绍:
Given two version numbers, version1 and version2, compare them.
Version numbers consist of one or more revisions joined by a dot ‘.’. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.
Return the following:
If version1 < version2, return -1.
If version1 > version2, return 1.
Otherwise, return 0.
解题思路:
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public int compareVersion(String version1, String version2) {
if(version1.length() == 0 || version2.length() == 0 ){
return 0;
}
String[] arr1 = version1.split("\\.");
String[] arr2 = version2.split("\\.");
int len = Math.max(arr1.length, arr2.length);
for(int i = 0; i< len; i++){
int val1 = 0 ;
int val2 =0 ;
if(i<arr1.length){
val1 = Integer.parseInt(arr1[i]);
}else{
val1 = 0;
}
if(i<arr2.length){
val2 = Integer.parseInt(arr2[i]);
}else{
val2 = 0;
}
if(val1 > val2){
return 1;
}
if(val1 < val2){
return -1;
}
}
return 0;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(n)
443. String Compression
题目介绍:
Given an array of characters chars, compress it using the following algorithm:
Begin with an empty string s. For each group of consecutive repeating characters in chars:
If the group’s length is 1, append the character to s.
Otherwise, append the character followed by the group’s length.
The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.
After you are done modifying the input array, return the new length of the array.
You must write an algorithm that uses only constant extra space.
解题思路:
- 看到题目说要记录连续的字符的数量,所以采用双指针。
- 用第二个指针来计算第一个指针指的字符出现了多少次,然后存储下来。并把数字变成字符串然后依次存储起来。
- 把list中的字符都写进input array之中。
注意的地方 & 须记住的地方:
- Integer to String : Integer.toString()
代码部分:
class Solution {
public int compress(char[] chars) {
int ptr1 = 0;
int ptr2 = 0;
List<Character> list = new ArrayList<>();
while(ptr1 < chars.length){
ptr2 = ptr1+1;
int count = 1;
while( ptr2 < chars.length && chars[ptr2] == chars[ptr1]){
count++;
ptr2++;
}
list.add(chars[ptr1]);
if(count == 1){
}else{
String num = Integer.toString(count);
for(int i = 0; i< num.length(); i++ ){
list.add( num.charAt(i));
}
}
ptr1 = ptr2;
}
for(int i = 0; i< list.size(); i++){
chars[i] = list.get(i);
}
return list.size();
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(n)
这篇博客介绍了四道LeetCode上的使用双指针解决的算法问题:917. Reverse Only Letters、167. Two Sum II - Input Array Is Sorted、165. Compare Version Numbers和443. String Compression。每道题目都详细讲解了解题思路,重点在于理解如何运用双指针来优化解决方案,并给出了时间复杂度和空间复杂度分析。
181

被折叠的 条评论
为什么被折叠?



