
获得徽章 0
赞了这篇沸点
赞了这篇沸点
赞了这篇沸点
赞了这篇文章
赞了这篇文章
赞了这篇沸点
#每日一题# 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例输入: s = "abcabcbb"
示例输出: 3
import java.util.HashSet;
import java.util.Set;
public class LongestSubstring {
public static void main(String[] args) {
String s = "abcabcbb";
int length = lengthOfLongestSubstring(s);
System.out.println(length);
}
public static int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
} else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}
示例输入: s = "abcabcbb"
示例输出: 3
import java.util.HashSet;
import java.util.Set;
public class LongestSubstring {
public static void main(String[] args) {
String s = "abcabcbb";
int length = lengthOfLongestSubstring(s);
System.out.println(length);
}
public static int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
} else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}
展开
评论
点赞
#每日一题# 给定一个整数数组,将该数组中的元素向右移动k个位置,其中k是非负数。
示例输入: nums = [1,2,3,4,5,6,7], k = 3
示例输出: [5,6,7,1,2,3,4]
public class RotateArray {
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6,7};
int k = 3;
rotate(nums, k);
System.out.println(Arrays.toString(nums));
}
public static void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
示例输入: nums = [1,2,3,4,5,6,7], k = 3
示例输出: [5,6,7,1,2,3,4]
public class RotateArray {
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6,7};
int k = 3;
rotate(nums, k);
System.out.println(Arrays.toString(nums));
}
public static void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
展开
评论
点赞
#每日一题# 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的下标。
示例输入: nums = [2, 7, 11, 15], target = 9
示例输出: [0, 1]
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = twoSum(nums, target);
System.out.println(result[0] + " " + result[1]);
}
public static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
示例输入: nums = [2, 7, 11, 15], target = 9
示例输出: [0, 1]
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = twoSum(nums, target);
System.out.println(result[0] + " " + result[1]);
}
public static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
展开
评论
点赞
#每日一题# 给定一个字符串,找出其中出现次数最多的字符,并输出该字符及其出现次数。
示例输入: "abbcccddddeeeee"
示例输出: e 5
import java.util.HashMap;
import java.util.Map;
public class MaxCharFrequency {
public static void main(String[] args) {
String str = "abbcccddddeeeee";
Map<Character, Integer> freqMap = new HashMap<>();
int maxFreq = 0;
char maxChar = 0;
for (char c : str.toCharArray()) {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
if (freqMap.get(c) > maxFreq) {
maxFreq = freqMap.get(c);
maxChar = c;
}
}
System.out.println(maxChar + " " + maxFreq);
}
}
示例输入: "abbcccddddeeeee"
示例输出: e 5
import java.util.HashMap;
import java.util.Map;
public class MaxCharFrequency {
public static void main(String[] args) {
String str = "abbcccddddeeeee";
Map<Character, Integer> freqMap = new HashMap<>();
int maxFreq = 0;
char maxChar = 0;
for (char c : str.toCharArray()) {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
if (freqMap.get(c) > maxFreq) {
maxFreq = freqMap.get(c);
maxChar = c;
}
}
System.out.println(maxChar + " " + maxFreq);
}
}
展开
评论
点赞
#每日一题# 给定一个整数数组 nums,找到其中两个数满足它们的和等于一个给定的目标值 target,并返回这两个数的索引。假设每个输入只有一个解决方案,并且你不能重复使用相同的元素。
public class TwoSum {
public int[] twoSum(int[] nums, int target) {
for(int i=0; i<nums.length; i++) {
for(int j=i+1; j<nums.length; j++) {
if(nums[i] + nums[j] == target) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
TwoSum ts = new TwoSum();
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = ts.twoSum(nums, target);
System.out.println(result[0] + " " + result[1]);
}
}
public class TwoSum {
public int[] twoSum(int[] nums, int target) {
for(int i=0; i<nums.length; i++) {
for(int j=i+1; j<nums.length; j++) {
if(nums[i] + nums[j] == target) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
TwoSum ts = new TwoSum();
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = ts.twoSum(nums, target);
System.out.println(result[0] + " " + result[1]);
}
}
展开
评论
点赞
#每日一题# 实现一个函数,输入一个整数数组,输出该数组中的最大值和最小值。
public class MaxAndMin {
public static void main(String[] args) {
int[] nums = {5, 8, 1, 4, 2, 9, 3, 7, 6};
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
if (nums[i] < min) {
min = nums[i];
}
}
System.out.println("Max value: " + max);
System.out.println("Min value: " + min);
}
}
输出结果:
mathematica
Copy code
Max value: 9
Min value: 1
public class MaxAndMin {
public static void main(String[] args) {
int[] nums = {5, 8, 1, 4, 2, 9, 3, 7, 6};
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
if (nums[i] < min) {
min = nums[i];
}
}
System.out.println("Max value: " + max);
System.out.println("Min value: " + min);
}
}
输出结果:
mathematica
Copy code
Max value: 9
Min value: 1
展开
评论
点赞