LeetCode Daily

╰半橙微兮° 2022-10-02 10:57 22阅读 0赞

6.18

771.宝石和石头**

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此”a”和”A”是不同类型的石头。

示例 1:

输入: J = “aA”, S = “aAAbbbb”
输出: 3
示例 2:

输入: J = “z”, S = “ZZ”
输出: 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jewels-and-stones

  1. class Solution {
  2. public int numJewelsInStones(String J, String S) {
  3. Map<Character,Integer> jmap=new HashMap<>();
  4. int num=0;
  5. char[] jc=J.toCharArray();
  6. char[] sc=S.toCharArray();
  7. for (int i=0;i<jc.length;i++){
  8. jmap.put(jc[i],i);
  9. }
  10. for (int j=0;j<sc.length;j++){
  11. if (jmap.containsKey(sc[j])){
  12. num++;
  13. }
  14. }
  15. return num;
  16. }
  17. }

6.19

1021. 删除最外层的括号**

有效括号字符串为空 (“”)、”(“ + A + “)” 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。例如,””,”()”,”(())()” 和 “(()(()))” 都是有效的括号字符串。

如果有效字符串 S 非空,且不存在将其拆分为 S = A+B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。

给出一个非空有效字符串 S,考虑将其进行原语化分解,使得:S = P_1 + P_2 + … + P_k,其中 P_i 是有效括号字符串原语。

对 S 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S 。

示例 1:

输入:”(()())(())”
输出:”()()()”
解释:
输入字符串为 “(()())(())”,原语化分解得到 “(()())” + “(())”,
删除每个部分中的最外层括号后得到 “()()” + “()” = “()()()”。
示例 2:

输入:”(()())(())(()(()))”
输出:”()()()()(())”
解释:
输入字符串为 “(()())(())(()(()))”,原语化分解得到 “(()())” + “(())” + “(()(()))”,
删除每隔部分中的最外层括号后得到 “()()” + “()” + “()(())” = “()()()()(())”。
示例 3:

输入:”()()”
输出:””
解释:
输入字符串为 “()()”,原语化分解得到 “()” + “()”,
删除每个部分中的最外层括号后得到 “” + “” = “”。

提示:

S.length <= 10000
S[i] 为 “(” 或 “)”
S 是一个有效括号字符串

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-outermost-parentheses

  1. class Solution {
  2. public static String removeOuterParentheses(String S) {
  3. if (S == null || S == ""){
  4. return null;
  5. }
  6. int saveIndex = 0,mark = 0;
  7. boolean save = false;
  8. char[] chars = S.toCharArray();
  9. char[] res = new char[chars.length];//保存结果的res,第一个和最后一个不需要保存。
  10. for (int i = 0; i < chars.length ; i++) {
  11. if (chars[i] == '('){
  12. mark++;
  13. }
  14. if (chars[i] == ')'){
  15. mark--;
  16. }
  17. if(mark == 0 && i !=0){
  18. save = false; //这个不需要保存,下一个也不需要保存
  19. continue;
  20. }
  21. if(save == true){
  22. //将该保存的保存到结果数组中
  23. res[saveIndex] = chars[i];
  24. saveIndex++;
  25. }
  26. save = true; //表示可以保存,即下一个可能需要保存
  27. }
  28. String result = new String(res);
  29. return result.trim();
  30. }
  31. }

1. Two Sum

原题连接

https://leetcode.com/problems/two-sum
内容描述

  1. Given an array of integers, return indices of the two numbers such that they add up to a specific target.
  2. You may assume that each input would have exactly one solution, and you may not use the same element twice.
  3. Example:
  4. Given nums = [2, 7, 11, 15], target = 9,
  5. Because nums[0] + nums[1] = 2 + 7 = 9,
  6. return [0, 1].

解题方案
HashMap原理
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. Map<Integer, Integer> lookup = new HashMap<>();
  4. int[] res = new int[2];
  5. for (int i = 0; i < nums.length; i++) {
  6. if (lookup.containsKey(target - nums[i])) {
  7. res = new int[] { lookup.get(target - nums[i]), i };
  8. break;
  9. } else {
  10. lookup.put(nums[i], i);
  11. }
  12. }
  13. return res;
  14. }
  15. }

