image_2022-03-18_19-39-07.png
30.6 KB
#N2176. Count Equal and Divisible Pairs in an Array
problem link
#solution
problem link
#solution
class Solution {
public int countPairs(int[] nums, int k) {
int count=0;
for(int i=0; i<nums.length-1; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]==nums[j] && i*j%k==0)
count++;
}
}
return count;
}
}image_2022-03-19_01-08-21.png
51.2 KB
#medium
#N1630. Arithmetic Subarrays
problem link
#solution
#N1630. Arithmetic Subarrays
problem link
#solution
class Solution {
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
List<Boolean> list = new ArrayList<>();
for(int i=0; i<l.length; i++){
int temp[] = new int[r[i]-l[i]+1];
for(int j=l[i]; j<=r[i]; j++){
temp[j-l[i]]=nums[j];
}
Arrays.sort(temp);
list.add(check(temp));
}
return list;
}
public boolean check(int[] arr){
for(int i=1; i<arr.length-1; i++){
if(arr[i]*2!=arr[i-1]+arr[i+1])
return false;
}
return true;
}
}👍2
image_2022-03-19_01-13-18.png
27.8 KB
#N2185. Counting Words With a Given Prefix
problem link
#solution
problem link
#solution
class Solution {
public int prefixCount(String[] words, String pref) {
int count=0;
for(String word: words){
if(word.startsWith(pref))
count++;
}
return count;
}
}🔥2
image_2022-03-19_15-14-53.png
30 KB
#N2169. Count Operations to Obtain Zero
problem link
#solution
problem link
#solution
class Solution {
public int countOperations(int num1, int num2) {
int count=0;
while(num1*num2!=0){
if(num1<num2)
num2-=num1;
else
num1-=num2;
count++;
}
return count;
}
}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;
}
}