Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87076b0

Browse files
committed
Added tasks 1 - 2100.
1 parentf8fc30b commit87076b0

File tree

1,655 files changed

+134563
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,655 files changed

+134563
-0
lines changed

‎README.md

Lines changed: 1654 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
##1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers`nums` and an integer`target`, return_indices of the two numbers such that they add up to`target`_.
6+
7+
You may assume that each input would have**_exactly_ one solution**, and you may not use the_same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[2,7,11,15], target = 9
14+
15+
**Output:**[0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return[0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[3,2,4], target = 6
22+
23+
**Output:**[1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums =[3,3], target = 6
28+
29+
**Output:**[0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
***Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
39+
40+
##Solution
41+
42+
```java
43+
importjava.util.Arrays;
44+
45+
publicclassSolution {
46+
publicint[]twoSum(int[]nums,inttarget) {
47+
if (nums==null|| nums.length<=1) {
48+
returnnewint[0];
49+
}
50+
int[] result=newint[2];
51+
int left=0;
52+
int right= nums.length-1;
53+
int[] nums1=Arrays.copyOf(nums, nums.length);
54+
Arrays.sort(nums1);
55+
while (left< right) {
56+
if (nums1[left]+ nums1[right]== target) {
57+
break;
58+
}elseif (nums1[left]+ nums1[right]> target) {
59+
right--;
60+
}elseif (nums1[left]+ nums1[right]< target) {
61+
left++;
62+
}
63+
}
64+
65+
for (int i=0; i< nums.length; i++) {
66+
if (nums1[left]== nums[i]) {
67+
result[0]= i;
68+
break;
69+
}
70+
}
71+
for (int j= nums.length-1; j>=0; j--) {
72+
if (nums1[right]== nums[j]) {
73+
result[1]= j;
74+
break;
75+
}
76+
}
77+
78+
int tmp= result[0];
79+
result[0]=Math.min(result[0], result[1]);
80+
result[1]=Math.max(tmp, result[1]);
81+
return result;
82+
}
83+
}
84+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
##2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two**non-empty** linked lists representing two non-negative integers. The digits are stored in**reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 =[2,4,3], l2 =[5,6,4]
14+
15+
**Output:**[7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 =[0], l2 =[0]
22+
23+
**Output:**[0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 =[9,9,9,9,9,9,9], l2 =[9,9,9,9]
28+
29+
**Output:**[8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range`[1, 100]`.
34+
*`0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
36+
37+
##Solution
38+
39+
```java
40+
importcom_github_leetcode.ListNode;
41+
42+
/*
43+
* Definition for singly-linked list.
44+
* public class ListNode {
45+
* int val;
46+
* ListNode next;
47+
* ListNode(int x) { val = x; }
48+
* }
49+
*/
50+
publicclassSolution {
51+
publicListNodeaddTwoNumbers(ListNodel1,ListNodel2) {
52+
ListNode dummyHead=newListNode(0);
53+
ListNode p= l1;
54+
ListNode q= l2;
55+
ListNode curr= dummyHead;
56+
int carry=0;
57+
while (p!=null|| q!=null) {
58+
int x= (p!=null)? p.val:0;
59+
int y= (q!=null)? q.val:0;
60+
int sum= carry+ x+ y;
61+
carry= sum/10;
62+
curr.next=newListNode(sum%10);
63+
curr= curr.next;
64+
if (p!=null) {
65+
p= p.next;
66+
}
67+
if (q!=null) {
68+
q= q.next;
69+
}
70+
}
71+
if (carry>0) {
72+
curr.next=newListNode(carry);
73+
}
74+
return dummyHead.next;
75+
}
76+
}
77+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
##3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string`s`, find the length of the**longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
*`s` consists of English letters, digits, symbols and spaces.
41+
42+
##Solution
43+
44+
```java
45+
publicclassSolution {
46+
publicintlengthOfLongestSubstring(Strings) {
47+
int[] lastIndices=newint[256];
48+
for (int i=0; i<256; i++) {
49+
lastIndices[i]=-1;
50+
}
51+
52+
int maxLen=0;
53+
int curLen=0;
54+
int start=0;
55+
for (int i=0; i< s.length(); i++) {
56+
char cur= s.charAt(i);
57+
if (lastIndices[cur]< start) {
58+
lastIndices[cur]= i;
59+
curLen++;
60+
}else {
61+
int lastIndex= lastIndices[cur];
62+
start= lastIndex+1;
63+
curLen= i- start+1;
64+
lastIndices[cur]= i;
65+
}
66+
67+
if (curLen> maxLen) {
68+
maxLen= curLen;
69+
}
70+
}
71+
72+
return maxLen;
73+
}
74+
}
75+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
##4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays`nums1` and`nums2` of size`m` and`n` respectively, return**the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be`O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 =[1,3], nums2 =[2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array =[1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 =[1,2], nums2 =[3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array =[1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 =[0,0], nums2 =[0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 =[], nums2 =[1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 =[2], nums2 =[]
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
*`nums1.length == m`
46+
*`nums2.length == n`
47+
*`0 <= m <= 1000`
48+
*`0 <= n <= 1000`
49+
*`1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
51+
52+
##Solution
53+
54+
```java
55+
@SuppressWarnings("java:S2234")
56+
publicclassSolution {
57+
publicdoublefindMedianSortedArrays(int[]nums1,int[]nums2) {
58+
if (nums2.length< nums1.length) {
59+
return findMedianSortedArrays(nums2, nums1);
60+
}
61+
int cut1=0;
62+
int cut2=0;
63+
int n1= nums1.length;
64+
int n2= nums2.length;
65+
int low=0;
66+
int high= n1;
67+
while (low<= high) {
68+
cut1= (low+ high)/2;
69+
cut2= ((n1+ n2+1)/2)- cut1;
70+
int l1= cut1==0?Integer.MIN_VALUE: nums1[cut1-1];
71+
int l2= cut2==0?Integer.MIN_VALUE: nums2[cut2-1];
72+
int r1= cut1== n1?Integer.MAX_VALUE: nums1[cut1];
73+
int r2= cut2== n2?Integer.MAX_VALUE: nums2[cut2];
74+
if (l1<= r2&& l2<= r1) {
75+
if ((n1+ n2)%2==0) {
76+
return (Math.max(l1, l2)+Math.min(r1, r2))/2.0;
77+
}
78+
returnMath.max(l1, l2);
79+
}elseif (l1> r2) {
80+
high= cut1-1;
81+
}else {
82+
low= cut1+1;
83+
}
84+
}
85+
return0.0f;
86+
}
87+
}
88+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp