大纲:0204 完成的

✅ 657. 机器人能否返回原点

https://leetcode-cn.com/problems/robot-return-to-origin/

✅ 1299. 将每个元素替换为右侧最大元素

https://leetcode-cn.com/problems/replace-elements-with-greatest-element-on-right-side/

✅ 1051 高度检查器

https://leetcode-cn.com/problems/height-checker

✅ 728 自除数

https://leetcode-cn.com/problems/self-dividing-numbers

✅ 104 二叉树的最大深度

https://leetcode-cn.com/problems/maximum-depth-of-binary-tree


notes

✅1051 高度检查器

首先我们其实并不关心排序后得到的结果,我们想知道的只是在该位置上,与最小的值是否一致

题目中已经明确了值的范围 1 <= heights[i] <= 100

这是一个在固定范围内的输入,比如输入: [1,1,4,2,1,3]

输入中有 3 个 1,1 个 2,1 个 3 和 1 个 4,3 个 1 肯定会在前面,依次类推

所以,我们需要的仅仅只是计数而已

  • 错误的一次解答:

  • fix:

✅ 728 自除数

https://leetcode-cn.com/problems/self-dividing-numbers

brute

class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList();
for (int n = left; n <= right; ++n) {
if (selfDividing(n)) ans.add(n);
}
return ans;
}
public boolean selfDividing(int n) {
for (char c: String.valueOf(n).toCharArray()) {
if (c == '0' || (n % (c - '0') > 0))
return false;
}
return true;
}
/*
Alternate implementation of selfDividing:
public boolean selfDividing(int n) {
int x = n;
while (x > 0) {
int d = x % 10;
x /= 10;
if (d == 0 || (n % d) > 0) return false;
}
return true;
*/
}

c解答:

//tt 主要是 temp%10 这个技巧,此可以 提取出 数中的每个位的数,
//tt eg: 128 依次提出: 8, 2, 1
//tt 总结就是 temp%10 这个技巧, 替代了 上述java 中的 `String.valueOf(n).toCharArray()` /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* selfDividingNumbers(int left, int right, int* returnSize){
int cnt=0;
int*num=(int*)malloc(sizeof(int)*(right-left+1));
for(int i=left;i<=right;i++)
{
int temp=i;
int flag=0;
while(temp!=0)
{
if(temp%10==0||(i%(temp%10))!=0)
{
flag=1;
break;
}
temp/=10;
}
if(flag!=1)
num[cnt++]=i;
}
*returnSize=cnt;
return num;
}

java switch 语句

switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}

java api: array 直接有 length 属性 ,不必:length()

✅104 二叉树 的最大高度,py java 对比

  • code:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        // BFS way, use Queue
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int depth = 0;
        while(!queue.isEmpty()){
            depth += 1;
            int fixedQueueSize = queue.size();
            for(int i = 0; i < fixedQueueSize; i++){
                TreeNode node = queue.poll();
                if(node.left != null) {
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                } 
            }
            
        }
        return depth;
    }
}

JAVA数组的toString()方法不能直接输出数组内容

你需要: Arrays.toString(array) 

java Queueoffer, poll

LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。

        //add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
queue.offer("a"); queue.poll()); //返回第一个元素,并在队列中删除
  • 纯递归似乎很好:

✅657机器人回到原点

class Solution {
public boolean judgeCircle(String moves) {
char [] allMovesSplited = moves.toCharArray();
int len = allMovesSplited.length;
int i = 0;
int x = 0;
int y = 0;
while(i < len) {
switch(allMovesSplited[i]) {
case 'U':
y++;
break;
case 'D':
y--;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
default:
break;
}
i++;
}
return x==0 && y==0;
}
}
//or
class Solution {
public boolean judgeCircle(String moves) {
int col = 0, row = 0;
for(char ch : moves.toCharArray()){
if(ch == 'U') row++;
else if(ch == 'D') row--;
else if(ch == 'L') col--;
else col++;
}
return col == 0 && row == 0;
}
}

✅1299 将每个元素替换为右侧最大元素(倒序来的思路很好)

class Solution {
public int[] replaceElements(int[] arr) {
//brute way
for(int i = 0; i < arr.length - 1; i++){
int maxInLatter = arr[i + 1];
for(int j = i + 1; j < arr.length; j++) {
if(arr[j] > maxInLatter){
//update the newest max
maxInLatter = arr[j];
}
}
arr[i] = maxInLatter;
}
//update the lastest one to -1;
arr[arr.length - 1]= -1;
return arr;
}
} //better class Solution {
public int[] replaceElements(int[] arr) {
int max = -1;
for(int i = arr.length - 1; i >= 0; i--){
int tmp = arr[i];
arr[i] = max;
if(tmp > max){
max = tmp;
}
}
return arr;
}
}

leetcode: 0204 完成的的更多相关文章

  1. 【LeetCode】LCP 07. 传递信息

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. 如何获取object数据的描述符

    const data = { portLand: '78/50', Dublin: '88/52', Lima: '58/40' } Object.defineProperty(data, 'Lima ...

  2. django入门与实践(开)

    1.什么是Django? 基于python的高级web开发框架 高效 快速 免费 开源 正常上网流程 浏览器浏览网页的基本原理 请求响应过程 开发环境搭建 Python Django pip inst ...

  3. 第八届蓝桥杯C++B组 日期问题

    标题:日期问题 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的 ...

  4. 8.14-T1村通网(pupil)

    题目大意 要建设一个村庄的网络 有两种操作可选 1.给中国移动交宽带费,直接连网,花费为 A. 2.向另外一座有网的建筑,安装共享网线,花费为 B×两者曼哈顿距离.   题解 显然的最小生成树的题 见 ...

  5. 实现手写体 mnist 数据集的识别任务

    实现手写体 mnist 数据集的识别任务,共分为三个模块文件,分别是描述网络结构的前向传播过程文件(mnist_forward.py). 描述网络参数优化方法的反向传播 过 程 文件 ( mnist_ ...

  6. tomcat在win10系统中安装失败的问题,修改tomcat内存

    自己以前在其他系统上安装tomcat服务都没有问题,但是在win10系统上安装就经常出现问题,自己总结了一下安装步骤: 1.首先需要配置环境变量, CATALINA_HOME 2.修改service. ...

  7. 普及C组第二题(8.1)

    2000. [2015.8.6普及组模拟赛]Leo搭积木(brick) 题目: Leo是一个快乐的火星人,总是能和地球上的OIers玩得很high.         2012到了,Leo又被召回火星了 ...

  8. __dirname和__filename和process.cwd()三者的区别

    1.process cwd() 方法返回 Node.js 进程当前工作的目录 例:我在F:\自己的文件\自己在网上学习的知识点\node学习\node-API\process 这个文件加下面创建了一个 ...

  9. mysql测试点

    前言 性能测试过程中,数据库相关指标的监控是不可忽视的,在这里我们就MySQL的监控配置及重点涉及性能的一些参数进行说明. 在笔者的日常性能测试过程中,重点关注了这些参数,但不代表仅仅只有这些参数对性 ...

  10. EVE上传Dynamips、IOL和QEMU镜像

    1.镜像保存目录: /opt/unetlab/addons ---/dynamips   Dynamips镜像保存目录 ---/iol               IOL镜像保存目录(运行IOU的镜像 ...