Welcome everyone

每周一练(13)

每周一练 汪明鑫 527浏览 0评论

【SQL】

这里有张 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 |
+—————–+————+————+————–+—————+

 

如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。

编写一个SQL查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

+————–+————-+————–+
| name | population | area |
+————–+————-+————–+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+————–+————-+————–+

 

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

 

 

【多线程】

按序打印

 

我们提供了一个类:

public class Foo {
public void one() { print(“one”); }
public void two() { print(“two”); }
public void three() { print(“three”); }
}
三个不同的线程将会共用一个 Foo 实例。

线程 A 将会调用 one() 方法
线程 B 将会调用 two() 方法
线程 C 将会调用 three() 方法
请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。

 

示例 1:

输入: [1,2,3]
输出: “onetwothree”
解释:
有三个线程会被异步启动。
输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
正确的输出是 “onetwothree”。
示例 2:

输入: [1,3,2]
输出: “onetwothree”
解释:
输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
正确的输出是 “onetwothree”。
注意:

尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。

你看到的输入格式主要是为了确保测试的全面性。

 

class Foo {

    public Foo() {
        
    }

    volatile int count = 1;

    public void first(Runnable printFirst) throws InterruptedException {
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        count ++ ;
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while(count!=2){

        }
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        count ++;
    }

    public void third(Runnable printThird) throws InterruptedException {
         while(count!=3){

        }
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}

感觉这题怪怪的。。。

 

这样也行

public class Foo {
    //控制变量
    private int flag = 0;
    //定义Object对象为锁
    private Object lock = new Object();
    public Foo() {
    }
    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (lock){
            //如果flag不为0则让first线程等待,while循环控制first线程如果不满住条件就一直在while代码块中,防止出现中途跳入,执行下面的代码,其余线程while循环同理
            while( flag != 0){
                lock.wait();
            }
            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            //定义成员变量为 1
            flag = 1;
            //唤醒其余所有的线程
            lock.notifyAll();
        }
    }
    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock){
            //如果成员变量不为1则让二号等待
            while (flag != 1){
                lock.wait();
            }
            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            //如果成员变量为 1 ,则代表first线程刚执行完,所以执行second,并且改变成员变量为 2
            flag = 2;
            //唤醒其余所有的线程
            lock.notifyAll();
        }
    }
    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock){
            //如果flag不等于2 则一直处于等待的状态
            while (flag != 2){
                lock.wait();
            }
            // printThird.run() outputs "third". Do not change or remove this line.
            //如果成员变量为 2 ,则代表second线程刚执行完,所以执行third,并且改变成员变量为 0
            printThird.run();
            flag = 0;
            lock.notifyAll();
        }
    }
}

 

【算法】

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。

 

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

 

就跟手机上的九健拼音法

2对应abc,3对应def

 

public class LeetCode_17_LetterCombinationsofaPhoneNumber {
    List<String> result =  new ArrayList<>();

    //手机上数字和字符串的印射关系  输入范围2-9  0和1是为了方便角标统一
    String []maping={
            "",         //0
            "",         //1
            "abc",      //2
            "def",      //3
            "ghi",      //4
            "jkl",      //5
            "mno",      //6
            "pqrs",     //7
            "tuv",      //8
            "wxyz"      //9
    };

    public List<String> letterCombinations(String digits) {
        if(digits==null || "".equals(digits)){
            return result;
        }

        String s = "";
        generateCombinations(digits,0,s);

        return result;
    }

    public void generateCombinations(String digits, int index, String s){

        //得到一组结果
        if(index == digits.length()){
            result.add(s);
            return;
        }

        //获取数字对应的字母 如'2' 对应到'abc'
        char c = digits.charAt(index);
        String str = maping[c - '0'];

        //遍历 字母 如abc
        for (int i = 0; i < str.length() ; i++) {
            generateCombinations(digits, index+1, s + str.charAt(i));
        }
    }

}

 

 

 

转载请注明:汪明鑫的个人博客 » 每周一练(13)

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz