【LeetCode】刷题-esay

595. Big Countries


There is a table World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

Write a SQL solution to output big countries' name, population and area.

For example, according to the above table, we should output:

//一个国家拥有大于300万平方公里的土地或者拥有超过2500万的人口,那他就是大国。

//写一个SQL语句,输出大国的名称、人口数量和面积。

//如果根据上面的表格,我们应该怎么写输出语句:


//答案:

select name, population, area from World where area>3000000 or population>25000000


input:

{"headers": {"World": ["name", "continent",	"area",	"population", "gdp"]}, "rows": {"World": [["Afghanistan", "Asia", 652230, 25500100, 20343000000], ["Albania", "Europe", 28748, 2831741, 12960000000], ["Algeria", "Africa", 2381741, 37100000, 188681000000], ["Andorra", "Europe", 468, 78115,	3712000000], ["Angola", "Africa", 1246700, 20609294, 100990000000]]}}

answer:
{"headers": ["name", "population", "area"], "values": [["Afghanistan", 25500100, 652230], ["Algeria", 37100000, 2381741]]}


201. Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.


//两个区间【m,n】0<=m<=n<=2147483647,返回在这个区间内的所有二进制和数据;

//即求m和n二进制编码中,同为1的前缀



//答案:

int count=0;

while(m!=n){

m>>=1;

n>>=1;

count++;

}

return n<<count;

笔记:

m>>=1  m转为2进制,向右边移动1位,并且赋值给m;

n<<count   左移运算 低位补0. 丢弃最高位,0补最低位,相当于左移count位就是乘以2的count次方。


654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

Construct the maximum tree by the given array and output the root node of this tree.


//找出做大的值,然后左右两边相互递归。


//答案:

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
            return subConstructMaximumBinaryTree(nums,0,nums.length-1);

    }
    public static TreeNode subConstructMaximumBinaryTree(int[] nums,int start,int end){
        if (start > end) return null;
        int max = Integer.MIN_VALUE;
        int index = 0;
        for (int i = start; i <= end; i++){
            if (max < nums[i]){
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left =  subConstructMaximumBinaryTree(nums,0,index-1);
        root.right =  subConstructMaximumBinaryTree(nums,index+1,end);
        return root;
    }
}


596. Classes More Than 5 Students

There is a table courses with columns: student andclass

Please list out all classes which have more than or equal to 5 students.

For example, the table:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

Should output:

+---------+
| class   |
+---------+
| Math    |
+---------+
//这是一张课程表,关于学生和班级。

//请找出有5个学生的班级。


//不知道正确错误的答案。。很迷茫这道题

select a.class from (select class,count(class) from(select distinct student,class from courses as bgroup by class having count (class)>=5)as a);


噫吁嚱,危乎高哉。

蜀道之难,难于上青天。


461. Hamming Distance


The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

The above arrows point to positions where the corresponding bits are different.

答案:

class Solution {
    public int hammingDistance(int x, int y) {
          int res = x ^ y;   //二进制 x-y
        int count = 0;  
        for (int i = 0; i < 32; i++) {  
            if ((res & 1) != 0)  
                count++;  
            res >>= 1;     //往右边移一位
        }  
        return count;  
    }
}

笔记:

异或运算符,“相同出0,不同处1”。转成二进制,然后统计1的出现次数


657. Judge Route Circle

nitially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back tothe original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves areR (Right),L (Left),U (Up) andD (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input: "UD"
Output: true

Example 2:

Input: "LL"
Output: false

机器人初始位置是(0,0),然后给机器人一个命令字符串,右(R),左(L),上(U),下(D)。机器人按照命令走完一个圈回到起点,则为true,如果没有,则为flase;

答案:

class Solution {
    public boolean judgeCircle(String moves) {
        int x=0;int y=0;
        int len=moves.size;
        if (len<=0){
            return false;
        }
        for(i;i<len;++i){
            switch(moves[i]){
                case "U":y++;break;
                case "D":y--;break;
                case "R":x++;break;
                case "L":x--;break;
            }
        }
        if(x==0&&y==0){
            return true;
        }else return false;
    }
}


3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be asubstring,"pwke" is asubsequence and not a substring.


答案:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int res=0,left=0,right=0;
        HashSet<Character> t=new HashSet<Character>();
        while(right<s.length()){
         if(!t.contains(s.charAt(right))){
             t.add(s.charAt(right++));
             res=Math.max(res,t.size());
            
         }else{
             t.remove(s.charAt(left++));
         }
            
        }
        return res;
        
 
    }
}

笔记:

Java Character类在对象中包装一个基本类型char的值;

Java String.contains()方法是包含。如果"abc".contains("a") 为true ,"abc".contains("abcd")为false,"abc".contains("d")为false;

Math.max("1","2");去两者最大的数;



198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
你是一个专业的强盗,计划去抢一条街上的房子。每个房子里都有一定量的钱,唯一限制你抢掉所有房子里的钱的是:相邻的房子的安保系统是连接在一起的,如果相邻的两个房子在同一晚上都被盗的话就会自动报警。

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
给出一系列非负的整数代表每个房子内的金钱数量,计算你今晚不触发报警条件的情况下可以抢到的最多的钱。

答案:

class Solution {
       public int rob(int[] nums) {
        if(nums == null || nums.length == 0)
            return 0;
        int[] money = new int[2];
        money[0] = 0;
        money[1] = nums[0];
        for(int i = 1; i < nums.length; i ++) {
            int temp = money[0];
            money[0] = Math.max(money[0], money[1]);
            money[1] = temp + nums[i];
        }
        return money[0] > money[1] ? money[0] : money[1];
    }
}

笔记:


213. House Robber II

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place arearranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonightwithout alerting the police.


那些房子在街头被抢劫后,小偷发现自己偷窃一个新的地方不会收到太多人注意。这一次,这里的房子都排成一个圆形,这说第一间房子和最后一间房子是邻居。

这些房子的安全系统和之前的房子是一样的。给出一个非负数的整数表,代表每个房子的钱数,确定今晚你可以抢劫的最大金额而不报警。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章