Skip to content

Commit 525db65

Browse files
committed
Array and String
1 parent 6c8af00 commit 525db65

13 files changed

+231
-4
lines changed

lcJava/1.two-sum.java renamed to lcJava/Array and String Topics/1.two-sum.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ public int[] twoSum(int[] nums, int target) {
2323
return null;
2424
}
2525
}
26-
/**
27-
* 1. The order of indices in the return array should match the problem statement,
28-
* (i.e., the first index should be the one stored in the hashmap and the second should be the current index)
29-
*/
26+
//注意:不要忘掉另一个return,每个条件对应肯定都有return
27+
//HashMap别写错:用wrapper class, 而且形式是开头大写
28+
3029
// @lc code=end
3130

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=167 lang=java
3+
*
4+
* [167] Two Sum II - Input Array Is Sorted
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] twoSum(int[] numbers, int target) {
10+
//sorted array->binary search
11+
//for each element in the array, calculate the complement, binary search for the complement in the folowing subaaray
12+
for(int i=0; i< numbers.length; i++){
13+
//initilize the pointers:binary search from the next element in the array
14+
int low= i+1;
15+
int high= numbers.length-1;
16+
//start search
17+
while(low <= high){
18+
int mid= low + (high-low)/2;
19+
//search for delta
20+
if( numbers[mid] == target-numbers[i] ){
21+
return new int[]{i+1, mid+1};
22+
}else if(numbers[mid] > target-numbers[i]){
23+
high = mid-1;
24+
}else{
25+
low=mid+1;
26+
}
27+
}
28+
}
29+
return new int[]{-1,-1};
30+
}
31+
}
32+
//一般对于sorted array,常见解法有binary search,two pointers
33+
//这里用binary search
34+
35+
// @lc code=end
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=167 lang=java
3+
*
4+
* [167] Two Sum II - Input Array Is Sorted
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] twoSum(int[] numbers, int target) {
10+
//sorted array-> two ppointers
11+
int l=0, r= numbers.length-1;
12+
// int sum = numbers[l]+ numbers[r]; if so the sum will be only calculated once
13+
while(l<r){
14+
int sum = numbers[l]+ numbers[r];
15+
if (sum == target){
16+
return new int[]{l+1,r+1};
17+
}else if(sum < target){
18+
l++;
19+
}else{
20+
r--;
21+
}
22+
}
23+
return new int[]{-1,-1};
24+
}
25+
}
26+
//一般对于sorted array,常见解法有binary search,two pointers
27+
//这里用two pointers:one pointer at the beginning, the other at the end, calculate the sum at each interation
28+
// if the sum is too large, move the right pointer to the left; if the sum is too small, move the left pointer to the right
29+
// @lc code=end
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=27 lang=java
3+
*
4+
* [27] Remove Element
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int removeElement(int[] nums, int val) {
10+
//move the val to the end of the array
11+
int newLength=nums.length;
12+
for (int i = 0; i < newLength; i++) {
13+
if(nums[i] == val){
14+
nums[i]= nums[newLength-1];
15+
newLength--;//contract the arr_length
16+
i--; //go one element back to check if the swapped the elements again fits the requirement.
17+
// so in the next iteration i++, it goes exactly back to the i in the last iteration
18+
19+
}
20+
}
21+
return newLength;
22+
}
23+
}
24+
25+
26+
/*
27+
Swap over:swapping the element to be removed with the last element in the array and then reducing the array size
28+
不需要将当前位置的值放到size-1位置,因为实际上是在缩短数组的有效长度,直接用末端的值覆盖要删除的位置,然后缩短数组的长度即可。
29+
这种方法的重点是避免多余的移动操作,而不是维护原始顺序。
30+
*/
31+
// @lc code=end
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode id=27 lang=java
3+
*
4+
* [27] Remove Element
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int removeElement(int[] nums, int val) {
10+
int s=0;
11+
//for each is better : for (int num: nums)
12+
for (int f=0; f<nums.length; f++){
13+
if(nums[f]!= val){
14+
nums[s]= nums[f];
15+
s++;
16+
}
17+
}
18+
return s; //s 增加几个就有几个元素
19+
}
20+
}
21+
//slow-fast pointers, namley copy-over
22+
//The 2nd approach:
23+
//Swap over:swapping the element to be removed with the last element in the array and then reducing the array size
24+
// @lc code=end
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode id=35 lang=java
3+
*
4+
* [35] Search Insert Position
5+
*/
6+
7+
// @lc code=start
8+
9+
class Solution {
10+
public int searchInsert(int[] nums, int target) {
11+
int l=0, r=nums.length-1;
12+
while(l<=r){
13+
int mid = l+ (r-l)/2;
14+
if(nums[mid]== target){
15+
return mid;
16+
}
17+
else if(nums[mid]<target){
18+
l=mid+1;
19+
}
20+
else
21+
{
22+
r=mid-1;
23+
}
24+
25+
}
26+
return l;
27+
}
28+
}
29+
//equivalent to : search the first value in the array that equals to or bigger than the target
30+
//binary search with O(logn)
31+
// the loop condition : <= (not looking for a pair so it makes senese in the '=' case)
32+
// @lc code=end
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=485 lang=java
3+
*
4+
* [485] Max Consecutive Ones
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int findMaxConsecutiveOnes(int[] nums) {
10+
int maxCount = 0;
11+
int left = 0, right = 0;
12+
int n = nums.length;
13+
// 双指针: left作为主要控制点,确保遍历了数组每个元素,right用于找到每个连续 1 的子序列的结束位置
14+
while (left < n) {//最多执行 n 次,因此是 O(n) left
15+
// Find the first occurrence of 1 starting from
16+
if (nums[left] != 1) {
17+
left++;
18+
} else {
19+
// Move right pointer from left until nums[right] != 1 or end of array
20+
right = left;
21+
// 最多执行 n 次,因此是 O(n)
22+
while (right < n && nums[right] == 1) {
23+
right++;
24+
}
25+
// Update maxCount with the length of the consecutive ones found
26+
maxCount = Math.max(maxCount, right - left);
27+
// Update left to the new position of right
28+
left = right;
29+
}
30+
}
31+
return maxCount;
32+
}
33+
}
34+
//while循环取条件里的最高复杂度
35+
36+
// @lc code=end
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=485 lang=java
3+
*
4+
* [485] Max Consecutive Ones
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int findMaxConsecutiveOnes(int[] nums) {
10+
int count = 0;
11+
int maxCount = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
if (nums[i] == 1) {
14+
count++;
15+
} else {
16+
// when a non-1 element is encoutered, compare the count of 1s with the current
17+
// maxCount
18+
// set the count for the track of the next sequence of 1s
19+
20+
maxCount = Math.max(maxCount, count);
21+
count = 0;
22+
23+
}
24+
}
25+
// if the iteration if completed by a 1s sequence, the count will not be
26+
// comapred to maxCount in the 'else'
27+
// we need to do it again here
28+
maxCount = Math.max(maxCount, count);
29+
return maxCount;
30+
}
31+
}
32+
// Greedy + one pass(O(n))
33+
34+
// @lc code=end
35+

0 commit comments

Comments
 (0)