Java Leetcode Day3
1672. 最富有客户的资产总量
第一遍运用暴力
分析缺点:
1)额外开辟数组
2)在新数组里面循坏找出最大值
3)额外申请空间过多
int sum=0;
int max=0;
int n=accounts.length;
int[] faye = new int [n];
for (int i = 0; i < accounts.length; i++) {
for (int j = 0; j < accounts[i].length; j++) {
sum +=accounts[i][j];
}
faye[i]=sum; // [6,6];
sum=0;
}
max=faye[0];
for (int i = 0; i < faye.length; i++) {
if(faye[i]>max) max=faye[i];
}
return max;
2)思路:把所以银行的钱都放入一家银行,只要比较那一家银行就行了
class Solution {
public int maximumWealth(int[][] accounts) {
int n=accounts.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < accounts[i].length; j++) {
accounts[i][0] +=accounts[i][j];
}//[[6,2,1][6,2,1]]累加到第一个元素
accounts[0][0]=Math.max(accounts[0][0],accounts[i][0]);
}//Math.max函数返回两者最大数值
return accounts[0][0];
}
}
78.子集
运用回溯(DFS)算法
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> faye = new ArrayList<>(); //数组列表
int deep = nums.length; //数组长度 ->深度 [1,2,3]
int index; //数组下标 记录当前状态变量
if (nums.length==0) return faye; //如果为空集合 返回空列表
Stack<Integer> zhan=new Stack<>(); //定义栈
dfs(nums,zhan,0,deep,faye); //头节点开始
return faye;
}
//从空集合开始 , 当index =1 有 选1 和不选1 两种选择
private void dfs(int[] nums, Stack<Integer> zhan,
int index, int deep, List<List<Integer>> faye) {
if(deep==index) { //找到叶子节点
faye.add(new ArrayList<>(zhan)); //返回栈元素
return; //faye.add(zhan) 回溯会返回原来节点
}
//不选择 index
dfs(nums, zhan, index+1, deep, faye); //寻找下一个节点
//选择 加入index
zhan.add(nums[index]); //进栈
dfs(nums, zhan, index+1, deep, faye); //再次深搜
zhan.pop(); //出栈
}
//结果 [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
709 转换小写字母
class Solution {
public String toLowerCase(String str) {
char [] faye = str.toCharArray();
int n= faye.length;
for (int i = 0; i < n; i++) {
if(faye[i]<='Z' && faye[i]>='A')
faye[i] +=32;
}
return new String(faye);
}
}
第二种思路:
class Solution {
public String toLowerCase(String str) {
if (str == null || str.length() == 0) {
return str;
}
char[] ch = str.toCharArray();
for (int i = 0; i < str.length(); i++) {
ch[i] |= 32;
}
return String.valueOf(ch);
}
}
206.反转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode fa = null;
ListNode ye = head;
while(ye!=null){
ListNode t=ye.next;
ye.next = fa; //指向空
fa = ye;
ye = t;
}
return fa; //ye最后指向null,此时头节点为fa
}
}
1603.设计停车系统
输入:
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[ [1, 1, 0] , [1], [2], [3], [1]]
输出:
[null, true, true, false, false]
解释:
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // 返回 true ,因为有 1 个空的大车位
parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位
parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位
parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了
class ParkingSystem {
int [] faye = new int [3];
public ParkingSystem(int big, int medium, int small) {
faye[0] = big;
faye[1] = medium;
faye[2] = small;
}
public boolean addCar(int carType) {
if(faye[carType-1]>0){
faye[carType-1]--;
return true;
}
return false;
}
}
7.整数反转
class Solution {
public int reverse(int x) {
long n = 0;
while (x!=0){
n = n*10 +x%10; //3
x=x/10;
}
return (int)n==n ? (int)n:0; //long 转int 失去精度 ,不超就没问题
}
}