【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.


那些房子在街頭被搶劫後,小偷發現自己偷竊一個新的地方不會收到太多人注意。這一次,這裏的房子都排成一個圓形,這說第一間房子和最後一間房子是鄰居。

這些房子的安全系統和之前的房子是一樣的。給出一個非負數的整數表,代表每個房子的錢數,確定今晚你可以搶劫的最大金額而不報警。



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