第 254 场周赛

5843. 作为子字符串出现在单词中的字符串数目

给你一个字符串数组 patterns 和一个字符串 word ,统计 patterns 中有多少个字符串是 word 的子字符串。返回字符串数目。

子字符串 是字符串中的一个连续字符序列。


示例 1:

输入:patterns = ["a","abc","bc","d"], word = "abc"
输出:3
解释:
- "a" 是 "abc" 的子字符串。
- "abc" 是 "abc" 的子字符串。
- "bc" 是 "abc" 的子字符串。
- "d" 不是 "abc" 的子字符串。
patterns 中有 3 个字符串作为子字符串出现在 word 中。
示例 2:

输入:patterns = ["a","b","c"], word = "aaaaabbbbb"
输出:2
解释:
- "a" 是 "aaaaabbbbb" 的子字符串。
- "b" 是 "aaaaabbbbb" 的子字符串。
- "c" 不是 "aaaaabbbbb" 的字符串。
patterns 中有 2 个字符串作为子字符串出现在 word 中。
示例 3:

输入:patterns = ["a","a","a"], word = "ab"
输出:3
解释:patterns 中的每个字符串都作为子字符串出现在 word "ab" 中。
class Solution {
    public int numOfStrings(String[] patterns, String word) {
        int cnt = 0;
        for (String s : patterns) {
            if (word.indexOf(s) >= 0) ++ cnt;
        }
        return cnt;
    }
}

5832. 构造元素不等于两相邻元素平均值的数组

给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值 。

更公式化的说法是,重新排列的数组应当满足这一属性:对于范围1 <= i < nums.length - 1中的每个i(nums[i-1] + nums[i+1]) / 2 不等于nums[i]均成立 。

返回满足题意的任一重排结果。

示例 1:

输入:nums = [1,2,3,4,5]
输出:[1,2,4,5,3]
解释:
i=1, nums[i] = 2, 两相邻元素平均值为 (1+4) / 2 = 2.5
i=2, nums[i] = 4, 两相邻元素平均值为 (2+5) / 2 = 3.5
i=3, nums[i] = 5, 两相邻元素平均值为 (4+3) / 2 = 3.5

示例 2:

输入:nums = [6,2,0,9,7]
输出:[9,7,6,2,0]
解释:
i=1, nums[i] = 7, 两相邻元素平均值为 (9+6) / 2 = 7.5
i=2, nums[i] = 6, 两相邻元素平均值为 (7+2) / 2 = 4.5
i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3
class Solution {
   public int[] rearrangeArray(int[] nums) {
       int n = nums.length;
       Arrays.sort(nums);
       int[] ans = new int[n];
       int idx = 0;
       for(int l = 0, r = n - 1; l < r; l++, r--){
           ans[idx++] = nums[l];
           ans[idx++] = nums[r];
       }
       if(n % 2 == 1) ans[idx] = nums[n / 2];
       return ans;
   }
}
class Solution 
{
    public int[] rearrangeArray(int[] nums) 
    {
        int n = nums.length;

        Arrays.sort(nums);

        int i = 0;
        while (i+1 < n)
        {
            int tmp = nums[i];
            nums[i] = nums[i + 1];
            nums[i + 1] = tmp;

            i += 2;
        }

        return nums;
    }
}
class Solution {
    public int[] rearrangeArray(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        int m  = (n + 1) / 2;
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < m; ++i) {
            res.add(nums[i]);
            if (i + m < n) {
                res.add(nums[i + m]);
            }
        }
        int[] arr1 = res.stream().mapToInt(Integer::valueOf).toArray();
        return  arr1;
    }
}
class Solution {
    public int[] rearrangeArray(int[] nums) {
        Arrays.sort(nums);
        int[] ans = new int[nums.length];
        int left = 0, right = nums.length - 1, index = -1;
        while (left <= right) {
            if (index ++ % 2 == 0) {
                ans[index] = nums[left ++];
            } else {
                ans[index] = nums[right --];
            }
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> rearrangeArray(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        for (int i = 2; i < nums.size(); i+= 2) {
            swap(nums[i -1], nums[i]);
        }
        return nums;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值