楼主: ttgao
跳转到指定楼层
上一主题 下一主题
收起左侧

立贴!从今天开始刷题。

🔗
 楼主| ttgao 2019-5-27 02:29:19 | 只看该作者
全局:
240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:

Consider the following matrix:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-27 02:30:03 | 只看该作者
全局:
用边界来缩小范围。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 08:02:37 | 只看该作者
全局:
重刷了200. Number of Islands。

此题简单讲就是一次遍历所有节点。

发现节点是1,则island数量加1,然后调用另外一个递归函数去判断和这个1相邻的四个点是否也是小岛。

在递归函数内,将节点1换成0这样避免重复访问。

回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 08:03:08 | 只看该作者
全局:
贴一下答案。

    public int numIslands(char[][] grid) {
        int count = 0;
        
        if(grid == null) return count;
        int y = grid.length;
        if(y==0) return count;
        int x = grid[0].length;
        if(x==0) return count;
        
        for(int j=0;j<y;j++){
            for(int i=0;i<x;i++){
                if(grid[j][i]=='1'){
                    count++;
                    marklands(grid,j,i);
                }
            }
        }
        return count;
    }
    public void marklands(char[][] grid,int j, int i)
    {
        int y = grid.length;
        int x = grid[0].length;
        if((j<0)||(i<0)) return;
        if((j>=y)||(i>=x)) return;
        
        if(grid[j][i]=='1'){
            grid[j][i] = '0';
            marklands(grid,j+1,i);
            marklands(grid,j-1,i);
            marklands(grid,j,i+1);
            marklands(grid,j,i-1);
        }
    }
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 08:04:13 | 只看该作者
全局:
答案没怎么美化,就凑合着看看。重刷此题,没看答案没看提示,完全自己做出。估计这题我可以不用再复习了。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 08:18:25 | 只看该作者
全局:
543. Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree
          1
         / \
        2   3
       / \     
      4   5   
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 09:02:33 | 只看该作者
全局:
此题还是看了一下答案才完全写对,需要好好总结一下。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 11:09:38 | 只看该作者
全局:
937. Reorder Log Files

You have an array of logs.  Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumeric identifier.  Then, either:

Each word after the identifier will consist only of lowercase letters, or;
Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs.  It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log.  The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties.  The digit-logs should be put in their original order.

Return the final order of the logs.



Example 1:

Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]


Note:

0 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i] is guaranteed to have an identifier, and a word after the identifier.
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-28 11:09:56 | 只看该作者
全局:
切割字符串,然后再判断是否是数字。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-29 22:56:46 | 只看该作者
全局:
今天开始重刷题目

1. Two Sum
3. Longest Substring Without Repeating Characters
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表