声明:题、图源自Leecode:https://leetcode-cn.com/
01.01:判定字符是否唯一
题目:
解答①:输入只有小写字母。执行用时 0 ms,内存消耗 36 MB。
class Solution {
public boolean isUnique(String astr) {
if(astr.length()>26) return false;
char[] astr_char=astr.toCharArray(); //将字符串转成字符数组
int num[]=new int[26]; //用来记录每个字符出现的次数
int a=0; //a用来存储当前字符对应的值
for(int i=0;i<astr_char.length;i++){
//需要考虑大写字母和小写字母的ASCII码:a-z的取值范围是97-122,A-Z的取值范围是65-90
a=Integer.valueOf(astr_char[i])-97; //获取(当前字符的ASCII码-65)的值
if(++num[a]>1){
return false;
}
}
return true;
}
}
解答②:利用set集合的不可重复性解题。执行用时 0 ms,内存消耗 36.2 MB。
class Solution {
public boolean isUnique(String astr) {
Set<Character> astr_set=new HashSet<Character>(); //注意:set集合不能使用基本类型
char[] astr_char=astr.toCharArray();
for(int i=0;i<astr_char.length;i++){
//set集合的特点是不可重复,如果字符重复即会加入失败
if(!astr_set.add(astr_char[i]))
return false;
}
return true;
}
}
解答③:考虑输入的可能有大写字母和小写字母两种情况,同一字母的大小写不算相同。
class Solution {
public boolean isUnique(String astr) {
char[] astr_char=astr.toCharArray(); //将字符串转成字符数组
int num[]=new int[58]; //用来记录每个字符出现的次数
int a=0; //a用来存储当前字符对应的值
for(int i=0;i<astr_char.length;i++){
//需要考虑大写字母和小写字母的ASCII码:a-z的取值范围是97-122,A-Z的取值范围是65-90
a=Integer.valueOf(astr_char[i])-65; //获取(当前字符的ASCII码-65)的值
if(++num[a]>1){
return false;
}
}
return true;
}
}
02.01:移除重复节点
题目
编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
解题
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if(head==null) return null; //空链表情况
Set<Integer> set=new HashSet(); //利用set集合的不可重复性
//由于头结点永远是第一个出现的结点,不会造成重复,因此不用考虑删除头结点的情况。
ListNode p1=head;
set.add(p1.val);
ListNode p2=p1.next; //p2指向p1的下一位
while(p2!=null){ //当p2等于空,则p1指向了尾结点
if(!set.add(p2.val)){ //若添加不成功则当前是重复结点
//删除p2结点
p1.next=p2.next;
p2=p1.next;
}else{
//p1与p2向后移
p1=p1.next;
p2=p2.next;
}
}
return head;
}
}
10.01:合并排序的数组
题目:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sorted-merge-lcci
解题:
class Solution {
public void merge(int[] a, int m, int[] b, int n) {
int k=m+n-1;
if(k<0){
}else if(m==0){
while(n>0){
a[k--]=b[--n];
}
}else if(n==0){
while(m>0){
a[k--]=a[--m];
}
}else{
m--;n--;
while(k>-1){
if(m==-1){
while(n>-1){
a[k--]=b[n--];
}
break;
}
if(n==-1){
while(m>-1){
a[k--]=a[m--];
}
break;
}
if(a[m]<b[n]){
a[k--]=b[n--];
}else if(a[m]==b[n]){
a[k--]=b[n--];
a[k--]=a[m--];
}else{
a[k--]=a[m--];
}
}
}
}
}
16.26:计算器
题目:
解题:执行用时:11 ms ;内存消耗:38.4 MB
class Solution {
public int calculate(String s){
//假设给定的所有表达式都是有效的
char ss[]=s.toCharArray(); //将字符串s转换成字符数组
//利用栈思想:栈存数值
Stack<Integer> number = new Stack<>(); //存放数字
int num=0;
char c='#';
int i=0;
while(i<ss.length){
num=0;
if(i<ss.length&&ss[i]==' '){
i++; //空格则跳到下一个
continue;
}
c=ss[i]; //当前是运算符时,c才有效
if(c=='+'||c=='/'||c=='*'||c=='-'){ //当前是运算符
i++; //i++获取后面的数字
while(i<ss.length&&ss[i]==' '){
i++; //空格则跳到下一个
}
}
while(i<ss.length&&ss[i]>='0'&&ss[i]<='9'){ //当前是数字
num=num*10+(ss[i]-'0');
i++; //若当前不是数字,则不再进行i++,当前运算符保留到下次操作。
}
//根据运算符进行运算
switch(c){
case '*':num=num*number.pop();break;
case '/':num=number.pop()/num;break;
case '-':num=-num;break;
default:break; //可能是空格
}
number.push(num);
}
int sum=0; //存放结果
while(!number.empty()){
sum+=number.pop();
}
return sum;
}
}
17.10:主要元素
题目:数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回 -1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。
链接:https://leetcode-cn.com/problems/find-majority-element-lcci/
解答:执行用时:4 ms ;内存消耗:44.5 MB
public int majorityElement(int[] nums) {
// 给数组排序
Arrays.sort(nums);
int current=-1;
int sum=0;
for(int i=0;i<nums.length;i++) {
if(nums[i] != current) {
// 与上一个不相同:记录当前值,重新计数
current=nums[i];
sum=1;
}else{
sum++;
}
if(sum > nums.length/2) {
return current;
}
}
return -1;
}