image_2022-03-22_14-30-43.png
23.4 KB
#N1523. Count Odd Numbers in an Interval Range
problem link
#solution
problem link
#solution
class Solution {
public int countOdds(int low, int high) {
return low % 2 == 0 && high % 2 == 0 ? (high-low)/2: (high-low)/2+1;
}
}image_2022-03-22_14-39-24.png
30 KB
#N191. Number of 1 Bits
problem link
#solution
problem link
#solution
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count=0;
while(n!=0){
if((n&1)==1)
count++;
n=n>>>1;
}
return count;
}
}image_2022-03-22_15-00-24.png
41.4 KB
#N202. Happy Number
problem link
#solution
problem link
#solution
class Solution {
public boolean isHappy(int n) {
int count=6;
while(count-- >0){
if(convert(n)!=1)
n=convert(n);
else
return true;
}
return false;
}
public int convert(int num){
int sum=0;
while(num>0){
sum+=Math.pow(num%10, 2);
num/=10;
}
return sum;
}
}image_2022-03-22_15-25-51.png
39.5 KB
#N1790. Check if One String Swap Can Make Strings Equal
problem link
#solution
problem link
#solution
class Solution {
public boolean areAlmostEqual(String s1, String s2) {
List<Integer> l = new ArrayList<>();
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) l.add(i);
if (l.size() > 2) return false;
}
return l.size() == 0 || (l.size() == 2 && s1.charAt(l.get(0)) == s2.charAt(l.get(1)) && s1.charAt(l.get(1)) == s2.charAt(l.get(0)));
}
}image_2022-03-22_16-13-42.png
32.9 KB
#N303. Range Sum Query - Immutable
problem link
#solution
problem link
#solution
class NumArray {
int nums[];
public NumArray(int[] nums) {
this.nums=nums;
}
public int sumRange(int left, int right) {
int sum=0;
for(int i=left; i<=right; i++)
sum+=nums[i];
return sum;
}
}image_2022-03-25_16-11-14.png
21.9 KB
#N1757. Recyclable and Low Fat Products
problem link
#solution
problem link
#solution
select product_id from products
where low_fats='Y' and recyclable='Y';
image_2022-03-28_17-12-32.png
22.1 KB
#N1693. Daily Leads and Partners
problem link
#solution
problem link
#solution
SELECT date_id, make_name, COUNT(DISTINCT lead_id) AS unique_leads, COUNT(DISTINCT partner_id) AS unique_partners
FROM DailySales group by date_id, make_name;
image_2022-03-28_17-18-13.png
21.5 KB
#N1741. Find Total Time Spent by Each Employee
problem link
#solution
problem link
#solution
select event_day as day, emp_id, (sum(out_time) - sum(in_time)) as total_time
from employees group by emp_id, day;
image_2022-03-28_17-43-22.png
21 KB
#N1873. Calculate Special Bonus
problem link
#solution
problem link
#solution
SELECT employee_id, (
CASE
WHEN employee_id%2=1 AND name NOT LIKE 'M%' THEN salary
ELSE 0
END
) AS bonus FROM employees;
image_2022-03-28_19-56-30.png
22.4 KB
#medium
#N1393. Capital Gain/Loss
problem link
#solution
#N1393. Capital Gain/Loss
problem link
#solution
select stock_name, sum(
case
when operation = 'Buy' then -price
else price
end
) as capital_gain_loss from stocks
group by stock_name
image_2022-03-29_17-58-36.png
27 KB
#medium
#N287. Find the Duplicate Number
problem link
#solution
#N287. Find the Duplicate Number
problem link
#solution
class Solution {
public int findDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int n: nums){
if(!set.contains(n))
set.add(n);
else
return n;
}
return -1;
}
}image_2022-03-30_15-50-07.png
42 KB
#medium
#N74. Search a 2D Matrix
problem link
#solution
#N74. Search a 2D Matrix
problem link
#solution
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r=matrix.length, c = matrix[0].length;
int start=0, end=r*c-1, mid=(start+end)/2;
while(start<=end){
if(matrix[mid/c][mid%c]>target)
end=mid-1;
else if(matrix[mid/c][mid%c]<target)
start=mid+1;
else
return true;
mid=(start+end)/2;
}
return false;
}
}image_2022-03-30_15-59-49.png
22.3 KB
#N1587. Bank Account Summary II
problem link
#solution
problem link
#solution
select
u.name, sum(t.amount) as balance
from users u
join transactions t on u.account=t.account
group by t.account
having balance>10000;
image_2022-04-01_14-59-19.png
34.1 KB
#N1232. Check If It Is a Straight Line
problem link
#solution
problem link
#solution
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x0 = coordinates[0][0], y0 = coordinates[0][1],
x1 = coordinates[1][0], y1 = coordinates[1][1];
int dx = x1 - x0, dy = y1 - y0;
for (int[] co : coordinates) {
int x = co[0], y = co[1];
if (dx * (y - y1) != dy * (x - x1))
return false;
}
return true;
}
}image_2022-04-01_15-06-04.png
36.4 KB
#N404. Sum of Left Leaves
problem link
#solution
problem link
#solution
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int res=0;
if(root == null) return 0;
if(root.left != null){
if(root.left.left == null && root.left.right == null) res += root.left.val;
else res += sumOfLeftLeaves(root.left);
}
res+=sumOfLeftLeaves(root.right);
return res;
}
}image_2022-04-01_16-16-16.png
33.5 KB
#N589. N-ary Tree Preorder Traversal
problem link
#solution
problem link
#solution
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorder(Node root) {
if(root==null) return list;
list.add(root.val);
for(int i=0; i<root.children.size(); i++){
preorder(root.children.get(i));
}
return list;
}
}image_2022-04-01_16-31-47.png
55.6 KB
#N953. Verifying an Alien Dictionary
problem link
#solution
problem link
#solution
class Solution {
public boolean isAlienSorted(String[] words, String order) {
for(int i=0; i<words.length-1; i++){
if(!check(words[i], words[i+1], order))
return false;
}
return true;
}
public boolean check(String s1, String s2, String order){
for(int i=0; i<Math.min(s1.length(), s2.length()); i++){
if(order.indexOf(s1.charAt(i))<order.indexOf(s2.charAt(i)))
return true;
if(order.indexOf(s1.charAt(i))>order.indexOf(s2.charAt(i)))
return false;
}
return s1.length()<=s2.length();
}
}part_1.jpg
226.3 KB
#N705. Design HashSet
problem link
#solution
problem link
#solution
class MyHashSet {
List<Integer>[] container = null;
int cap = 1000;
double loadFactor = 0.75;
int count = 0;
public MyHashSet() {
container = new LinkedList[cap];
}
public void add(int key) {
if(contains(key)) return;
if(loadFactor*cap == count){
count = 0;
//rehash
cap *= 2;
List<Integer>[] oldC = container;
container = new LinkedList[cap];
for(int i=0; i < oldC.length; i++){
List<Integer> list = oldC[i];
if(list != null){
for(int entry : list)
this.add(entry);
}
}
}
int hash = key % cap;
if(container[hash] == null)
container[hash] = new LinkedList<>();
container[hash].add(key);
++count;
}part_2.jpg
182 KB
public void remove(int key) {
int hash = key % cap;
List<Integer> list = container[hash];
if(list != null){
Iterator<Integer> itr = list.iterator();
while(itr.hasNext())
if(itr.next() == key){
itr.remove();
--count;
break;
}
}
}
public boolean contains(int key) {
int hash = key % cap;
List<Integer> list = container[hash];
if(list != null){
Iterator<Integer> itr = list.iterator();
while(itr.hasNext())
if(itr.next() == key)
return true;
}
return false;
}
}👍2
image_2022-04-03_13-28-26.png
70.5 KB
#medium
#N1396. Design Underground System
problem link
#solution
#N1396. Design Underground System
problem link
#solution
class UndergroundSystem {
Map<Integer, Pair<String, Integer>> checkInMap = new HashMap<>();
Map<String, Pair<Integer, Integer>> timeMap = new HashMap<>();
public UndergroundSystem() {}
public void checkIn(int id, String stationName, int t) {
checkInMap.put(id, new Pair<>(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
Pair<String, Integer> checkIn = checkInMap.get(id);
String stations = checkIn.getKey()+"-"+stationName;
Pair<Integer, Integer> times = timeMap.getOrDefault(stations, new Pair<>(0, 0));
int sum=(t-checkIn.getValue())+times.getKey();
timeMap.put(stations, new Pair<>(sum, times.getValue()+1));
}
public double getAverageTime(String startStation, String endStation) {
Pair<Integer, Integer> times = timeMap.get(startStation+"-"+endStation);
return (double)times.getKey()/times.getValue();
}
}