当前位置:网站首页>Sword finger offer 21.57.58 I Double pointer (simple)
Sword finger offer 21.57.58 I Double pointer (simple)
2022-06-26 14:13:00 【hedgehog:】
27.
subject :
idea : Double pointers judge the parity of two numbers from the beginning to the end , To move or exchange in the middle
class Solution {
public int[] exchange(int[] nums) {
int i=0;
int j=nums.length-1;
while(i<j){
if((nums[i]&1)==0 && (nums[j]&1)==1){// accidentally p.
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
i++;j--;
}
else if((nums[i]&1)==1 && (nums[j]&1)==1)// p. p.
i++;
else if((nums[i]&1)==0 && (nums[j]&1)==0)// accidentally accidentally
j--;
else{// p. accidentally
i++;
j--;
}
}
return nums;
}
}
result :
57.
subject :
Code :
class Solution {
public int[] twoSum(int[] nums, int target) {
int i=0;
int j=nums.length-1;
while(i<j){
if(target-nums[j]<nums[i])
// explain j It's too big
j--;
else if(target-nums[i]>nums[j])
// explain i It's too small
i++;
else
break;
}
return new int[]{nums[i],nums[j]};
}
}
result :
58Ⅰ.
subject :
At first it was seen as reversing all the characters , Start with characters , Not getting the right results . After referring to others, you realize that you should start with words
Reference resources https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/solution/mian-shi-ti-58-i-fan-zhuan-dan-ci-shun-xu-shuang-z/
Idea one : Split a string into an array of strings with spaces , Store each word , You can traverse the splicing in reverse order , Note that the string encountered should be skipped .
Code 1 :
class Solution {
public String reverseWords(String s) {
String[] strs = s.trim().split(" "); // Delete leading and trailing spaces , Split string
StringBuilder res = new StringBuilder();
for(int i = strs.length - 1; i >= 0; i--) { // Traverse the word list in reverse order
if(strs[i].equals(""))
continue; // If an empty string is encountered, skip
res.append(strs[i] + " "); // Splice words into StringBuilder
}
return res.toString().trim(); // Convert to string , Delete trailing spaces , And back to
}
}
result :
Idea 2 : Use double pointer , Traversing strings in reverse order , Take out the substring in turn ( word ), Skip spaces during .
class Solution {
public String reverseWords(String s) {
s = s.trim(); // Delete leading and trailing spaces
// Reverse search
int j = s.length() - 1, i = j;
StringBuilder res = new StringBuilder();
while(i >= 0) {
while(i >= 0 && s.charAt(i) != ' ')
i--; // Search for the first space
res.append(s.substring(i + 1, j + 1) + " "); // Add words
while(i >= 0 && s.charAt(i) == ' ')
i--; // Skip spaces between words
j = i; // j The last character pointing to the next word
}
return res.toString().trim(); // Convert to a string and return
}
}
result :
边栏推荐
- 基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
- Hard (magnetic) disk (I)
- Range of types
- Educational Codeforces Round 117 (Rated for Div. 2)E. Messages
- Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
- Original code, inverse code and complement code
- First k large XOR value problem
- Niuke challenge 53:c. strange magic array
- Memory considerations under bug memory management
- Win10 home vs pro vs enterprise vs enterprise LTSC
猜你喜欢
Tips for using nexys A7 development board resources
Jenkins build prompt error: eacces: permission denied
Zero basics of C language lesson 7: break & continue
Common operation and Principle Exploration of stream
Range of types
Reprint - easy to use wechat applet UI component library
9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
服务器创建虚拟环境跑代码
Related knowledge of libsvm support vector machine
ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
随机推荐
常用控件及自定义控件
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Knowledge about adsorption
Codeforces Global Round 21A~D
Svn commit error after deleting files locally
33. Use rgbd camera for target detection and depth information output
Hard (magnetic) disk (II)
The most critical elements of team management
Is it safe to open a securities account? Is there any danger
A must for programmers, an artifact utools that can improve your work efficiency n times
Practice with the topic of bit operation force deduction
Half search, character array definition, character array uses D11
"Scoi2016" delicious problem solution
PHP非对称加密算法(RSA)加密机制设计
array
Network remote access using raspberry pie
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
[jsoi2015] string tree
Pointer