LeetCode题解(八)0700-0799,kotlin面试题

public TreeNode searchBST(TreeNode root, int val) {

if (root == null)

return root;

if (root.val == val)

return root;

else if (val < root.val)

return searchBST(root.left, val);

else

return searchBST(root.right, val);

}

}

709. To Lower Case

[Description\

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

]

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

[Example]

Example 1:

Input: “Hello”

Output: “hello”

Example 2:

Input: “here”

Output: “here”

Example 3:

Input: “LOVELY”

Output: “lovely”

[Answer]

Runtime: 0 ms, faster than 100.00% of Java online submissions for To Lower Case.

Memory Usage: 34.1 MB, less than 99.91% of Java online submissions for To Lower Case.

class Solution {

public String toLowerCase(String str) {

return str.toLowerCase();

}

}

Runtime: 0 ms, faster than 100.00% of Java online submissions for To Lower Case.

Memory Usage: 34 MB, less than 99.91% of Java online submissions for To Lower Case.

class Solution {

public String toLowerCase(String str) {

char[] arrayChar = str.toCharArray();

for (int i = 0; i < arrayChar.length; i++) {

if (arrayChar[i] >= ‘A’ && arrayChar[i] <= ‘Z’)

arrayChar[i] += 32;

}

return new String(arrayChar);

}

}

class Solution {

public String toLowerCase(String str) {

StringBuilder sb = new StringBuilder();

for (char c : str.toCharArray()) {

if ((c - 0) >= 65 && (c - 0) <= 90) {

sb.append(Character.toChars(c + 32));

} else {

sb.append©;

}

}

return sb.toString();

}

}

728. Self Dividing Numbers

[Description]

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:

left = 1, right = 22

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

The boundaries of each input argument are 1 <= left <= right <= 10000.

[Answer]

Runtime: 7 ms, faster than 13.00% of Java online submissions for Self Dividing Numbers.

Memory Usage: 36.2 MB, less than 33.49% of Java online submissions for Self Dividing Numbers.

class Solution {

public List selfDividingNumbers(int left, int right) {

List list = new ArrayList();

while (left <= right) {

if (isSelfNumber(left))

list.add(left);

left++;

}

return list;

}

private boolean isSelfNumber(int num) {

if (num < 1)

return false;

if (num < 10)

return true;

String str = num + “”;

if (str.contains(“0”))

return false;

for (char c : str.toCharArray()) {

int n = Integer.parseInt("" + c);

if (num % n != 0)

return false;

}

return true;

}

}

771. Jewels and Stones

[Description]

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”.

[Example]

Example 1:

Input: J = “aA”, S = “aAAbbbb”

Output: 3

Example 2:

Input: J = “z”, S = “ZZ”

Output: 0

Note:

S and J will consist of letters and have length at most 50.

The characters in J are distinct.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值