AC16. 替换空格
AC32. 调整数组顺序使奇数位于偶数前面
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i ++) {
System.out.print(arr[i]);
System.out.print(" ");
}
System.out.println();
reOrderArray(arr);
for (int i = 0; i < arr.length; i ++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
public static void reOrderArray(int [] array) {
if (array == null || array.length == 1) return ;
int i = 0, j = array.length - 1;
while (i <= j) {
while (i <= j && array[i] % 2 == 1) i ++;
while (i <= j && array[j] % 2 == 0) j --;
if (i < j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
AC61. 最长不含重复字符的子字符串
import java.util.*;
public class Main {
public static void main (String[] args) {
String s = "abcabcbb";
String s = "pwwkew";
int res = lengthOfLongestSubstring(s);
System.out.println(res);
}
public static int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> hash = new HashMap<>();
int res = 0;
for (int j = 0, i = 0; i < s.length(); i ++) {
hash.put(s.charAt(i), hash.getOrDefault(s.charAt(i), 0) + 1);
while (hash.get(s.charAt(i)) > 1) {
hash.put(s.charAt(j), hash.get(s.charAt(j)) - 1);
j ++;
}
res = Math.max(res, i - j + 1);
}
return res;
}
}
AC76. 和为S的连续正数序列
import java.util.*;
public class Main {
public static void main (String[] args) {
int sum = 15;
List<List<Integer>> res = findContinuousSequence(sum);
for (List<Integer> r : res) {
for (int x : r) {
System.out.print(x);
System.out.print(" ");
}
System.out.println();
}
}
public static List<List<Integer> > findContinuousSequence(int sum) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 1, j = 1, s = 1; i <= sum; i ++) {
while (s < sum) s += ++j;
if (s == sum && j - i + 1 > 1) {
List<Integer> line = new ArrayList<>();
for (int k = i; k <= j; k ++) line.add(k);
res.add(line);
}
s -= i;
}
return res;
}
}
AC77. 翻转单词顺序
LC3. 无重复字符的最长子串
LC5. 最长回文子串
LC15. 三数之和
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> res = threeSum(nums);
for (List<Integer> r : res) {
for (int x : r) {
System.out.print(x);
System.out.print(" ");
}
System.out.println();
}
}
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length < 3) return res;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i ++) {
int j = i + 1, k = nums.length - 1;
if (i != 0 && nums[i] == nums[i - 1]) continue;
if (nums[i] + nums[i + 1] + nums[i + 2] > 0) break;
if (nums[i] + nums[k - 1] + nums[k] < 0) continue;
while (j < k) {
int s = nums[i] + nums[j] + nums[k];
if (s > 0) {
do {k --;} while (j < k && nums[k] == nums[k + 1]);
}
else if (s < 0) {
j ++;
while (j < k && nums[j] == nums[j - 1]) j ++;
}
else {
res.add(Arrays.asList(nums[i], nums[j], nums[k]));
j ++; k --;
while (j < k && nums[k] == nums[k + 1]) k --;
while (j < k && nums[j] == nums[j - 1]) j ++;
}
}
}
return res;
}
}
LC16. 最接近的三数之和
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {-1, 2, 1, 4};
int target = 1;
int res = threeSumClosest(nums, target);
System.out.println(res);
}
public static int threeSumClosest(int[] nums, int target) {
int ans = nums[0] + nums[1] + nums[2];
Arrays.sort(nums);
for (int i = 0; i < nums.length; i ++){
int l = i + 1, r = nums.length - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (Math.abs(sum - target) < Math.abs(ans - target))
ans= sum;
if (sum == target) return target;
else if (sum < target) l ++;
else r --;
}
}
return ans;
}
}
LC18. 四数之和
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {1, 0, -1, 0, -2, 2};
int target = 0;
List<List<Integer>> res = fourSum(nums, target);
for (List<Integer> r : res) {
for (int x : r) {
System.out.print(x);
System.out.print(" ");
}
System.out.println();
}
}
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for (int i = 0; i < n; i ++) {
while (i > 0 && i < n && nums[i] == nums[i - 1]) i ++;
for (int j = i + 1; j < n; j ++) {
while (j != i + 1 && j < n && nums[j] == nums[j - 1]) j ++;
int l = j + 1, r = n - 1;
while (l < r) {
int sum = nums[i] + nums[j] + nums[l] + nums[r];
if (sum == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
do {l ++;} while (l < r && nums[l - 1] == nums[l]);
do {r --;} while (l < r && nums[r] == nums[r + 1]);
}
else if (sum < target) {
do {l ++;} while (l < r && nums[l - 1] == nums[l]);
}
else {
do {r --;} while (l < r && nums[r] == nums[r + 1]);
}
}
}
}
return res;
}
}
LC26. 删除排序数组中的重复项
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {1, 1, 2};
int res = removeDuplicates(nums);
System.out.println(res);
}
public static int removeDuplicates(int[] nums) {
int k = 0;
for (int i = 0; i < nums.length; i ++)
if (i == 0 || nums[i] != nums[i - 1 ]) nums[k ++] = nums[i];
return k;
}
}
LC27. 移除元素
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {3, 2, 2, 3};
int val = 3;
int res = removeElement(nums, val);
System.out.println(res);
}
public static int removeElement(int[] nums, int val) {
int k = 0;
for (int i = 0; i < nums.length; i ++)
if (nums[i] != val) nums[k ++] = nums[i];
return k;
}
}
LC58. 最后一个单词的长度
import java.util.*;
public class Main {
public static void main (String[] args) {
String s = "Hello World";
int res = lengthOfLastWord(s);
System.out.println(res);
}
public static int lengthOfLastWord(String s) {
for (int i = s.length() - 1; i >= 0; i --) {
if (s.charAt(i) == ' ') continue;
int j = i - 1;
while (j >= 0 && s.charAt(j) != ' ')j --;
return i - j;
}
return 0;
}
}
LC76. 最小覆盖子串
LC125. 验证回文串
LC167. 两数之和 II - 输入有序数组
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] res = twoSum(nums, target);
for (int i = 0; i < res.length; i ++)
System.out.println(res[i]);
}
public static int[] twoSum(int[] numbers, int target) {
for (int i = 0, j = numbers.length - 1; i < j; i --) {
while (i < j && numbers[i] + numbers[j] > target) j --;
while (i < j && numbers[i] + numbers[j] == target) return new int[]{i + 1, j + 1};
}
return new int[]{};
}
}
209. 长度最小的子数组
import java.util.*;
public class Main {
public static void main (String[] args) {
int s = 7;
int[] nums = {2,3,1,2,4,3};
int res = minSubArrayLen(s, nums);
System.out.println(res);
}
public static int minSubArrayLen(int s, int[] nums) {
int res = Integer.MAX_VALUE;
for (int i = 0, j = 0, sum = 0; i < nums.length; i ++) {
sum += nums[i];
while (sum - nums[j] >= s) sum -= nums[j ++];
if (sum >= s) res = Math.min(res, i - j + 1);
}
if (res == Integer.MAX_VALUE) res = 0;
return res;
}
}
228. 汇总区间
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] nums = {0,1,2,4,5,7};
List<String> res = summaryRanges(nums);
for (String r : res) {
System.out.println(r);
}
}
public static List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
for (int i = 0; i < nums.length; i ++) {
int j = i + 1;
while (j < nums.length && nums[j] == nums[j- 1] + 1) j ++;
if (j == i + 1) res.add(nums[i] + "");
else res.add(nums[i] + "->" + nums[j - 1]);
i = j - 1;
}
return res;
}
}