提交 0f29f942 编写于 作者: A AbelEthan

1.添加771題案例代碼,以及测试案例

2.添加844題案例代碼,以及测试案例
3.添加1431題案例代碼,以及测试案例
4.添加1470題案例代碼,以及测试案例
5.添加1480題案例代碼,以及测试案例
6.添加1512題案例代碼,以及测试案例
7.添加1603題案例代碼,以及测试案例
上级 1920f4fd
package com.aen.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午3:32
*/
public class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max = 0;
int len = candies.length;
List<Boolean> candyList = new ArrayList<>();
for (int i = 0; i < len; ++i) {
max = Math.max(max, candies[i]);
}
for (int i = 0; i < len; ++i) {
candyList.add(candies[i] + extraCandies >= max);
}
return candyList;
}
}
package com.aen.leetcode;
import java.util.List;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午3:39
*/
public class SolutionTest {
public static void main(String[] args) {
Solution solution = new Solution();
List<Boolean> booleanList = solution.kidsWithCandies(new int[]{2, 3, 5, 1, 3}, 3);
System.out.println(booleanList);
}
}
package com.aen.leetcode;
/**
* Title: {@link Solution}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午3:06
*/
public class Solution {
public int[] shuffle(int[] nums, int n) {
int len = nums.length;
int[] ints = new int[len];
int j = 0;
for (int i = 0; i < n; ++i) {
ints[j] = nums[i];
ints[j + 1] = nums[i + n];
j += 2;
}
return ints;
}
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午3:30
*/
public class SolutionTest {
public static void main(String[] args) {
int[] s = new int[]{2, 5, 1, 3, 4, 7};
Solution solution = new Solution();
int[] shuffle = solution.shuffle(s, 3);
for (int i : shuffle) {
System.out.println(i);
}
}
}
package com.aen.leetcode;
/**
* Title: {@link Solution}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午2:48
*/
public class Solution {
public int[] runningSum(int[] nums) {
int len = nums.length;
if (len <= 1){
return nums;
}
for (int i = 1; i < len; ++i) {
nums[i] += nums[i -1];
}
return nums;
}
}
package com.aen.leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午4:02
*/
public class Solution {
public int numIdenticalPairsOne(int[] nums) {
int len = nums.length;
int count = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len; ++i) {
Integer integer = map.getOrDefault(nums[i], 0);
count += integer;
map.put(nums[i], integer + 1);
}
return count;
}
public int numIdenticalPairsTwo(int[] nums) {
int len = nums.length;
int count = 0;
for (int i = 0; i < len; ++i) {
for (int j = i + 1; j < len; ++j) {
if (nums[i] == nums[j]) {
++count;
}
}
}
return count;
}
public int numIdenticalPairsThree(int[] nums) {
int len = nums.length;
int count = 0;
int[] countArr = new int[100];
for (int i = 0; i < len; ++i) {
count += countArr[nums[i]];
++countArr[nums[i]];
}
return count;
}
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午4:02
*/
public class SolutionTest {
public static void main(String[] args) {
Solution solution = new Solution();
int i = solution.numIdenticalPairsThree(new int[]{1,1,1,1});
System.out.println(i);
}
}
package com.aen.leetcode;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午4:48
*/
public class ParkingSystem {
private Map<Integer, Integer> map = new HashMap<>();
private int big;
private int medium;
private int small;
public ParkingSystem(int big, int medium, int small) {
// map.put(1, big);
// map.put(2, medium);
// map.put(3, small);
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
switch (carType) {
case 1:
if (big > 0) {
--big;
return true;
}
break;
case 2:
if (medium > 0) {
--medium;
return true;
}
break;
case 3:
if (small > 0) {
--small;
return true;
}
break;
}
return false;
}
// public boolean addCar(int carType) {
// Integer integer = map.get(carType);
// if (integer > 0) {
// map.put(carType, integer-1);
// return true;
// }
// return false;
// }
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午4:53
*/
public class ParkingSystemTest {
public static void main(String[] args) {
ParkingSystem parkingSystem = new ParkingSystem(1,1,0);
System.out.println(parkingSystem.addCar(1));
System.out.println(parkingSystem.addCar(2));
System.out.println(parkingSystem.addCar(3));
System.out.println(parkingSystem.addCar(1));
}
}
package com.aen.leetcode;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午5:08
*/
public class Solution {
private Map<Character, Integer> map = new HashMap<>();
public int numJewelsInStones(String J, String S) {
int slen = S.length();
int jlen = J.length();
int count = 0;
for (int i = 0; i < slen; ++i) {
char s = S.charAt(i);
for (int j = 0; j < jlen; ++j) {
if (s == J.charAt(j)) {
count++;
}
}
}
return count;
}
public void getMap(String J) {
int len = J.length();
for (int i = 0; i < len; ++i) {
map.put(J.charAt(i), 1);
}
}
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午5:39
*/
public class SolutionTest {
public static void main(String[] args) {
Solution solution = new Solution();
int i = solution.numJewelsInStones("aA", "aAAbbbb");
System.out.println(i);
}
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午2:03
*/
public class Solution {
public boolean backspaceCompare(String S, String T) {
return compare(S).equals(compare(T));
}
public String compare(String s) {
int len = s.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
if (s.charAt(i) != '#') {
sb.append(s.charAt(i));
} else {
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
}
}
return sb.toString();
}
}
package com.aen.leetcode;
/**
* Title: {@link com.aen.leetcode}
* Description:
*
* @author 谭 tmn
* @email AbelEthan@126.com
* @date 20-10-19 下午2:44
*/
public class SolutionTest {
public static void main(String[] args) {
// 输入:S = "ab#c", T = "ad#c"
// 输出:true
// 解释:S 和 T 都会变成 “ac”。
//
// 输入:S = "ab##", T = "c#d#"
// 输出:true
// 解释:S 和 T 都会变成 “”。
//
// 输入:S = "a##c", T = "#a#c"
// 输出:true
// 解释:S 和 T 都会变成 “c”。
//
// 输入:S = "a#c", T = "b"
// 输出:false
// 解释:S 会变成 “c”,但 T 仍然是 “b”。
Solution solution = new Solution();
System.out.println(solution.backspaceCompare("ab#c", "ad#c"));
System.out.println(solution.backspaceCompare("ab##", "c#d#"));
System.out.println(solution.backspaceCompare("a##c", "#a#c"));
System.out.println(solution.backspaceCompare("a#c", "b"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册