第一种解法

  1. public int[] twoSum(int[] nums,int target){
  2. for(int i = 0; i < nums.length ; i ++){
  3. for(int j = 0; j < nums.length ; j ++){
  4. if(nums[j] == target.nums[i]){
  5. return new int [] {i,j};
  6. }
  7. }
  8. }
  9. }

2nd

  1. public int[] twoSum(int[] ,int target){
  2. Map<Integer,Integer> map = new HashMap<Integer,Integer>();
  3. for(int i = 0; i < nums.length; i ++){
  4. map.put(nums[i],i);
  5. }
  6. for(int i = 0; i < nums.length; i ++){
  7. int complement = target - num[i];
  8. if(map.containsKey(complement) && map.get(complement) != i){
  9. return new int[]{(i,map.get(complement))};
  10. }
  11. }
  12. throw new IllegalArgumentException("No two sum solution")
  13. }

一个HashMap解法

  1. public int[] TwoSum(int[] nums,int target){
  2. Map<Integer,Integer> map = new HashMap<>();
  3. for(int i = 0; i < nums.length ; i ++){
  4. int complement = target - nums[i];
  5. if(map.containsKey(complement)){
  6. return new int{}{map.get(complement),i};
  7. }
  8. map.put(nums[i],i)
  9. }
  10. throw new IllegalArgumentException("No two sum solution")
  11. }
  12. 找出数组中重复的数字
  13. 题目描述
  14. 在一个长度为 n 的数组里的所有数字都在 0 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2, 3, 1, 0, 2, 5, 3},那么对应的输出是重复的数字 2 或者 3
  15. 解法
  16. 解法一
  17. 排序后,顺序扫描,判断是否有重复,时间复杂度为 O(n²)。
  18. 解法二
  19. 利用哈希表,遍历数组,如果哈希表中没有该元素,则存入哈希表中,否则返回重复的元素。时间复杂度为 O(n),空间复杂度为 O(n)。
  20. 解法三
  21. 长度为 n,元素的数值范围也为 n,如果没有重复元素,那么数组每个下标对应的值与下标相等。
  22. 从头到尾遍历数组,当扫描到下标 i 的数字 nums[i]:
  23. 如果等于 i,继续向下扫描;
  24. 如果不等于 i,拿它与第 nums[i] 个数进行比较,如果相等,说明有重复值,返回 nums[i]。如果不相等,就把第 i 个数 和第 nums[i] 个数交换。重复这个比较交换的过程。
  25. 此算法时间复杂度为 O(n),因为每个元素最多只要两次交换,就能确定位置。空间复杂度为 O(1)。
  26. /**
  27. * @author bingo
  28. * @since 2018/10/27
  29. */
  30. public class Solution {
  31. /**
  32. * 查找数组中的重复元素
  33. * @param numbers 数组
  34. * @param length 数组长度
  35. * @param duplication duplication[0]存储重复元素
  36. * @return boolean
  37. */
  38. public boolean duplicate(int[] numbers, int length, int[] duplication) {
  39. if (numbers == null || length < 1) {
  40. return false;
  41. }
  42. for (int e : numbers) {
  43. if (e >= length) {
  44. return false;
  45. }
  46. }
  47. for (int i = 0; i < length; ++i) {
  48. while (numbers[i] != i) {
  49. if (numbers[i] == numbers[numbers[i]]) {
  50. duplication[0] = numbers[i];
  51. return true;
  52. }
  53. swap(numbers, i, numbers[i]);
  54. }
  55. }
  56. return false;
  57. }
  58. private void swap(int[] numbers, int i, int j) {
  59. int t = numbers[i];
  60. numbers[i] = numbers[j];
  61. numbers[j] = t;
  62. }
  63. }
  64. 测试用例
  65. 长度为 n 的数组中包含一个或多个重复的数字;
  66. 数组中不包含重复的数字;
  67. 无效测试输入用例(输入空指针;长度为 n 的数组中包含 0~n-1 之外的数字)。

发表评论

表情:
评论列表 (有 0 条评论,22人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode Daily

    6.18 771.宝石和石头\\ 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中

    相关 Daily Scrum 11.1

    今天我们主要讨论了一下几个问题: 1.我们的用户交互模式是否需要改变。因为上次邹欣老师提到我们不能过分限制用户的行为,我们我们重新分析了一下我们产品的用户交互模式。简短的讨论