删除链表节点:
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
1603.设计停车系统
class ParkingSystem {
int big,medium,small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
if(carType == 1){
if(big == 0) return false;
else {
big--;
return true;
}
}else if(carType == 2){
if(medium == 0) return false;
else {
medium--;
return true;
}
}else if(carType == 3){
if(small == 0) return false;
else {
small--;
return true;
}
}
return false;
}
}
修改:
class ParkingSystem {
int big,medium,small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
if(carType == 1){
if(big > 0){
big--;
return true;
}
}else if(carType == 2){
if(medium > 0){
medium--;
return true;
}
}else if(carType == 3){
if(small > 0){
small--;
return true;
}
}
return false;
}
}
写一次return false
771.宝石与石头
方法一:
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int jewelscount = 0;
HashSet<Character> jewelsset = new HashSet<>();
for(int i = 0; i < jewels.length(); i++){
char jew = jewels.charAt(i);
jewelsset.add(jew);
}
for(int j = 0; j<stones.length(); j++){
char sto = stones.charAt(j);
if(jewelsset.contains(sto)){
jewelscount++;
}
}
return jewelscount;
}
}
方法二:
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int jewelscount = 0;
HashSet<Character> jewelsset = new HashSet<>();
for(int i = 0; i < jewels.length(); i++){
for(int j = 0; j<stones.length(); j++){
char jew = jewels.charAt(i);
char sto = stones.charAt(j);
if (sto ==jew ){
jewelscount++;
}
}
}
return jewelscount;
}
}