Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions C语言网/_1.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# lettcode-

题目可能不会经常发布但是不代表我没有做啊!
现在会定时发布一些解题技巧和方法,做到不知学算法还要学框架。
发布时间一个星期一篇算法解题技巧,没有做到就没有做到吧......
20 changes: 20 additions & 0 deletions lettcode/ListNode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

20 changes: 20 additions & 0 deletions lettcode/_1035_不相交的线.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
23 changes: 23 additions & 0 deletions lettcode/_1269_停在原地的方案数.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
16 changes: 16 additions & 0 deletions lettcode/_1310_子数组异或查询.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions lettcode/_137_只出现一次的数字.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
42 changes: 42 additions & 0 deletions lettcode/_1482_制作m束花所需的最少天数.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 43 additions & 0 deletions lettcode/_15_三数之和.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//15. 三数之和
//https://leetcode-cn.com/problems/3sum/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
// 枚举 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<c 的 c 了,可以退出循环
if (second == third) {
break;
}
if (nums[second] + nums[third] == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
return ans;
}
}
23 changes: 23 additions & 0 deletions lettcode/_1738_找出第 K 大的异或坐标值.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<k-1;i++){
queue.poll();
}

return queue.poll();
}
}
20 changes: 20 additions & 0 deletions lettcode/_1_两数之和.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
/**
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;
}
30 changes: 30 additions & 0 deletions lettcode/_22_括号生成.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//https://leetcode-cn.com/problems/generate-parentheses/
//22. 括号生成
class Solution {
List<String> res = new ArrayList<>();
public List<String> 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);
}
}
}
39 changes: 39 additions & 0 deletions lettcode/_26_删除有序数组中的重复项.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
//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<numsSize;i++){
if(nums[i] == nums[i - 1]){
continue;
} else{
nums[j] = nums[i];
j++;
}
}
return j;
}
Loading