1.杨辉三角
给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。
解析
题目需求是根据numRows生成相应生成「杨辉三角」的前 numRows 行,最后方法返回的是一个二维数组。每一行的头尾都是1,中间的数是根据它左上方和右上方的数的和,也就是上一层的数组的两个数的和,所以生成一层的数组先要有上一层的数组。
代码
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> integers = new ArrayList<List<Integer>>();
digui(numRows, integers);
return integers;
}
private List<Integer> digui(int i, List<List<Integer>> integers) {
if (i == 1) {
List<Integer> objects = new ArrayList<>();
objects.add(1);
integers.add(objects);
return objects;
}
List<Integer> objects = new ArrayList<>();
//获取上一层的数组
List<Integer> preObjects = digui(i - 1, integers);
//组装这一层的数组
for (int i1 = 0; i1 < i; i1++) {
if (i1 == 0 || i1 == i - 1) {
objects.add(1);
continue;
}
objects.add(preObjects.get(i1 - 1) + preObjects.get(i1));
}
integers.add(objects);
return objects;
}
判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
解析
使用双指针,一个指针在字符串 s 上标识每个字符,另外一个指针在字符串 t上标识字符,当两个指针指向的字符相同时,字符串 s 上走一位,不相同时字符串 t上的指针走一位。当字符串 s 上的指针走完时返回true,字符串 t上的指针走完,字符串 s 上的指针没走完则返回false。
代码
public boolean isSubsequence(String s, String t) {
if (s.equals("")){
return true;
}
if (t.equals("")&&!s.equals("")){
return false;
}
int sLength = s.length();
int tLength = t.length();
int index =0;
for (int i = 0; i < tLength; i++) {
if (t.charAt(i)==s.charAt(index)){
if (index==sLength-1){
return true;
}
index++;
}
}
return false;
}
翻转数位
给定一个32位整数 num,你可以将一个数位从0变为1。请编写一个程序,找出你能够获得的最长的一串1的长度。
解析
因为题目给的是32位整数 num,所以循环判断1的次数最多为32次,接下来就是为1时计数增加,代码里有两个计数是为了区分有一次机会将一个数位从0变为1,这个机会可以将两个不连续的一串1合在一起获得更长的一串1。
代码
public int reverseBits(int num) {
if(num==0){
return 1;
}
if(num==-1){
return 32;
}
int index = 1;
int count = 0;
int count2 = 0;
//最大值
int max = 0;
//二进制位数
int curCount = 1;
while (curCount <= 32) {
if ((num & index) == index) {
count++;
count2++;
max = Math.max(max, Math.max(count2, count));
} else {
max = Math.max(max, count + 1);
count2 = count + 1;
count = 0;
if ((num & index >> 1) != index >> 1) {
count2 = 0;
}
}
index = index << 1;
curCount++;
}
count++;
max = Math.max(max, Math.max(count2, count));
return max;
}
来源:力扣(LeetCode)