原题连接:https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
题目:找出数组中重复的数字
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
思路:简单的双层for循环
代码:
class Solution {public int findRepeatNumber(int[] nums) {
for (int i = 0; i for (int j = i+1; j if (nums[i]==nums[j]){return nums[j];}}}return 0;}
}
网页连接:https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/solutions/103048/mian-shi-ti-03-shu-zu-zhong-zhong-fu-de-shu-zi-b-4/
注意:本题与154 题相同:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/
原题连接:
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一次旋转,该数组的最小值为 1。
注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]] 。
思路:排序—输出
代码:
class Solution {public int minArray(int[] numbers) {
Arrays.sort(numbers);return numbers[0];}
}
网页连接:https://leetcode.cn/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/solutions/340801/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-by-leetcode-s/
原题连接:https://leetcode.cn/problems/ti-huan-kong-ge-lcof/
题目:请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
代码:
class Solution {public String replaceSpace(String s) {
String replace = s.replace(" ", "%20");return replace;}
}
网页链接:https://leetcode.cn/problems/ti-huan-kong-ge-lcof/solutions/103058/mian-shi-ti-05-ti-huan-kong-ge-by-leetcode-solutio/
原题连接:https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。
代码:
class Solution {public String replaceSpace(String s) {
String replace = s.replace(" ", "%20");return replace;}
}
网页链接:https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/solutions/1785640/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-en35/