学习剑指offer: 第13天

165 阅读1分钟

调整数组顺序使奇数位于偶数前面

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。

输入: nums = [1,2,3,4]
输出: [1,3,2,4] 
注: [3,1,2,4] 也是正确的答案之一。

提示:

  1. 0 <= nums.length <= 50000
  2. 0 <= nums[i] <= 10000

leetcode-cn.com/problems/di…

class Solution {
    public int[] exchange(int[] nums) {
        if(nums.length == 0){
            return nums;
        }

        int left = 0;
        int right = nums.length -1;
        while(left < right){
            if(nums[left] % 2 == 0 && nums[right] % 2 == 1){
                 int temp = nums[left];
                 nums[left] = nums[right];
                 nums[right] = temp;
            }

            if(nums[left] % 2 == 1){
                left++;
            }
            if(nums[right] %2 == 0){
                right--;
            }
        }
        return nums;
    }
}

和为s的两个数字

输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。

输入: nums = [2,7,11,15], target = 9
输出: [2,7] 或者 [7,2]
输入: nums = [10,26,30,31,47,60], target = 40
输出: [10,30] 或者 [30,10]

限制:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6

leetcode-cn.com/problems/he…

class Solution {
    public int[] twoSum(int[] nums, int target) {
        if(nums.length == 0){
            return new int[0];
        }
        if(nums.length ==1 && nums[0] == target){
            return nums;
        }

        int left = 0;
        int right = nums.length -1;
        int[] result = new int[2]; 
        while(left <right){
            if(nums[left] + nums[right] < target){
                left++;
                continue;
            }else if (nums[left] + nums[right] > target){
                right--;
                continue;
            }else {
                result[0] = nums[left];
                result[1] = nums[right];
                break;
            }
        }
        return result;
    }
}

翻转单词顺序

输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. ",则输出"student. a am I"。

输入: "the sky is blue"
输出: "blue is sky the"
输入: "  hello world!  "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
输入: "a good   example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

说明:

  • 无空格字符构成一个单词。
  • 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
  • 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个

leetcode-cn.com/problems/fa…

class Solution {
    public String reverseWords(String s) {
        boolean flag = false;
        StringBuilder sb = new StringBuilder();
        String re = "";
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i) == ' ' ){
                if(re.length() >0 && s.charAt(i-1) != ' '){
                    re = sb +" "+  re;
                } else {
                    re = sb +  re;
                }
                sb = new StringBuilder();
                continue;
            }
            sb.append(s.charAt(i)+"");
            if(i==s.length()-1&& s.charAt(i) != ' ' && sb.length() != 0){
                 if(re.length() ==0){
                    re = sb +"";
                 }else {
                    re = sb +" "+  re;
                 }
                 
            }
        }
        return re;
    }

    private String reverse(String str){
        String res = "";
        for(int i= str.length()-1;i>0;i--){
            res += str.charAt(i) +"";
        }
        return res;
    }
}