4.递归和动态规划
1.跳跃游戏
题目arr[i]=k 代表可以从位置i 向右跳1-k个距离, arr[2]==3,代表可以从位置2跳到位置3,位置4,位置5. 如果从位置0出发,返回最少跳几次能跳到arr最后的位置上.
arr=[3,2,3,1,1,4]
eg:arr[0] == 3 选择跳到arr[2]==3 再跳一次就返回末尾了, return 2
代码:
说明:
1.jump跳了多少步
2. cur 如果只跳jump步最远能达到的位置
3.next如果再跳一步最远能达到的位置
public int jump(int[] arr){
if(null==arr|| arr.length==0 ){
return 0;
}
int jump =0, cur=0, next=0;
for(int i=0; i<arr.length;i++)
{
if( cur<i ){
jump++;
cur = next ;
}
next =Math.max (next, i+arr[i]);
}
return jump;
}
没看懂,就先这样吧
[leetcode],跳跃游戏 1-5_duchenlong的博客-CSDN博客
五.字符串
1.两个字符串是否互为变形词
"123" 和 "321"是
"123" 和"1123" 不是
public boolean test(String str1, String str2){
空和长度不等retrun false;
//转换成字符数组
char[] ch1 = str1.toCharArray();
char[] ch2= str2.toCharArray();
int[] map = int[256];
int len=ch1.length;
for(int i=0; i <len ; i++){
map[ch1[i]]++;
}
for(int i=0; i <len ; i++){
if (ch2[i]--<0)
return false;
}
return true;
}
2.旋转词
abcd cdab 是
1ab2 ab12 不是
参数 a 和b
String b2=b+b;
return getIndexOf(b2,a)!=-1;
-
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
3.字符串的统计串
aaabbadddffc
返回的是a_3_b_2_a_1_d_3_ f_2_c_1
char[] chs=str.toCharArray;
String res = String.valueOf(chs[0]);
int num=1;
for(int i=1; i< chs.length;i++){
if(chs[i] !=chs[i-1]){
res = concat(res, String.valueOf(num),String.valueOf(ch[i]));
num =1;
}else{
num++;
}
}
4.是否所有字符都只出现过一次
boolean test(){
boolean map=new boolean(256);
for(int i=0; i<chs.length;i++){
if(map[chas[i]])( //注意if里面本身就是一个boolean
//当分配数组时,该数组将初始化为false
//第二次有的话直接返回false
return false;
)
第一次有的话记为为true;
map(chas[i])==true;
}
return true;
}
5.字符串的调整与替换
整体的思想是逆序复制
字符串的替换
空格换成%20
eg: a%20b%20%20c
void replace(char chas[]){
int num=0; //空格有多少
int len=0; //左面半区有多大
for(len=0;len<chas.lengh && chas[len]!=0;len++){
if(chas[len]=='空格') {num++}
}
替换后整体的长度是 int j = len + num * 2 -1;
对字符串的左面的区域进行逆顺序的遍历
for(int i = len-1; i >-1; i--){
if (chas[i]!='空格'){
chas[j--]=chas[i]
}esle[
chas[j--]=' 0';
chas[j--]='2';
chas[j--]='%';
]
}
}
6.翻转字符串
也考的是字符串的逆序
dog loves pig 翻转过后是 pig loves dog
void test(char[] chas){
reverse(chas, 0 , chas.length-1); //先整体逆序一次
//然后拿空格卡位分段逆序
}
void reverse(char[] chas, int start, int end){
char tmp= 0;
while(start<end){
tmp = chas[start];
chas[start] = chas[end];
start++;
end;
}
}
7.括号字符串的有效
str="(((())()))"
本来以为有效括号字符串是中心对称的,但是仔翻翻又不是
public boolean test(String str)
{
char[] chas=str.toCharArray();
int status=0;
for(int i=0; i< chas.length, i++){
if(char[i]!=')' && char[i]!='('){ return false;}
if(chas[i]==')' && --status<0){return false}
if(chas[i] == '('){ status++;}
}
return status==0; //这是一个boolean值
}
8.找指定新类型的字符
要求:
1.1到两个字符
2.小写字母. 大写+小写字母, 大写+大写字母
eg:aaABCDEcBCg
k=7的时候返回的Ec
k =4的时候返回CD
k=10的时候返回 g
我觉得这题没什么意义
6.大数据和空间限制
1.2g内存20亿整数找出现次数最多的数
2.100亿url中重复的url以及搜索词汇的topk问题
这两个是大数据的题
7.位运算
1.不用额外的变量交换两个整数的值
a=a^b;b=a^b; a=a^b;
背下来,拿数往回逆着代一下就可以了
8.数组和矩阵
1.转圈打印矩阵
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
打印的结果是 1,2,3 ,4 , 8 , 12,16, 15, 14,13, 9,5,6,7 ,11,10
左上角的坐标 (tr,tc)
右下角的坐标(dr,dc)
当(tr,tc)=(0,0) (dr,dc)=(5,5)时表示整个矩阵
import java.utils.*;
public class main{
void test(int[][] m) {
int tr=0;
int tc=0;
int dr=m.length -1 ; //行长
int dc=m[0].length -1 ; //列长
//对矩阵进行打印
while(tr<dr && tc< dc){
printEdge(m,tr++, tc++, dr--, dc--);
}
void printEdge(m,tr, tc, dr, dc){
if(tr==dr){
//子矩阵只有一行.
for(int i=tc; i <dc; i++){ sysout (m[tr][i])+" "}
}
else if(tc==dc) {
//子矩阵只有一列
for(int i=tr; i< dr; i++){sysout(m[i][tc]+" ")}
}
else{ //一般情况
int coutC = tC;
int coutR = tR;
while (coutC != dC) {
System.out.print(m[tR][coutC] + " ");
coutC++;
}
while (coutR != dR) {
System.out.print(m[coutR][dC] + " ");
coutR++;
}
while (coutC != tC) {
System.out.print(m[dR][coutC] + " ");
coutC--;
}
while (coutR != tR) {
System.out.print(m[coutR][tC] + " ");
coutR--;
}
————————————————
版权声明:本文为CSDN博主「zai_zi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zai_zi/article/details/107491573
}}
2.正方型矩阵旋转90度
3.打印之字形矩阵
上面两个不太想写了,p365
4.需要排序的最短子数组的长度
arr=[1,5,3,4, 2, 6,7]
返回值是4, 因为只有 5,3,4,2是需要排序的
5.有序矩阵中找指定的数
6.自然数数组的排序
7.奇数下标都是奇数或者偶数的下标都是偶数
void modify(int[] arr){
int even= 0; //偶数
int odd = 1; //奇数
int end = arr.length-1;
while(even<=end)
}