Day1附加题目
Leetcode: 69
二分查找 Java
class Solution {
public int mySqrt(int x) {
long i = 0;
long j = (x >> 1) + 1;
while (i <= j) {
long middle = i + ((j - i) >> 1);
if (middle * middle < x) {
i = middle + 1;
} else if (middle * middle > x) {
j = middle - 1;
} else {
return (int) middle;
}
}
return (int) i - 1;
}
}
Leetcode: 367
二分查找 Java
class Solution {
public boolean isPerfectSquare(int num) {
int i = 0;
int j = (num >> 1) + 1;
while(i <= j){
long middle =(long)(i + ((j - i) >> 1));
if(middle * middle < (long)num){
i = (int)middle + 1;
}else if(middle * middle > (long)num){
j = (int)middle - 1;
}else{
return true;
}
}
return false;
}
}
Leetcode: 283
快慢指针 Java
class Solution {
public void moveZeroes(int[] nums) {
int slowIndex = 0;
for(int fastIndex = 0; fastIndex < nums.length; fastIndex++){
if(nums[fastIndex]!=0){
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
for(int zeroPos = slowIndex; zeroPos < nums.length; zeroPos++){
nums[zeroPos] = 0;
}
}
}
Leetcode: 26
快慢指针 Java
class Solution {
public int removeDuplicates(int[] nums) {
int slowIndex = 1;
for(int fastIndex = 1; fastIndex < nums.length; fastIndex++){
if(nums[fastIndex] != nums[fastIndex-1]){
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
}
Leetcode:844
快慢指针 Java
写法不好,将String转为char[]数组,占用空间,实际上可以直接在字符串上操作 s.charAt(i)
在首次实现中没有考虑左指针下限问题
另本题可以使用栈方法做
class Solution {
public boolean backspaceCompare(String s, String t) {
int left1 = 0;
int left2 = 0;
char[] chars = s.toCharArray();
char[] chart = t.toCharArray();
for(int right1 = 0; right1 < chars.length; right1++){
if(chars[right1]!='#'){
chars[left1] = chars[right1];
left1++;
}else if(left1 > 0){
left1--;
}
}
for(int right2 = 0; right2 < chart.length; right2++){
if(chart[right2]!='#'){
chart[left2] = chart[right2];
left2++;
}else if(left2 > 0){
left2--;
}
}
if (left1!=left2) return false;
for(int i = 0; i < left1; i++){
if(chars[i]!=chart[i]) return false;
}
return true;
}
}
Day2
Leetcode: 977
双指针法,没看讲解按自己思路写,出错了几次
- 没有加 right >= left 判断
- 将判断加在&&后面,导致数组越界的判断语句执行
最终AC代码如下
class Solution {
public int[] sortedSquares(int[] nums) {
int left = 0;
int right = nums.length - 1;
int tmp = nums.length -1;
int[] pownums = new int[nums.length];
while(left <= right){
while(right >= left && nums[right] >= Math.abs(nums[left])){
if(tmp >= 0){
pownums[tmp] = nums[right] * nums[right];
tmp--;
}
else return pownums;
right--;
}
while(right >=left && nums[right] < Math.abs(nums[left])){
if(tmp >= 0){
pownums[tmp] = nums[left] * nums[left];
tmp--;
}
else return pownums;
left++;
}
}
return pownums;
}
}
解析代码更为简便
Leetcode: 209
将题目 ≥ target 看错成等于
然后自己胡写出来一段代码,或许符合等于的要求,但复杂度也很高
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int least = nums.length;
boolean flag = false;
int right = 0;
int left = 0;
while(right < nums.length){
while(right < nums.length - 1 && countSum(nums, left, right) < target){
right++;
}
while(left < right && countSum(nums, left, right) > target){
left++;
}
if(left <= right && right < nums.length){
if(countSum(nums, left, right) == target)
flag = true;
least = Math.min(least, right - left + 1);
right++;
}
}
if(flag) return least;
else return 0;
}
public int countSum(int[] nums, int left, int right){
int sum = 0;
for(int i = left; i <= right; i++)
sum += nums[i];
return sum;
}
}
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0;
int sum = 0;
int result = Integer.MAX_VALUE;
for(int right=0; right < nums.length; right++){
sum += nums[right];
while(sum >= target){
result = Math.min(result, right - left + 1);
sum -= nums[left++];
}
}
result = (result == Integer.MAX_VALUE)?0:result;
return result;
}
}
Leetcode: 904 水果成篮
哈希表+滑动窗口
class Solution {
public int totalFruit(int[] fruits) {
int n = fruits.length;
HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>();
int left = 0;
int ans = 0;
for(int right = 0; right < n; right++){
cnt.put(fruits[right], cnt.getOrDefault(fruits[right], 0) + 1);
while(cnt.size() > 2){
cnt.put(fruits[left], cnt.get(fruits[left]) - 1);
if (cnt.get(fruits[left]) == 0){
cnt.remove(fruits[left]);
}
left++;
}
ans = Math.max(ans, right - left + 1);
}
return ans;
}
}
纯滑动窗口(自己写的,有点乱)
class Solution {
public int totalFruit(int[] fruits) {
int left = 0;
if(fruits.length <= 2) return fruits.length;
int[] type = new int[]{-1, -1};
int[] last = new int[]{-1, -1};
int result = 0;
for(int right = 0; right < fruits.length; right++){
int righttype = fruits[right];
if(righttype != type[0] && righttype != type[1]){
if(last[0] > last[1]){
type[1] = righttype;
left = last[1] + 1;
last[1] = right;
}
else if(last[0] <= last[1]){
type[0] = righttype;
left = last[0] + 1;
last[0] = right;
}
}else if(righttype == type[0]){
last[0] = right;
}else{
last[1] = right;
}
result = Math.max(result, right - left + 1);
}
return result;
}
}
Leetcode:76 最小字串覆盖
哈希表+滑动窗口
class Solution {
public String minWindow(String s, String t) {
int n = s.length();
int m = t.length();
int left = 0;
int ans = Integer.MAX_VALUE;
int ansleft = -1;
int ansright = -1;
boolean subexistlock = false;
boolean subexist = false;
HashMap<Character, Integer> cnt1= new HashMap<Character, Integer>();
HashMap<Character, Integer> cnt2= new HashMap<Character, Integer>();
for(int i = 0; i < m; i++){
char c = t.charAt(i);
cnt2.put(c, cnt2.getOrDefault(c, 0) + 1);
}
for(int right = 0; right < n; right++){
char cright = s.charAt(right);
cnt1.put(cright, cnt1.getOrDefault(cright, 0) + 1);
while(isSubstring(cnt1, cnt2)){
char cleft = s.charAt(left);
cnt1.put(cleft, cnt1.get(cleft) - 1);
if(cnt1.get(cleft) == 0){
cnt1.remove(cleft);
}
left++;
subexistlock = true;
}
if((right - left + 1) < ans && subexistlock){
System.out.print("wwww\n");
ans = right - left + 2;
ansleft = left - 1;
ansright = right + 1;
subexistlock = false;
subexist = true;
}
}
if(subexist)
return s.substring(ansleft, ansright);
else
return "";
}
boolean isSubstring(HashMap<Character, Integer>cnt1, HashMap<Character, Integer>cnt2){
if (cnt1.size() < cnt2.size()) return false;
boolean issub = true;
for(Character c: cnt2.keySet()){
int num2 = cnt2.get(c);
int num1 = cnt1.getOrDefault(c, 0);
if (num1 < num2){
issub = false;
break;
}
}
return issub;
}
}
Leetcode: 59
把我转晕了,这里用的讲解提供的代码
class Solution {
public int[][] generateMatrix(int n) {
int loop = 0; // 控制循环次数
int[][] res = new int[n][n];
int start = 0; // 每次循环的开始点(start, start)
int count = 1; // 定义填充数字
int i, j;
while (loop++ < n / 2) { // 判断边界后,loop从1开始
// 模拟上侧从左到右
for (j = start; j < n - loop; j++) {
res[start][j] = count++;
}
// 模拟右侧从上到下
for (i = start; i < n - loop; i++) {
res[i][j] = count++;
}
// 模拟下侧从右到左
for (; j >= loop; j--) {
res[i][j] = count++;
}
// 模拟左侧从下到上
for (; i >= loop; i--) {
res[i][j] = count++;
}
start++;
}
if (n % 2 == 1) {
res[start][start] = count;
}
return res;
}
}
自己重新写了一遍
class Solution {
public int[][] generateMatrix(int n) {
int[][] result= new int[n][n];
int loop_max = n / 2;
int loop = 0;
int count = 1;
while(loop < loop_max){
for (int j = loop; j < n - loop - 1; j++){
result[loop][j] = count++;
}
for (int i = loop; i < n - loop - 1; i++){
result[i][n-loop-1] = count++;
}
for (int j = n-loop-1; j > loop; j--){
result[n-loop-1][j] = count++;
}
for (int i = n-loop-1; i > loop; i--){
result[i][loop] = count++;
}
loop++;
}
if(n % 2 == 1){
result[n/2][n/2] = n*n;
}
return result;
}
}