加油冲
This commit is contained in:
parent
39cb705c71
commit
a01e34bcfb
|
@ -0,0 +1,36 @@
|
||||||
|
package com.zerroi.leetcode.Three11;
|
||||||
|
|
||||||
|
public class LengthOfLastWord {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionSecond solutionSecond = new SolutionSecond();
|
||||||
|
// int res = solutionSecond.lengthOfLastWord(" fly me to the moon ");
|
||||||
|
// int res = solutionSecond.lengthOfLastWord("Hello World");
|
||||||
|
int i = solutionSecond.lengthOfLastWord("a ");
|
||||||
|
System.out.println("i = " + i);
|
||||||
|
// System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。
|
||||||
|
|
||||||
|
单词 是指仅由字母组成、不包含任何空格字符的最大
|
||||||
|
子字符串
|
||||||
|
。*/
|
||||||
|
class SolutionSecond {
|
||||||
|
public int lengthOfLastWord(String s) {
|
||||||
|
int res = 0;
|
||||||
|
boolean flag = false;
|
||||||
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||||||
|
// 从字符串后面开始遍历,找到第一字母出现的位置开始记录
|
||||||
|
if (s.charAt(i) != ' ') {
|
||||||
|
res++;
|
||||||
|
flag = true;
|
||||||
|
} else if (flag) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// System.out.println(s.substring(startIndex, endIndex + 1));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.zerroi.leetcode.Three11;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RomanToInt {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionFirst solutionFirst = new SolutionFirst();
|
||||||
|
int res = solutionFirst.romanToInt("XLVI");
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SolutionFirst {
|
||||||
|
public int romanToInt(String s) {
|
||||||
|
Map<Character, Integer> map = new HashMap<>();
|
||||||
|
map.put('I', 1);
|
||||||
|
map.put('V', 5);
|
||||||
|
map.put('X', 10);
|
||||||
|
map.put('L', 50);
|
||||||
|
map.put('C', 100);
|
||||||
|
map.put('D', 500);
|
||||||
|
map.put('M', 1000);
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (i != s.length() - 1) {
|
||||||
|
if (map.get(s.charAt(i + 1)) > map.get(s.charAt(i))) {
|
||||||
|
res += map.get(s.charAt(i + 1)) - map.get(s.charAt(i));
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
res += map.get(s.charAt(i));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res += map.get(s.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.zerroi.leetcode.Three12;
|
||||||
|
|
||||||
|
public class IsSubsequence {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionFirst solutionFirst = new SolutionFirst();
|
||||||
|
// boolean res = solutionFirst.isSubsequence("abc", "ahbgdc");
|
||||||
|
// boolean res = solutionFirst.isSubsequence("axc", "ahbgdc");
|
||||||
|
// boolean res = solutionFirst.isSubsequence("acb", "ahbgdc");
|
||||||
|
boolean res = solutionFirst.isSubsequence("", "");
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
|
||||||
|
|
||||||
|
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
|
||||||
|
|
||||||
|
进阶:
|
||||||
|
|
||||||
|
如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
|
||||||
|
* */
|
||||||
|
class SolutionFirst {
|
||||||
|
public boolean isSubsequence(String s, String t) {
|
||||||
|
int n1 = s.length();
|
||||||
|
int j = 0;
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < n1; i++) {
|
||||||
|
while (j < t.length()) {
|
||||||
|
if (s.charAt(i) == t.charAt(j)) {
|
||||||
|
count ++;
|
||||||
|
j++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count == s.length();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.zerroi.leetcode.Three12;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
|
||||||
|
|
||||||
|
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
|
||||||
|
|
||||||
|
返回容器可以储存的最大水量。
|
||||||
|
|
||||||
|
说明:你不能倾斜容器。
|
||||||
|
* */
|
||||||
|
public class MaxArea {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new Solution();
|
||||||
|
int res = solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int maxArea(int[] height) {
|
||||||
|
int left = 0;
|
||||||
|
int right = height.length - 1;
|
||||||
|
int res = 0;
|
||||||
|
while (left < right) {
|
||||||
|
int h = Math.min(height[left], height[right]);
|
||||||
|
int width = right - left;
|
||||||
|
res = Math.max(h * width, res);
|
||||||
|
while (left < right && height[left] <= h) {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (left < right && height[right] <= h) {
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.zerroi.leetcode.Three12;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。
|
||||||
|
|
||||||
|
以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2。
|
||||||
|
|
||||||
|
你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。
|
||||||
|
|
||||||
|
你所设计的解决方案必须只使用常量级的额外空间。
|
||||||
|
* */
|
||||||
|
public class TwoSum {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionSecond solutionSecond = new SolutionSecond();
|
||||||
|
int[] res = solutionSecond.twoSum(new int[]{2, 7, 11, 15}, 9);
|
||||||
|
for (int i : res) {
|
||||||
|
System.out.println("i = " + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SolutionSecond {
|
||||||
|
public int[] twoSum(int[] numbers, int target) {
|
||||||
|
for (int i = 0; i < numbers.length; i++) {
|
||||||
|
for (int j = i + 1; j < numbers.length; j++) {
|
||||||
|
if (numbers[i] + numbers[j] == target) {
|
||||||
|
return new int[]{i+1, j+1};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] twoSum(int[] numbers, int target, int abc) {
|
||||||
|
int left = 0;
|
||||||
|
int right = numbers.length - 1;
|
||||||
|
while (left < right) {
|
||||||
|
int sum = numbers[left] + numbers[right];
|
||||||
|
if (sum == target) {
|
||||||
|
return new int[]{left + 1, right + 1};
|
||||||
|
} else if (sum > target) {
|
||||||
|
right--;
|
||||||
|
} else {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.zerroi.leetcode.Three13;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MinSubArrayLen {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionSecond solutionSecond = new SolutionSecond();
|
||||||
|
// int res = solutionSecond.minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3});
|
||||||
|
int res = solutionSecond.minSubArrayLen(11, new int[]{1,2,3,4,5});
|
||||||
|
// int res = solutionSecond.minSubArrayLen(11, new int[]{1, 1, 1, 1, 1, 1, 1, 1});
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
给定一个含有 n 个正整数的数组和一个正整数 target 。
|
||||||
|
|
||||||
|
找出该数组中满足其总和大于等于 target 的长度最小的 连续
|
||||||
|
子数组
|
||||||
|
[numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
|
||||||
|
*/
|
||||||
|
class SolutionSecond {
|
||||||
|
public int minSubArrayLen(int target, int[] nums) {
|
||||||
|
int sum = 0;
|
||||||
|
int minLen = Integer.MAX_VALUE;
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
sum += nums[i];
|
||||||
|
while (sum >= target) {
|
||||||
|
minLen = Math.min(minLen, index - i + 1);
|
||||||
|
sum -= nums[index];
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minLen == Integer.MAX_VALUE ? 0 : minLen;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.zerroi.leetcode.Three13;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ThreeSum {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionFirst solutionFirst = new SolutionFirst();
|
||||||
|
List<List<Integer>> res = solutionFirst.threeSum(new int[]{-1,0,1,2,-1,-4});
|
||||||
|
res.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
|
||||||
|
|
||||||
|
你返回所有和为 0 且不重复的三元组。
|
||||||
|
|
||||||
|
注意:答案中不可以包含重复的三元组。
|
||||||
|
* */
|
||||||
|
class SolutionFirst {
|
||||||
|
public List<List<Integer>> threeSum(int[] nums) {
|
||||||
|
Arrays.sort(nums);
|
||||||
|
List<List<Integer>> res = new ArrayList<>();
|
||||||
|
for (int i = 0; i < nums.length - 1; i++) {
|
||||||
|
if (i > 0 &&nums[i] == nums[i - 1]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int left = i + 1;
|
||||||
|
int right = nums.length - 1;
|
||||||
|
while (left < right ) {
|
||||||
|
int sum = nums[left] + nums[right] + nums[i];
|
||||||
|
if (sum == 0) {
|
||||||
|
res.add(List.of(nums[left], nums[right], nums[i]));
|
||||||
|
while (left < right && nums[left] == nums[left + 1]) {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (left < right && nums[right] == nums[right - 1]) {
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
left++;
|
||||||
|
} else if (sum > 0) {
|
||||||
|
right--;
|
||||||
|
} else {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.zerroi.leetcode.Three14;
|
||||||
|
|
||||||
|
public class LongestCommonPrefix {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionFirst solutionFirst = new SolutionFirst();
|
||||||
|
String res = solutionFirst.longestCommonPrefix(new String[]{"flower", "flow", "flight"});
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SolutionFirst {
|
||||||
|
public String longestCommonPrefix(String[] strs) {
|
||||||
|
if (strs.length == 0) return "";
|
||||||
|
String prefix = strs[0];
|
||||||
|
for (String str : strs) {
|
||||||
|
while (!str.startsWith(prefix)) {
|
||||||
|
if (prefix.length() == 0) return "";
|
||||||
|
prefix = prefix.substring(0, prefix.length() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.zerroi.leetcode.Three14;
|
||||||
|
|
||||||
|
public class ReverseWords {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionSecond solutionSecond = new SolutionSecond();
|
||||||
|
String res = solutionSecond.reverseWords("a good example");
|
||||||
|
System.out.println("res = " + res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SolutionSecond {
|
||||||
|
public String reverseWords(String s) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
int startIndex = 0;
|
||||||
|
int endIndex = s.length() - 1;
|
||||||
|
for (; startIndex < s.length(); startIndex++) {
|
||||||
|
if (s.charAt(startIndex) != ' ') break;
|
||||||
|
}
|
||||||
|
for (; endIndex >= 0; endIndex--) {
|
||||||
|
if (s.charAt(endIndex) != ' ') break;
|
||||||
|
}
|
||||||
|
int cur = endIndex;
|
||||||
|
for (; startIndex <= endIndex; endIndex--) {
|
||||||
|
if (s.charAt(endIndex) == ' ') {
|
||||||
|
if (s.charAt(endIndex + 1) != ' ') {
|
||||||
|
builder.append(s, endIndex + 1, cur + 1).append(" ");
|
||||||
|
}
|
||||||
|
cur = endIndex - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.append(s, startIndex, cur + 1);
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,5 +53,6 @@ class SolutionSecond {
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue