diff --git "a/C\350\257\255\350\250\200\347\275\221/_1.java" "b/C\350\257\255\350\250\200\347\275\221/_1.java" new file mode 100644 index 0000000..064e56b --- /dev/null +++ "b/C\350\257\255\350\250\200\347\275\221/_1.java" @@ -0,0 +1,14 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + while (scanner.hasNext()) { + int a = scanner.nextInt(); + int b = scanner.nextInt(); + while + int c = a + b; + System.out.println(c); + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 7fba805..f5495d8 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # lettcode- + +题目可能不会经常发布但是不代表我没有做啊! +现在会定时发布一些解题技巧和方法,做到不知学算法还要学框架。 +发布时间一个星期一篇算法解题技巧,没有做到就没有做到吧...... \ No newline at end of file diff --git a/lettcode/ListNode.java b/lettcode/ListNode.java new file mode 100644 index 0000000..7c29345 --- /dev/null +++ b/lettcode/ListNode.java @@ -0,0 +1,20 @@ +public class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode num = new ListNode(); + + for(int i = 0 ; i < l1.val ; i++){ + for(int j = 0 ; j < l2.val ; j++){ + // num.val = l1[i].val + l2[j].val; + } + } + return num; + } + } +} + diff --git "a/lettcode/_1035_\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.java" "b/lettcode/_1035_\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.java" new file mode 100644 index 0000000..9eb6e60 --- /dev/null +++ "b/lettcode/_1035_\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.java" @@ -0,0 +1,20 @@ +//https://leetcode-cn.com/problems/uncrossed-lines/ +//_1035_不相交的线 +class Solution { + public int maxUncrossedLines(int[] nums1, int[] nums2) { + int m = nums1.length, n = nums2.length; + int[][] dp = new int[m + 1][n + 1]; + for (int i = 1; i <= m; i++) { + int num1 = nums1[i - 1]; + for (int j = 1; j <= n; j++) { + int num2 = nums2[j - 1]; + if (num1 == num2) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[m][n]; + } +} \ No newline at end of file diff --git "a/lettcode/_1269_\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.java" "b/lettcode/_1269_\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.java" new file mode 100644 index 0000000..86c4893 --- /dev/null +++ "b/lettcode/_1269_\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.java" @@ -0,0 +1,23 @@ +//https://leetcode-cn.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/submissions/ +//_1269_停在原地的方案数 +class Solution { + int mod = (int)1e9+7; + //强制转换int + public int numWays(int steps, int len) { + int max = Math.min(steps / 2, len - 1); + int[][] f = new int[steps + 1][max + 1]; + f[steps][0] = 1; +//由「原地」操作到达当前状态,消耗一次操作,此时由状态 f[i + 1][j]f[i+1][j] 转移而来 +//由「向左」操作到达当前状态,消耗一次操作,此时由状态 f[i + 1][j + 1]f[i+1][j+1] 转移而来 +//由「向右」操作到达当前状态,消耗一次操作,此时由状态 f[i + 1][j - 1]f[i+1][j−1] 转移而来。 + for (int i = steps - 1; i >= 0; i--) { + for (int j = 0; j <= max; j++) { + f[i][j] = (f[i][j] + f[i + 1][j]) % mod; + if (j - 1 >= 0) f[i][j] = (f[i][j] + f[i + 1][j - 1]) % mod; + if (j + 1 <= max) f[i][j] = (f[i][j] + f[i + 1][j + 1]) % mod; + } + } +//起始位置为 00,操作次数为 stepstep,即有初始化条件 f[step][0] = 1f[step][0]=1,f[0][0]f[0][0] 则是我们的最终答案 + return f[0][0]; + } +} \ No newline at end of file diff --git "a/lettcode/_1310_\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.java" "b/lettcode/_1310_\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.java" new file mode 100644 index 0000000..c8bd101 --- /dev/null +++ "b/lettcode/_1310_\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.java" @@ -0,0 +1,16 @@ +//https://leetcode-cn.com/problems/xor-queries-of-a-subarray/submissions/ +//_1310_子数组异或查询 +class Solution { + public int[] xorQueries(int[] arr, int[][] queries) { + int[] xors = new int[arr.length + 1]; + for (int i = 0; i < arr.length; i++) { + xors[i + 1] = xors[i] ^ arr[i]; + } + + int[] ans = new int[queries.length]; + for (int i = 0; i < queries.length; i++) { + ans[i] = xors[queries[i][0]] ^ xors[queries[i][1] + 1]; + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_137_\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.java" "b/lettcode/_137_\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.java" new file mode 100644 index 0000000..69094d3 --- /dev/null +++ "b/lettcode/_137_\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.java" @@ -0,0 +1,18 @@ +//https://leetcode-cn.com/problems/single-number-ii/ +//137. 只出现一次的数字 II + +class Solution { + public int singleNumber(int[] nums) { + int ans = 0; + for (int i = 0; i < 32; ++i) { + int total = 0; + for (int num: nums) { + total += ((num >> i) & 1); + } + if (total % 3 != 0) { + ans |= (1 << i); + } + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_1442_\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.java" "b/lettcode/_1442_\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.java" new file mode 100644 index 0000000..2014e13 --- /dev/null +++ "b/lettcode/_1442_\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.java" @@ -0,0 +1,17 @@ +//https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/ +//_1442_形成两个异或相等数组的三元组数目 +class Solution { + public int countTriplets(int[] arr) { + int ans = 0; + for(int i = 0; i < arr.length - 1; i++){ + int sum = 0; + for(int k = i; k < arr.length; k++){ + sum ^= arr[k]; + if(sum == 0 && k > i){ + ans += (k - i); + } + } + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_1482_\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.java" "b/lettcode/_1482_\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.java" new file mode 100644 index 0000000..dfb6409 --- /dev/null +++ "b/lettcode/_1482_\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.java" @@ -0,0 +1,42 @@ +//https://leetcode-cn.com/problems/minimum-number-of-days-to-make-m-bouquets/ +//_1482_制作m束花所需的最少天数 +class Solution { + public int minDays(int[] bloomDay, int m, int k) { + if (m > bloomDay.length / k) { + return -1; + } + int low = Integer.MAX_VALUE, high = 0; + int length = bloomDay.length; + for (int i = 0; i < length; i++) { + low = Math.min(low, bloomDay[i]); + high = Math.max(high, bloomDay[i]); + } + while (low < high) { + int days = (high - low) / 2 + low; + if (canMake(bloomDay, days, m, k)) { + high = days; + } else { + low = days + 1; + } + } + return low; + } + + public boolean canMake(int[] bloomDay, int days, int m, int k) { + int bouquets = 0; + int flowers = 0; + int length = bloomDay.length; + for (int i = 0; i < length && bouquets < m; i++) { + if (bloomDay[i] <= days) { + flowers++; + if (flowers == k) { + bouquets++; + flowers = 0; + } + } else { + flowers = 0; + } + } + return bouquets >= m; + } +} \ No newline at end of file diff --git "a/lettcode/_15_\344\270\211\346\225\260\344\271\213\345\222\214.java" "b/lettcode/_15_\344\270\211\346\225\260\344\271\213\345\222\214.java" new file mode 100644 index 0000000..77f06ee --- /dev/null +++ "b/lettcode/_15_\344\270\211\346\225\260\344\271\213\345\222\214.java" @@ -0,0 +1,43 @@ +//15. 三数之和 +//https://leetcode-cn.com/problems/3sum/ +class Solution { + public List> threeSum(int[] nums) { + int n = nums.length; + Arrays.sort(nums); + List> ans = new ArrayList>(); + // 枚举 a + for (int first = 0; first < n; ++first) { + // 需要和上一次枚举的数不相同 + if (first > 0 && nums[first] == nums[first - 1]) { + continue; + } + // c 对应的指针初始指向数组的最右端 + int third = n - 1; + int target = -nums[first]; + // 枚举 b + for (int second = first + 1; second < n; ++second) { + // 需要和上一次枚举的数不相同 + if (second > first + 1 && nums[second] == nums[second - 1]) { + continue; + } + // 需要保证 b 的指针在 c 的指针的左侧 + while (second < third && nums[second] + nums[third] > target) { + --third; + } + // 如果指针重合,随着 b 后续的增加 + // 就不会有满足 a+b+c=0 并且 b list = new ArrayList(); + list.add(nums[first]); + list.add(nums[second]); + list.add(nums[third]); + ans.add(list); + } + } + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_1738_\346\211\276\345\207\272\347\254\254 K \345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.java" "b/lettcode/_1738_\346\211\276\345\207\272\347\254\254 K \345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.java" new file mode 100644 index 0000000..2dbede8 --- /dev/null +++ "b/lettcode/_1738_\346\211\276\345\207\272\347\254\254 K \345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.java" @@ -0,0 +1,23 @@ +//https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/ +//_1738_找出第 K 大的异或坐标值 +class Solution { + public int kthLargestValue(int[][] matrix, int k) { + int m=matrix.length; + int n=matrix[0].length; + int[][] temp = new int[m + 1][n + 1]; + + PriorityQueue queue = new PriorityQueue<>(Comparator.reverseOrder()); + for (int i=1;i<=m;i++){ + for (int j=1;j<=n;j++){ + temp[i][j]=temp[i-1][j] ^ temp[i][j-1] ^ temp[i-1][j-1] ^ matrix[i-1][j-1]; + queue.offer(temp[i][j]); + } + } + + for (int i=0;i +#include +/** +https://leetcode-cn.com/problems/two-sum/ + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + for (int i = 0; i < numsSize; ++i) { + for (int j = i + 1; j < numsSize; ++j) { + if (nums[i] + nums[j] == target) { + int* ret = (int *)malloc(sizeof(int) * 2); + ret[0] = i, ret[1] = j; + *returnSize = 2; + return ret; + } + } + } + *returnSize = 0; + return NULL; +} \ No newline at end of file diff --git "a/lettcode/_22_\346\213\254\345\217\267\347\224\237\346\210\220.java" "b/lettcode/_22_\346\213\254\345\217\267\347\224\237\346\210\220.java" new file mode 100644 index 0000000..770710b --- /dev/null +++ "b/lettcode/_22_\346\213\254\345\217\267\347\224\237\346\210\220.java" @@ -0,0 +1,30 @@ +//https://leetcode-cn.com/problems/generate-parentheses/ +//22. 括号生成 +class Solution { + List res = new ArrayList<>(); + public List generateParenthesis(int n) { + if(n<=0){ + return res; + } + // 3 + getParenthesis("",n,n); + return res; + } + public void getParenthesis(String str,int left, int right) { + if(left == 0 && right == 0 ){ + res.add(str); + return; + } + + if(left == right){ + //剩余左右括号数相等,下一个只能用左括号 + getParenthesis(str+"(",left-1,right); + }else if(left < right){ + //剩余左括号小于右括号,下一个可以用左括号也可以用右括号 + if(left > 0){ + getParenthesis(str+"(",left-1,right); + } + getParenthesis(str+")",left,right-1); + } + } +} \ No newline at end of file diff --git "a/lettcode/_26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" "b/lettcode/_26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" new file mode 100644 index 0000000..c5157fd --- /dev/null +++ "b/lettcode/_26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" @@ -0,0 +1,39 @@ +#include +#include +//26. 删除有序数组中的重复项 +//链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/?discussion=hpUNXD +/**int removeDuplicates(int* nums, int numsSize){ + int j = numsSize; + if(j > 1){ + j = 1; + for(int i = 1; i < numsSize; i ++){ + if(nums[i] == nums[i - 1]){ + continue; + } else{ + nums[j] = nums[i]; + j++; + } + } + } + return j; +}*/ +/* +输入 +[1,1,2] +输出 +[1,2] +预期结果 +[1,2]*/ +int removeDuplicates(int* nums,int numsSize) +{int j = numsSize; +j=1; +for(int i=1;i= 0; j--){ + int diff = stones[i] - stones[j]; + if(diff > i){ + break; + } + //对于 石头 j ,它需要跳 diff 步 + if(dp[j][diff]){ + dp[i][diff - 1] = true; + dp[i][diff] = true; + dp[i][diff + 1] = true; + flag = true; + } + } + //当到达了终点 而 flag ,表示无法从前面的任意石头跳到终点,返回 false + if(i == len - 1 && !flag){ + return false; + } + } + return true; + + } +} \ No newline at end of file diff --git "a/lettcode/_43_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.java" "b/lettcode/_43_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.java" new file mode 100644 index 0000000..f5cb9f2 --- /dev/null +++ "b/lettcode/_43_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.java" @@ -0,0 +1,47 @@ +//43. 字符串相乘 +//https://leetcode-cn.com/problems/multiply-strings/ +class Solution { + public String multiply(String num1, String num2) { + if (num1.equals("0") || num2.equals("0")) { + return "0"; + } + + String ans = "0"; + int m = num1.length(), n = num2.length(); + for (int i = n - 1; i >= 0; i--) { + StringBuffer curr = new StringBuffer(); + int add = 0; + for (int j = n - 1; j > i; j--) { + curr.append(0); + } + int y = num2.charAt(i) - '0'; + for (int j = m - 1; j >= 0; j--) { + int x = num1.charAt(j) - '0'; + int product = x * y + add; + curr.append(product % 10); + add = product / 10; + } + if (add != 0) { + curr.append(add % 10); + } + ans = addStrings(ans, curr.reverse().toString()); + } + return ans; + } + + public String addStrings(String num1, String num2) { + int i = num1.length() - 1, j = num2.length() - 1, add = 0; + StringBuffer ans = new StringBuffer(); + while (i >= 0 || j >= 0 || add != 0) { + int x = i >= 0 ? num1.charAt(i) - '0' : 0; + int y = j >= 0 ? num2.charAt(j) - '0' : 0; + int result = x + y + add; + ans.append(result % 10); + add = result / 10; + i--; + j--; + } + ans.reverse(); + return ans.toString(); + } +} \ No newline at end of file diff --git a/lettcode/_50_Pow.java b/lettcode/_50_Pow.java new file mode 100644 index 0000000..33cbbb5 --- /dev/null +++ b/lettcode/_50_Pow.java @@ -0,0 +1,19 @@ +//https://leetcode-cn.com/problems/powx-n/submissions/ +//_50_Pow +class Solution { + public double myPow(double x, int n) { + if(x==1) return 1; + if(x==0) return 0; + if(n==0) return 1; + if(n==Integer.MIN_VALUE) return 1/myPow(x,Integer.MAX_VALUE)*x; + if(n<0) return 1/myPow(x,-n); + double a = x; + double ans = 1; + while(n>0){ + if((n&1)==1) ans *= a; + n = n>>1; + a = a*a; + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227_2.java" "b/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227_2.java" new file mode 100644 index 0000000..f88bdd1 --- /dev/null +++ "b/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227_2.java" @@ -0,0 +1,31 @@ +//https://leetcode-cn.com/problems/longest-palindromic-subsequence/ +//_516_最长回文子序列 +class Solution { +public: + int longestPalindromeSubseq(string s) { + int n = s.size(); + // dp 数组全部初始化为 0 + vector> dp(n, vector(n, 0)); + // base case + for (int i = 0; i < n; i++) + dp[i][i] = 1; + // 反着遍历保证正确的状态转移 + for (int i = n - 1; i >= 0; i--) { + for (int j = i + 1; j < n; j++) { + // 状态转移方程 + if (s[i] == s[j]) + dp[i][j] = dp[i + 1][j - 1] + 2; + else + dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); + } + } + // 整个 s 的最长回文子串长度 + return dp[0][n - 1]; +} +}; +class Solution { + public int longestPalindromeSubseq(String s) { + int n = s.length(); + vector + } +} \ No newline at end of file diff --git "a/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\227\347\254\246.java" "b/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\227\347\254\246.java" new file mode 100644 index 0000000..d9cb522 --- /dev/null +++ "b/lettcode/_516_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\227\347\254\246.java" @@ -0,0 +1,19 @@ +//https://leetcode-cn.com/problems/longest-palindromic-subsequence/ +//_516_最长回文字符 +class Solution { + public int longestPalindromeSubseq(String s) { + int n = s.length(); + int[][] f = new int[n][n]; + for (int i = n - 1; i >= 0; i--) { + f[i][i] = 1; + for (int j = i + 1; j < n; j++) { + if (s.charAt(i) == s.charAt(j)) { + f[i][j] = f[i + 1][j - 1] + 2; + } else { + f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]); + } + } + } + return f[0][n - 1]; + } +} diff --git "a/lettcode/_554_\347\240\226\345\242\231.java" "b/lettcode/_554_\347\240\226\345\242\231.java" new file mode 100644 index 0000000..b9fdfa7 --- /dev/null +++ "b/lettcode/_554_\347\240\226\345\242\231.java" @@ -0,0 +1,20 @@ +//https://leetcode-cn.com/problems/brick-wall/ +//_554_砖墙 +class Solution { + public int leastBricks(List> wall) { + Map cnt = new HashMap(); + for (List widths : wall) { + int n = widths.size(); + int sum = 0; + for (int i = 0; i < n - 1; i++) { + sum += widths.get(i); + cnt.put(sum, cnt.getOrDefault(sum, 0) + 1); + } + } + int maxCnt = 0; + for (Map.Entry entry : cnt.entrySet()) { + maxCnt = Math.max(maxCnt, entry.getValue()); + } + return wall.size() - maxCnt; + } +} \ No newline at end of file diff --git "a/lettcode/_5730_\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.java" "b/lettcode/_5730_\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.java" new file mode 100644 index 0000000..f30242c --- /dev/null +++ "b/lettcode/_5730_\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.java" @@ -0,0 +1,30 @@ +//https://leetcode-cn.com/problems/replace-all-digits-with-characters/ +//5730. 将所有数字用字符替换 +class Solution { +public: + string replaceDigits(string s) { + for (int i = 0; i < s.size(); i += 2) + if (i + 1 < s.size()) + s[i + 1] += s[i] - '0'; + return s; + } +}; + + +class Solution { + public String replaceDigits(String s) { + char[] a = s.toCharArray(); + for(int i = 1; i < s.length(); i += 2) + //偶数 + //字符串 - 数字 + 数字 + a[i] = (char)(a[i - 1] + a[i] - '0'); + return new String(a); + //a[1] + // a[1] = (char)('s' + '1' - '0'); + // a[3] = (char)('s' + '1' - '0'); + // a[5] = (char)('s' + '2' - '0'); + + //a[3] + + } +} \ No newline at end of file diff --git "a/lettcode/_5731_\345\272\247\344\275\215\351\242\204\347\272\246\347\256\241\347\220\206\347\263\273\347\273\237.java" "b/lettcode/_5731_\345\272\247\344\275\215\351\242\204\347\272\246\347\256\241\347\220\206\347\263\273\347\273\237.java" new file mode 100644 index 0000000..8b59956 --- /dev/null +++ "b/lettcode/_5731_\345\272\247\344\275\215\351\242\204\347\272\246\347\256\241\347\220\206\347\263\273\347\273\237.java" @@ -0,0 +1,30 @@ +//https://leetcode-cn.com/problems/seat-reservation-manager/ +//5731. 座位预约管理系统 +class SeatManager { + + public Queue head ; + //初始化一个 SeatManager 对象,它管理从 1 到 n 编号的 n 个座位。所有座位初始都是可预约的。 + public SeatManager(int n) { + head = new PriorityQueue<>(); + for(int i = 0 ; i < n ; i++){ + head.add(i+1); + } + } + + //返回可以预约座位的 最小编号 ,此座位变为不可预约。 + public int reserve() { + return head.poll(); + } + + // 将给定编号 seatNumber 对应的座位变成可以预约。 + public void unreserve(int seatNumber) { + head.add(seatNumber); + } +} + +/** + * Your SeatManager object will be instantiated and called as such: + * SeatManager obj = new SeatManager(n); + * int param_1 = obj.reserve(); + * obj.unreserve(seatNumber); + */ \ No newline at end of file diff --git "a/lettcode/_5732_\345\207\217\345\260\217\345\222\214\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204\345\220\216\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240.java" "b/lettcode/_5732_\345\207\217\345\260\217\345\222\214\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204\345\220\216\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240.java" new file mode 100644 index 0000000..e0b7944 --- /dev/null +++ "b/lettcode/_5732_\345\207\217\345\260\217\345\222\214\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204\345\220\216\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240.java" @@ -0,0 +1,17 @@ +//https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/ +//5732. 减小和重新排列数组后的最大元素 +class Solution { + public int maximumElementAfterDecrementingAndRearranging(int[] arr) { + if(arr.length == 0) return 0; + Arrays.sort(arr); + if(arr[0] != 1){ + arr[0] = 1; + } + for(int i = 1 ; i < arr.length; i++){ + if(arr[i] - arr[i-1]>1){ + arr[i]=arr[i-1]+1; + } + } + return arr[arr.length - 1]; + } +} \ No newline at end of file diff --git "a/lettcode/_5733_\346\234\200\350\277\221\347\232\204\346\210\277\351\227\264.java" "b/lettcode/_5733_\346\234\200\350\277\221\347\232\204\346\210\277\351\227\264.java" new file mode 100644 index 0000000..8d7afd6 --- /dev/null +++ "b/lettcode/_5733_\346\234\200\350\277\221\347\232\204\346\210\277\351\227\264.java" @@ -0,0 +1,36 @@ +//https://leetcode-cn.com/problems/closest-room/ +//_5733_最近的房间 +class Solution { + public int[] closestRoom(int[][] rooms, int[][] queries) { + int[][] q=new int[queries.length][3]; + for(int i=0;i y[1]-x[1]); + Arrays.sort(rooms,(x,y)->y[1]-x[1]); + + TreeSet set=new TreeSet<>(); + int idx=0; + int[] ans=new int[q.length]; + Arrays.fill(ans,-1); + for(int i=0;i=q[i][1]){ + set.add(rooms[idx][0]); + idx+=1; + } + Integer a=set.floor(q[i][0]); + Integer b=set.ceiling(q[i][0]); + if(a==null&&b==null){ + ans[q[i][2]]=-1; + }else if(b==null||a==null){ + ans[q[i][2]]=(a==null)?b:a; + }else{ + ans[q[i][2]]=((q[i][0]-a)<=(b-q[i][0]))?a:b; + } + } + return ans; + } +} \ No newline at end of file diff --git "a/lettcode/_5750_\344\272\272\345\217\243\346\234\200\345\244\232\347\232\204\345\271\264\344\273\275.java" "b/lettcode/_5750_\344\272\272\345\217\243\346\234\200\345\244\232\347\232\204\345\271\264\344\273\275.java" new file mode 100644 index 0000000..561eb67 --- /dev/null +++ "b/lettcode/_5750_\344\272\272\345\217\243\346\234\200\345\244\232\347\232\204\345\271\264\344\273\275.java" @@ -0,0 +1,18 @@ +//_5750_人口最多的年份 +//https://leetcode-cn.com/problems/maximum-population-year/ +class Solution { + public int maximumPopulation(int[][] logs) { + int res = 0, cnt = 0; + for (int i = 1950; i <= 2050; i++) { //循环年份 + int s = 0; //记录当前年份存活人数 + for (int[] log : logs) + if (i >= log[0] && i < log[1]) //如果存活 + s ++ ; + if (s > cnt) { //记录存活人数最大值 + res = i; //存活人数最多的年份 + cnt = s; + } + } + return res; + } +} \ No newline at end of file diff --git "a/lettcode/_690_\345\221\230\345\267\245\347\232\204\351\207\215\350\246\201\346\200\247.java" "b/lettcode/_690_\345\221\230\345\267\245\347\232\204\351\207\215\350\246\201\346\200\247.java" new file mode 100644 index 0000000..5a37bad --- /dev/null +++ "b/lettcode/_690_\345\221\230\345\267\245\347\232\204\351\207\215\350\246\201\346\200\247.java" @@ -0,0 +1,36 @@ +//690. 员工的重要性 +//https://leetcode-cn.com/problems/employee-importance/ +/* +// Definition for Employee. +*/ +class Employee { + public int id; + public int importance; + public List subordinates; +}; + +class Solution { + Map map = new HashMap(); + + public int getImportance(List employees, int id) { + //员工1 [1, 15, [2]] + //员工2 [2, 10, [3]] + //员工3 [3, 5, []] + for(Employee n : employees){ + map.put(n.id,n); + } + + return dfs(id); + //ArrayList<> array = new ArrayList<>(); + } + + public int dfs(int id){ + Employee employee = map.get(id); + int total = employee.importance; + List list = employee.subordinates; + for(int subid : list){ + total +=dfs(subid); + } + return total; + } +} \ No newline at end of file diff --git "a/lettcode/_6_Z\345\255\227\345\275\242\345\217\230\346\215\242.java" "b/lettcode/_6_Z\345\255\227\345\275\242\345\217\230\346\215\242.java" new file mode 100644 index 0000000..1f1caa0 --- /dev/null +++ "b/lettcode/_6_Z\345\255\227\345\275\242\345\217\230\346\215\242.java" @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.List; + +//https://leetcode-cn.com/problems/zigzag-conversion/ +//_6_Z字形变换 +public class _6_Z字形变换 { + class Solution { + public String convert(String s, int numRows) { + + if (numRows == 1) return s; + + List rows = new ArrayList<>(); + for (int i = 0; i < Math.min(numRows, s.length()); i++) + rows.add(new StringBuilder()); + + int curRow = 0; + boolean goingDown = false; + + for (char c : s.toCharArray()) { + rows.get(curRow).append(c); + if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown; + curRow += goingDown ? 1 : -1; + } + + StringBuilder ret = new StringBuilder(); + for (StringBuilder row : rows) ret.append(row); + return ret.toString(); + } + } +} +/* + +作者:LeetCode +链接:https://leetcode-cn.com/problems/zigzag-conversion/solution/z-zi-xing-bian-huan-by-leetcode/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ diff --git "a/lettcode/_740_\345\210\240\351\231\244\345\271\266\350\216\267\345\276\227\347\202\271\346\225\260.java" "b/lettcode/_740_\345\210\240\351\231\244\345\271\266\350\216\267\345\276\227\347\202\271\346\225\260.java" new file mode 100644 index 0000000..8287c83 --- /dev/null +++ "b/lettcode/_740_\345\210\240\351\231\244\345\271\266\350\216\267\345\276\227\347\202\271\346\225\260.java" @@ -0,0 +1,36 @@ +//https://leetcode-cn.com/problems/delete-and-earn/ +//_740_删除并获得点数 +public class _740_删除并获得点数 { + + class Solution { + public int deleteAndEarn(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } else if (nums.length == 1) { + return nums[0]; + } + int len = nums.length; + int max = nums[0]; + for (int i = 0; i < len; ++i) { + max = Math.max(max, nums[i]); + } +// 构造一个新的数组all + int[] all = new int[max + 1]; + for (int item : nums) { + all[item]++; + } + int[] dp = new int[max + 1]; + dp[1] = all[1] * 1; + dp[2] = Math.max(dp[1], all[2] * 2); +// 动态规划求解 + for (int i = 2; i <= max; ++i) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + i * all[i]); + } + return dp[max]; + } + } +}/* +作者:fakerleet +链接:https://leetcode-cn.com/problems/delete-and-earn/solution/ru-guo-ni-li-jie-liao-da-jia-jie-she-zhe-ti-ni-ken/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ \ No newline at end of file diff --git "a/lettcode/_993_\344\272\214\345\217\211\346\240\221\347\232\204\345\240\202\345\205\204\345\274\237\350\212\202\347\202\271.java" "b/lettcode/_993_\344\272\214\345\217\211\346\240\221\347\232\204\345\240\202\345\205\204\345\274\237\350\212\202\347\202\271.java" new file mode 100644 index 0000000..d0d3d27 --- /dev/null +++ "b/lettcode/_993_\344\272\214\345\217\211\346\240\221\347\232\204\345\240\202\345\205\204\345\274\237\350\212\202\347\202\271.java" @@ -0,0 +1,52 @@ +//https://leetcode-cn.com/problems/cousins-in-binary-tree/ +//_993_二叉树的堂兄弟节点 +class Solution { + // x 的信息 + int x; + TreeNode xParent; + int xDepth; + boolean xFound = false; + + // y 的信息 + int y; + TreeNode yParent; + int yDepth; + boolean yFound = false; + + public boolean isCousins(TreeNode root, int x, int y) { + this.x = x; + this.y = y; + dfs(root, 0, null); + return xDepth == yDepth && xParent != yParent; + } + + public void dfs(TreeNode node, int depth, TreeNode parent) { + if (node == null) { + return; + } + + if (node.val == x) { + xParent = parent; + xDepth = depth; + xFound = true; + } else if (node.val == y) { + yParent = parent; + yDepth = depth; + yFound = true; + } + + // 如果两个节点都找到了,就可以提前退出遍历 + // 即使不提前退出,对最坏情况下的时间复杂度也不会有影响 + if (xFound && yFound) { + return; + } + + dfs(node.left, depth + 1, node); + + if (xFound && yFound) { + return; + } + + dfs(node.right, depth + 1, node); + } +} \ No newline at end of file diff --git "a/lettcode/_\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.java" "b/lettcode/_\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.java" new file mode 100644 index 0000000..8925e38 --- /dev/null +++ "b/lettcode/_\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.java" @@ -0,0 +1,33 @@ +//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xn2925/ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode pre = head; + int last = length(head) - n; + if(last == 0){ + return head.next; + } + for(int i = 0 ; i < last - 1; i++){ + pre = pre.next; + } + pre.next = pre.next.next; + return head; + } + + private int length(ListNode head){ + for(int len = 0;head != null;){ + len++; + head = head.next; + } + return len; + } +} \ No newline at end of file diff --git "a/lettcode/_\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.java" "b/lettcode/_\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.java" new file mode 100644 index 0000000..dce725c --- /dev/null +++ "b/lettcode/_\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.java" @@ -0,0 +1,16 @@ +//字符串中的第一个唯一字符 + +class Solution { + public int firstUniqChar(String s) { + int count[] = new int[26]; + char[] chars = s.toCharArray(); + //先统计每个字符出现的次数 + for (int i = 0; i < s.length(); i++) + count[chars[i] - 'a']++; + //然后在遍历字符串s中的字符,如果出现次数是1就直接返回 + for (int i = 0; i < s.length(); i++) + if (count[chars[i] - 'a'] == 1) + return i; + return -1; + } +} \ No newline at end of file diff --git "a/lettcode/_\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260.java" "b/lettcode/_\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260.java" new file mode 100644 index 0000000..9b4f337 --- /dev/null +++ "b/lettcode/_\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260.java" @@ -0,0 +1,37 @@ +//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnoilh/ +//字符串转换整数(atoi) +class Solution { + public int myAtoi(String str) { + str = str.trim();//去掉前后的空格 + //如果为空,直接返回0 + if (str.length() == 0) + return 0; + int index = 0;//遍历字符串中字符的位置 + int res = 0;//最终结果 + int sign = 1;//符号,1是正数,-1是负数,默认为正数 + int length = str.length(); + //判断符号 + if (str.charAt(index) == '-' || str.charAt(index) == '+') + sign = str.charAt(index++) == '+' ? 1 : -1; + for (; index < length; ++index) { + //取出字符串中字符,然后转化为数字 + int digit = str.charAt(index) - '0'; + //按照题中的要求,读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。 + //字符串的其余部分将被忽略。如果读取了非数字,后面的都要忽略 + if (digit < 0 || digit > 9) + break; + //越界处理 + if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) + return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; + else + res = res * 10 + digit; + } + return sign * res; + } +} +/** +作者:数据结构和算法 +链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnoilh/?discussion=5uqDse +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +*/ \ No newline at end of file diff --git "a/lettcode/_\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.java" "b/lettcode/_\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.java" new file mode 100644 index 0000000..3629850 --- /dev/null +++ "b/lettcode/_\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.java" @@ -0,0 +1,24 @@ +//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnmav1/ +//最长公共前缀 +public class _最长公共前缀 { + public static void main(String[] args) { + //TEST ["flower","flow","flight"] + args = new String[]{"flower","flow","flight"}; + _最长公共前缀 ane = new _最长公共前缀(); + System.out.println(new Solution().longestCommonPrefix(args)); + } + static class Solution { + public String longestCommonPrefix(String[] strs) { + if(strs.length == 0 ||strs == null){ + return ""; + } + //默认第一个字符串是他们的公共前缀 + String pre = strs[0]; + for(int i = 1 ; i < strs.length; i++){ + while (strs[i].indexOf(pre) != 0) + pre = pre.substring(0, pre.length() - 1); + } + return pre; + } + } +} \ No newline at end of file diff --git "a/lettcode/_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.java" "b/lettcode/_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.java" new file mode 100644 index 0000000..8f59894 --- /dev/null +++ "b/lettcode/_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.java" @@ -0,0 +1,29 @@ +//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2f9gg/ +//有效的数独 +class Solution { + public boolean isValidSudoku(char board[][]) { + int length = board.length; + //二维数组line表示的是对应的行中是否有对应的数字,比如line[0][3] + //表示的是第0行(实际上是第1行,因为数组的下标是从0开始的)是否有数字3 + int line[][] = new int[length][length]; + int column[][] = new int[length][length]; + int cell[][] = new int[length][length]; + for (int i = 0; i < length; ++i) + for (int j = 0; j < length; ++j) { + //如果还没有填数字,直接跳过 + if (board[i][j] == '.') + continue; + //num是当前格子的数字 + int num = board[i][j] - '0' - 1; + //k是第几个单元格,9宫格数独横着和竖着都是3个单元格 + int k = i / 3 * 3 + j / 3; + //如果当前数字对应的行和列以及单元格,只要一个由数字,说明冲突了,直接返回false。 + //举个例子,如果line[i][num]不等于0,说明第i(i从0开始)行有num这个数字。 + if (line[i][num] != 0 || column[j][num] != 0 || cell[k][num] != 0) + return false; + //表示第i行有num这个数字,第j列有num这个数字,对应的单元格内也有num这个数字 + line[i][num] = column[j][num] = cell[k][num] = 1; + } + return true; + } +} \ No newline at end of file diff --git "a/lettcode/_\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.java" "b/lettcode/_\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.java" new file mode 100644 index 0000000..b4fad3e --- /dev/null +++ "b/lettcode/_\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.java" @@ -0,0 +1,17 @@ +//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xne8id/ +//验证回文串 +public boolean isPalindrome(String s) { + if (s == null || s.length() == 0) + return true; + + s = s.toLowerCase(); + for (int i = 0, j = s.length() - 1; i < j; i++, j--) { + while (i < j && !Character.isLetterOrDigit(s.charAt(i))) + i++; + while (i < j && !Character.isLetterOrDigit(s.charAt(j))) + j--; + if (s.charAt(i) != s.charAt(j)) + return false; + } + return true; +} diff --git a/lettcode/arrays.java b/lettcode/arrays.java new file mode 100644 index 0000000..5670141 --- /dev/null +++ b/lettcode/arrays.java @@ -0,0 +1,56 @@ +import java.util.Arrays; +import java.util.List; + +/* public int minimumTotal(List> triangle) { + int nums; + List> night; + List> left; + if(night % 2 == 0){ + + triangle + night.add(); + } + + } +*/class Solution { + + + /* + 0 1 2 3 4 + 0 ∞ 0 ∞ ∞ ∞ + 1 ∞ 2 ∞ ∞ ∞ + 2 ∞ 3 4 ∞ ∞ + 3 ∞ 6 5 7 ∞ + 4 ∞ 4 1 8 3 + + */ + + private int minInArray(int[] arr) { + int min = Integer.MAX_VALUE; + for (int e : arr) { + min = Math.min(min, e); + } + return min; + } + + private void initialiseWithInfinity(int arr[][]) { + for (int[] ints : arr) { + Arrays.fill(ints, Integer.MAX_VALUE); + } + } + + public int minimumTotal(List> triangle) { + int m = triangle.size(); + if (m == 0) return 0; + int[][] dp = new int[m + 1][m + 1]; + initialiseWithInfinity(dp); + dp[0][1] = 0; // base case + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= i; j++) { + dp[i][j] = triangle.get(i - 1).get(j - 1) + Math.min(dp[i - 1][j - 1], dp[i - 1][j]); + } + } + return minInArray(dp[m]); + } + +} \ No newline at end of file diff --git a/lettcode/s.java b/lettcode/s.java new file mode 100644 index 0000000..e4cdf39 --- /dev/null +++ b/lettcode/s.java @@ -0,0 +1,17 @@ +/* + public String longestCommonPrefix(String[] strs) { + //边界条件判断 + if (strs == null || strs.length == 0) + return ""; + //默认第一个字符串是他们的公共前缀 + String pre = strs[0]; + int i = 1; + while (i < strs.length) { + //不断的截取 + while (strs[i].indexOf(pre) != 0) + pre = pre.substring(0, pre.length() - 1); + i++; + } + return pre; + } +*/ diff --git a/lettcode/there_3.java b/lettcode/there_3.java new file mode 100644 index 0000000..817f08e --- /dev/null +++ b/lettcode/there_3.java @@ -0,0 +1,26 @@ +//https://leetcode.com/problems/longest-substring-without-repeating-characters/ + +import java.util.HashSet; +import java.util.Set; + +public class there_3{ + public static void main(String[] args) { + Solution solution = new Solution(); + System.out.println(solution.lengthOfLongestSubstring("abcabcbb")); + } + static class Solution { + public int lengthOfLongestSubstring(String s) { + int i = 0, j =0 , count = 0; + Set set = new HashSet<>(); + while(i < s.length()){ + if(!set.contains(s.charAt(i))){ + set.add(s.charAt(i++)); + count = count > set.size() ? count : set.size(); + } else { + set.remove(s.charAt(j++)); + } + } + return count; + } + } +} \ No newline at end of file diff --git "a/lettcode/\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.java" "b/lettcode/\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.java" new file mode 100644 index 0000000..9511e04 --- /dev/null +++ "b/lettcode/\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.java" @@ -0,0 +1,17 @@ +//https://leetcode-cn.com/leetbook/read/array-and-string/cxqdh/ +//搜索插入位置 +class Solution { + public int searchInsert(int[] nums, int target) { + int a=nums.length; + for(int i=0;i +/** +https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/ + +*/ + +int removeDuplicates(int* nums, int numsSize){ + if (numsSize <= 1) { + return numsSize; + } + + int i = 0,j = 1; + while (i+1 <= numsSize) { + if (nums[i] != nums[j-1]) { + nums[j] = nums[i]; + j ++; + } + i++; + } + return j; +} \ No newline at end of file diff --git "a/lettcode/\346\272\2203.cpp" "b/lettcode/\346\272\2203.cpp" new file mode 100644 index 0000000..f90cf78 --- /dev/null +++ "b/lettcode/\346\272\2203.cpp" @@ -0,0 +1,12 @@ +#include + +int main() +{ + int arr[10] = {0}; + int is = 0; + for (is = 0; is < 10; is++)//i<11DzԵģԽ + { + arr[is] = is; + } + return 0; +} diff --git "a/lettcode/\351\233\266\347\237\251\351\230\265.java" "b/lettcode/\351\233\266\347\237\251\351\230\265.java" new file mode 100644 index 0000000..d389bbe --- /dev/null +++ "b/lettcode/\351\233\266\347\237\251\351\230\265.java" @@ -0,0 +1,24 @@ +//https://leetcode-cn.com/leetbook/read/array-and-string/ciekh/ +//零矩阵 +class Solution { + public void setZeroes(int[][] matrix) { + boolean[] row = new boolean[matrix.length]; + boolean[] column = new boolean[matrix[0].length]; + for(int i = 0 ; i < matrix.length;i++){ + //查找0 + for(int j=0;j +using namespace std; + +#define MAXSIZE 50 +typedef int KeyType; + +typedef struct +{ KeyType key; +} ElemType; + +typedef struct +{ ElemType *R; + int length; +} SSTable; + +void Create(SSTable &T) +{ int i; + T.R=new ElemType[MAXSIZE+1]; + cin>>T.length; + for(i=1;i<=T.length;i++) + cin>>T.R[i].key; +} + +int Search_Bin(SSTable T, KeyType k); + +int main () +{ SSTable T; KeyType k; + Create(T); + cin>>k; + int pos=Search_Bin(T,k); + if(pos==0) cout<<"NOT FOUND"<k) + j=mid-1; + else if(T.R[mid].key + +#define MAXN 10 +typedef float ElementType; + +ElementType Median( ElementType A[], int N ); + +int main () +{ + ElementType A[MAXN]; + int N, i; + + scanf("%d", &N); + for ( i=0; i 0; p /= 2){ + for (int i = p; i < N; i++){ + //求N的总数是否为A【i】的中位数 + s = A[ i ]; + //判断数组中符合条件的 + for (j = i;j >= p;j -= p ){ + if ( s < A[ j - p ]) + A[j] = A[ j - p ]; + else + break; + } + A[j] = s; + } + } + + return A[ N / 2 ]; +} \ No newline at end of file diff --git "a/\346\264\233\350\260\267/P1425.cpp" "b/\346\264\233\350\260\267/P1425.cpp" new file mode 100644 index 0000000..8b1e6e4 --- /dev/null +++ "b/\346\264\233\350\260\267/P1425.cpp" @@ -0,0 +1,25 @@ +//https://www.luogu.com.cn/problem/P1425#submit +//P1425 小鱼的游泳时间 + +#include + +int main() +{ + //19 10 + //12 50 + //6:20 + int a,b,c,d; + scanf("%d %d %d %d",&a,&b,&c,&d); + if(0 <= a <= 24 || 0 <= c <= 24 || 0 <= b <= 60 || 0 <= d <= 60){ + //确认时间 + if(d +int main() +{ + char a; + scanf("%c",&a); + printf("%c",a -32); + return 0; +} \ No newline at end of file diff --git "a/\346\264\233\350\260\267/P5705.cpp" "b/\346\264\233\350\260\267/P5705.cpp" new file mode 100644 index 0000000..e9320ab --- /dev/null +++ "b/\346\264\233\350\260\267/P5705.cpp" @@ -0,0 +1,13 @@ +//P5704 +//https://www.luogu.com.cn/problem/P5704#submit +//P5705 【深基2.例7】数字反转 + +#include +using namespace std; +string a; +int main() +{ + cin>>a; + for(int i=a.size()-1;i>=0;i--)cout< +using namespace std; + +int main(){ + double a; + int b; + cin>>a>>b; + cout< +#include +#include +#include +#define inf 100010 +#define ll long long +#define INF 0x7fffffff +#define ull unsigned long long +using namespace std; + +const int fx[] = {0, -2, -1, 1, 2, 2, 1, -1, -2}; +const int fy[] = {0, 1, 2, 2, 1, -1, -2, -2, -1}; + +inline int read(){ + int num = 0; char c = getchar(), up = c; + while(c < '0' || c > '9') up = c, c = getchar(); + while(c >= '0' && c <= '9') num = (num << 1) + (num << 3) + (c ^ '0'), c = getchar(); + return up == '-' ? -num : num; +} + +int bx, by, mx, my; +ull f[40];//这次只需要一维数组啦 +bool s[40][40]; + +int main(){ + bx = read(); by = read(); + mx = read(); my = read(); + bx += 2; by += 2; mx += 2; my += 2; + f[2] = 1;//初始化 + s[mx][my] = 1; + for(int i = 1;i <= 8; i++) + s[ mx + fx[i] ][ my + fy[i] ]=1; + for(int i = 2; i <= bx; i++){ + for(int j = 2; j <= by; j++){ + if(s[i][j]){ + f[j] = 0;//还是别忘了清零 + continue; + } + f[j] += f[j - 1]; + //全新的 简洁的状态转移方程 + } + } + printf("%llu\n", f[by]); + return 0; +} \ No newline at end of file diff --git "a/\346\264\233\350\260\267/_P5703.cpp" "b/\346\264\233\350\260\267/_P5703.cpp" new file mode 100644 index 0000000..f095c0b --- /dev/null +++ "b/\346\264\233\350\260\267/_P5703.cpp" @@ -0,0 +1,18 @@ +//P5703 【深基2.例5】苹果采购 +//https://www.luogu.com.cn/problem/P5703 + +#include //头文件:使用一些函数必备的,如 cin , cout + +#include //头文件:使用 scanf , printf 必备的 + +using namespace std; +//关于using namespace std;可以看一下这个https://blog.csdn.net/quyafeng2011/article/details/68921750 + +int a,b; +//定义两个整形的变量,因为题目中说保证输入和答案都在int范围内的非负整数。所以用int就够了。如果超出了int的范围的话要使用long long。 + +int main() {//主函数 + cin>>a>>b;//输入,可以使用scanf("%d%d",&a,&b); + cout<