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

The finger of the sword Offer 21. Adjust the array order so that the odd Numbers precede the even Numbers https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/

  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 : 

The finger of the sword Offer 57. And for s Two numbers of https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/

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 :

The finger of the sword Offer 58 - I. Flip word order https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/

 

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 :

原网站

版权声明
本文为[hedgehog:]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170509327169.html