1、二维数组的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
Solution1:我的暴力解决
public class Solution {
public boolean Find(int target, int [][] array) {
int a = array.length,b=array[0].length;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++)
if(array[i][j]==target)return true;
else if(array[i][j]>target)break;
}
return false;
}
}
Solution2:从左下or右下找,一次排除一行or一列
public class Solution {
public boolean Find(int target, int [][] array) {
int rows = array.length;
if(rows == 0)return false;
int cols = array[0].length;
if (cols==0) return false;
//从左下开始
int i = rows-1;
int j = 0;
while(i>=0 && j<cols){
if(array[i][j] < target){
j++;
}else if(array[i][j]>target){
i--;
}else return true;
}
return false;
}
}
2、替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
Solution:遍历循环判断
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
public String replaceSpace (String s) {
// write code here
String res = "";
for(int i=0;i<s.length();i++){
if((s.charAt(i))==' ')res+="%20";
else res+=s.charAt(i);
}
return res;
}
}
Solution: java自带的replaceAll
public String replaceSpace (String s) {
// write code here
if(s==null||"".equals(s))return s;
return s.replaceAll(" ","%20");
}
3、从尾到头打印链表
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
Solution:简单直接:先把链表保存到数组,再倒着放到另一个数组
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
//if(listNode==null)return null;
ArrayList<Integer> arr = new ArrayList<Integer>();
ArrayList<Integer> res = new ArrayList<Integer>();
while(listNode!=null){
arr.add(listNode.val);
listNode=listNode.next;
}
for(int i = arr.size()-1;i>=0;i--){
res.add(arr.get(i));
}
return res;
}
}
4、用两个栈实现队列
solution:一个作为存储数据,另一个作为取出数据的辅助容器。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
while(!stack1.empty()){
stack2.push(stack1.pop());
}
int p = stack2.pop();
while(!stack2.empty()){
stack1.push(stack2.pop());
}
return p;
}
}
5、跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
solution1:递归(时间复杂度和空间复杂度过高)
public class Solution {
int cnt;
public int jumpFloor(int target) {
cnt = 0;
recur(target);
return cnt;
}
public void recur(int n){
if(n==0){
cnt++;
return;
}
if(n<0){
return;
}
recur(n-1);
recur(n-2);
}
}
solution2:动态规划
dp[i] = dp[i-1]+dp[i-2];
public class Solution {
int cnt;
public int jumpFloor(int target) {
int[] dp = new int[target+1];
dp[0]=dp[1]=1;
for (int i=2;i<=target;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[target];
}
}
6.跳台阶扩展问题
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
Solution1: 动态规划, f(n) = f(n-1)+f(n-2)+...+f(2)+f(1)+f(0),其中f(0)=1,f(1)=1
Solution2:找规律发现,f(n) = 2^(n-1)
public class Solution {
public int jumpFloorII(int target) {
if(target==1)return 1;
if(target==2)return 2;
//System.out.print(2<<1); 2<<1 = 4
return 2<<(target-2);
}
}
7.连续子数组的最大和
输入一个整型数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为 O(n).
public int FindGreatestSumOfSubArray(int[] array) {
int max = array[0];
for(int i=1;i<array.length;i++){
array[i]+= array[i-1]>0?array[i-1]:0;
max = getMax(max,array[i]);
}
return max;
}
8.第一个只出现一次的字符
要找字符串中只出现一次的字符,只需要知道它第一次出现的位置是否为当前位置,当前位置后面是否还出现该字符,如果没有则输出当前位置
public class Solution {
public int FirstNotRepeatingChar(String str) {
int index = -1;
for(int i=0;i<str.length();i++){
int first = str.indexOf(str.charAt(i),0);
index = str.indexOf(str.charAt(i),i+1);
if(first==i && index<0){
index = i;
return index;
}
}
return index;
}
}