当前位置:网站首页>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 :

边栏推荐
- What is the use of index aliases in ES
- Insect operator overloaded a fun
- Network remote access using raspberry pie
- Self created notes (unique in the whole network, continuously updated)
- Never use redis expired monitoring to implement scheduled tasks!
- 9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
- [path of system analyst] Chapter 15 double disk database system (database case analysis)
- I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
- 33. Use rgbd camera for target detection and depth information output
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
猜你喜欢

Correlation analysis related knowledge

Mongodb series window environment deployment configuration

Included angle of 3D vector

Cloudcompare - Poisson reconstruction

Logical operation

Exercise set 1

character constants

Gartner 2022 Top Strategic Technology Trends Report

9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions

Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
随机推荐
Es common grammar I
Zero basics of C language lesson 8: Functions
近期比较重要消息
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
BP neural network for prediction
Wechat applet - bind and prevent event bubble catch
Lucky numbers in the matrix
Exercises under insect STL string
Niuke challenge 48 e speed instant forwarding (tree over tree)
It is better and safer to choose which securities company to open an account for flush stock
Half search, character array definition, character array uses D11
Pointer
Difference between classification and regression
Never use redis expired monitoring to implement scheduled tasks!
Luogu p4513 xiaobaiguang Park
Matlab programming related knowledge
C language ---getchar() and putchar()
FreeFileSync 文件夹比较与同步软件
程序员必备,一款让你提高工作效率N倍的神器uTools
Insect operator overloaded a fun
https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/