Java十大经典排序算法的实现图解
292
2022-08-19
剑指Offer之Java算法习题精讲链表与字符串及数组
题目一
解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode headhttp://, int val) {
ListNode q = new ListNode(-1);
ListNode a = q;
q.next = head;
while(q.next!=null){
if(q.next.val==val){
q.next = q.next.next;
}else{
q = q.next;
}
}
return a.next;
}
}
题目二
解法
class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length()!=t.length()){
return false;
}
for(int i =0;i if(s.indexOf(s.charAt(i))!=t.indexOf(t.charAt(i))){ return false; } } return true; } } 题目三 解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } 题目四 解法 class Solution { public boolean isAnagram(String s, String t) { if(s.length()!=t.length()) return false; ArrayList ArrayList for (int i = 0; i < s.length(); i++) { list1.add(s.charAt(i)); } for (int i = 0; i < t.length(); i++) { list2.add(t.charAt(i)); } Collections.sort(list1); Collections.sort(list2); for (int i = 0; i < list1.size(); i++) { if (list1.get(i)!=list2.get(i)) return false; } return true; } } //要熟悉API class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } char[] str1 = s.toCharArray(); char[] str2 = t.toCharArray(); Arrays.sort(str1); Arrays.sort(str2); return Arrays.equals(str1, str2); } } 题目五 解法 class Solution { public int missingNumber(int[] nums) { Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { if(i!=nums[i]){ return i; } } return nums.length; } } 题目六 解法 class Solution { public boolean wordPattern(String pattern, String s) { String[] split = s.split(" "); if(split.length!=pattern.length()) return false;http:// int q = 0; for(int i =0;i l: for(int w = 0;w if(split[w].equals(split[i])){ q = w; break l; } } if(pattern.indexOf(pattern.charAt(i))!=q){ return false; } } return true; } }
if(s.indexOf(s.charAt(i))!=t.indexOf(t.charAt(i))){
return false;
}
}
return true;
}
}
题目三
解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
题目四
解法
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
ArrayList
ArrayList
for (int i = 0; i < s.length(); i++) {
list1.add(s.charAt(i));
}
for (int i = 0; i < t.length(); i++) {
list2.add(t.charAt(i));
}
Collections.sort(list1);
Collections.sort(list2);
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i)!=list2.get(i)) return false;
}
return true;
}
}
//要熟悉API
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}
}
题目五
解法
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if(i!=nums[i]){
return i;
}
}
return nums.length;
}
}
题目六
解法
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] split = s.split(" ");
if(split.length!=pattern.length()) return false;http://
int q = 0;
for(int i =0;i l: for(int w = 0;w if(split[w].equals(split[i])){ q = w; break l; } } if(pattern.indexOf(pattern.charAt(i))!=q){ return false; } } return true; } }
l: for(int w = 0;w if(split[w].equals(split[i])){ q = w; break l; } } if(pattern.indexOf(pattern.charAt(i))!=q){ return false; } } return true; } }
if(split[w].equals(split[i])){
q = w;
break l;
}
}
if(pattern.indexOf(pattern.charAt(i))!=q){
return false;
}
}
return true;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~