2000. 反转单词前缀
Leetcode
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
idx = word.find(ch) + 1
return word[:idx][::-1] + word[idx:] if idx != 0 else word
class Solution {
public String reversePrefix(String word, char ch) {
char[] arr = word.toCharArray();
for (int i = 0, j = word.indexOf(ch); i < j; i++, j--){
char tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return new String(arr);
}
}
2165. 重排数字的最小值
Leetcode
class Solution:
def smallestNumber(self, num: int) -> int:
nums = list(str(num))
if nums[0] == '-': nums[1:] = sorted(nums[1:], reverse=True)
else:
nums.sort()
for i in range(len(nums)):
if nums[i] != '0':
nums[0], nums[i] = nums[i], nums[0]
break
return int(''.join(nums))
class Solution {
public long smallestNumber(long num) {
char[] nums = String.valueOf(num).toCharArray();
if (nums[0] == '-') {
Arrays.sort(nums, 1, nums.length);
int i = 1, j = nums.length - 1;
while (i < j){
char ch = nums[i]; nums[i] = nums[j]; nums[j] = ch;
i++;j--;
}
} else {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++){
if (nums[i] != '0'){
char ch = nums[i]; nums[i] = nums[0]; nums[0] = ch;
break;
}
}
}
return Long.parseLong(String.valueOf(nums));
}
}
基础知识
char 和 String 相互转换
Java 中 char 是基本类型,而 String 是引用类型。
String 转换为 char
String s = "abcd";
int index = 2;
char ch = s.charAt(index);
char[] chars = s.toCharArray();
char、char[] 转换为 String
String s = String.valueOf('c');
String s = String.valueOf(new char[]{'c'});
String s = Character.toString('c');
String s = new Character('c').toString();
String s = "" + 'c';
String s = new String(new char[]{'c'});
char[] data = {‘a’, ‘b’, ‘c’};
String s = new String(data);
String s = String.valueOf(data);
char[] charArr = s.toCharArray();
String s = "abc,def,ghi";
String[] strArr= s.split(",");
String[] arr = {"0123","sb","12f"};
StringBuffer sb = new StringBuffer();
for(int i = 0; i < arr.length; i++){
sb.append(arr[i]);
}
String s = sb.toString();
String s = new StringBuilder(s).reverse().toString();
带 valueof 逆向,带 to 顺向:
int 与 String 互相转换
1、String 转 int 有两种方式:
Integer.parseInt(str)
Integer.valueOf(str).intValue()
---------------------------------
String str = "123";
int n = 0;
for(int i = 0; i < 100000; i++){
n = Integer.parseInt(str);
}
1、int 转 String 有三种方式
int num = 10;
String str = num + "";
String str = String.valueOf(num);
String str = Integer.toString(num);
采用 String.valueOf(object) 的基础是 Object.toString(),JDK 中 String .valueOf(object) 源码:
public static String valueOf(Object obj){return (obj==null)?"null":obj.toString();}
使用该方法不必担心 object 为 null 的情况,但同时注意当 object 为 null 时该方法返回 “null”,而非 nul l!!!
采用 Integer.toString() 的基础仍是 Object.toString(),因为 java.lang.Object 类中已有 public 方法 toString(),所以对任何严格意义上的 Java 对象都可以调用此方法,但使用时需要注意,必须保证 object 不是 null 值,否则将会抛出NullPointerException 异常!!!
isDigit()+ Integer.parseInt
正则表达式 String.matches("[0-9]*") 检查输入是否为有效数字。
String number = "-100";
int i;
if (String.isDigit(number)) {
i = Integer.parseInt(number));
} else {
System.out.println("Please provide a valid digit [0-9]");
}
public static boolean isDigit(String input) {
if (input == null || input.length() < 0)
return false;
input = input.trim();
if ("".equals(input))
return false;
if (input.startsWith("-")) {
return input.substring(1).matches("[0-9]*");
} else {
return input.matches("[0-9]*");
}
}
|