題目1.實現一個算法,確定一個字符串 s 的所有字符是否全都不同。
示例 1:
輸入: s = “leetcode”
輸出: false
示例 2:
輸入: s = “abc”
輸出: true
限制:
0 <= len(s) <= 100
如果你不使用額外的數據結構,會很加分。
題解
java代碼
class Solution {
public boolean isUnique(String astr) {
char[] arr = astr.toCharArray();
int flag = 0;
for(int i = 0 ; i < arr.length ; i++){
int index = arr[i] - 'a';
int x = 1 << index;
if((flag&x) != 0){
return false;
}else {
flag = flag|x;
}
}
return true;
}
}
Go代碼
func isUnique(astr string) bool {
arr := []byte(astr)
flag := 0
for _, ch := range arr {
num := int8(ch) - int8('a')
x := 1 << num
if (x & flag) == 0 {
flag = x|flag
}else {
return false
}
}
return true
}
題目2.給定兩個字符串 s1 和 s2,請編寫一個程序,確定其中一個字符串的字符重新排列後,能否變成另一個字符串。
示例 1:
輸入: s1 = “abc”, s2 = “bca”
輸出: true
示例 2:
輸入: s1 = “abc”, s2 = “bad”
輸出: false
說明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
題解
java代碼
class Solution {
public boolean CheckPermutation(String s1, String s2) {
if(s1 == null || s2 == null || s1.length() != s2.length()){
return false;
}
char[] arr = s1.toCharArray();
for(int i = 0 ; i < arr.length ; i++){
int idx = s2.indexOf(arr[i]);
if(idx != -1){
s2 = s2.replaceFirst(String.valueOf(arr[i]), "");
}else {
return false;
}
}
return true;
}
}
Go代碼
func CheckPermutation(s1 string, s2 string) bool {
if len(s1) != len(s2) {
return false
}
arr := []byte(s1)
for _, ch := range arr{
if strings.Contains(s2, string(ch)) {
s2 = strings.Replace(s2, string(ch), "", 1)
}else {
return false;
}
}
return true;
}
題目3.迴文排列
給定一個字符串,編寫一個函數判定其是否爲某個迴文串的排列之一。
迴文串是指正反兩個方向都一樣的單詞或短語。排列是指字母的重新排列。
迴文串不一定是字典當中的單詞。
示例1
輸入:“tactcoa”
輸出:true(排列有"tacocat"、“atcocta”,等等)
題解
java代碼
class Solution {
public boolean canPermutePalindrome(String s) {
char[] arr = s.toCharArray();
int[] dic = new int[128];
for(int i=0 ; i<arr.length ; i++){
dic[arr[i]]++;
}
int odd = 0;
for(int i=0 ; i<dic.length ; i++){
if(dic[i]%2 == 1){
odd++;
}
if(odd > 1){
return false;
}
}
return true;
}
}
Go代碼(待補充)
題目4.一次編輯
字符串有三種編輯操作:插入一個字符、刪除一個字符或者替換一個字符。 給定兩個字符串,編寫一個函數判定它們是否只需要一次(或者零次)編輯。
示例1
輸入:
first = “pale”
second = “ple”
輸出: True
示例2
輸入:
first = “pales”
second = “pal”
輸出: False
題解
java代碼
class Solution {
public boolean oneEditAway(String first, String second) {
if(first == null || second == null) {
return false;
}
int lenDiff = first.length() - second.length();
int count = 0;
if(lenDiff <= 1 && lenDiff >= -1) {
for(int i = 0, j = 0 ; i <first.length() && j<second.length() ; i++, j++) {
if(first.charAt(i) != second.charAt(j)){
if(lenDiff == 1){
j--;
}else if(lenDiff == -1) {
i--;
}
count++;
}
}
}else {
return false;
}
return count <= 1;
}
}
Go代碼(待補充)
題目5.字符串壓縮
字符串壓縮。利用字符重複出現的次數,編寫一種方法,實現基本的字符串壓縮功能。比如,字符串aabcccccaaa會變爲a2b1c5a3。若“壓縮”後的字符串沒有變短,則返回原先的字符串。你可以假設字符串中只包含大小寫英文字母(a至z)。
示例1
輸入:“aabcccccaaa”
輸出:“a2b1c5a3”
示例2
輸入:“abbccd”
輸出:“abbccd”
解釋:“abbccd"壓縮後爲"a1b2c2d1”,比原字符串長度更長。
提示
字符串長度在[0, 50000]範圍內。
題解
java代碼
class Solution {
public String compressString(String S) {
if(S == null || S.length() == 0){
return S;
}
char flag = S.charAt(0);
int count = 0;
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < S.length() ; i++) {
if(flag == S.charAt(i)){
count++;
}else {
sb.append(flag).append(count);
flag = S.charAt(i);
count = 1;
}
}
sb.append(flag).append(count);
if(S.length() <= sb.length()){
return S;
}else {
return sb.toString();
}
}
}
Go代碼(待補充)
題目6.旋轉矩陣
給你一幅由 N × N 矩陣表示的圖像,其中每個像素的大小爲 4 字節。請你設計一種算法,將圖像旋轉 90 度。
不佔用額外內存空間能否做到?
示例1
給定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋轉輸入矩陣,使其變爲:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例2
給定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋轉輸入矩陣,使其變爲:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
題解
java代碼
class Solution {
public void rotate(int[][] matrix) {
//j -> i
//len-i -> j
int temp;
int next;
int len = matrix.length;
for(int i = 0 ; i < len/2 ; i++) {
for(int j = i ; j < len - i - 1 ; j++ ){
temp = matrix[i][j];
for(int t = 0 ; t < 4 ; t++){
//進行4次旋轉
int nexti = j;
int nextj = len - i - 1;
next = matrix[nexti][nextj];
matrix[nexti][nextj] = temp;
temp = next;
i = nexti;
j = nextj;
}
}
}
}
}
Go代碼(待補充)
7.零矩陣
編寫一種算法,若M × N矩陣中某個元素爲0,則將其所在的行與列清零。
示例1
輸入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
輸出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例2
輸入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
輸出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
題解
java代碼
class Solution {
public void setZeroes(int[][] matrix) {
boolean[] arr1 = new boolean[matrix.length];
boolean[] arr2 = new boolean[matrix[0].length];
for(int i = 0 ; i < matrix.length ; i++) {
for(int j = 0 ; j < matrix[0].length ; j++) {
if(matrix[i][j] == 0){
arr1[i] = true;
arr2[j] = true;
}
}
}
for(int i = 0 ; i < arr1.length ; i++) {
for(int j = 0 ; j < arr2.length ; j++) {
if(arr1[i] || arr2[j]) {
matrix[i][j] = 0;
}
}
}
}
}
Go代碼(待補充)
題目8.迴文鏈表
編寫一個函數,檢查輸入的鏈表是否是迴文的。
示例1
輸入: 1->2
輸出: false
示例2
輸入: 1->2->2->1
輸出: true
**進階:**你能否用 O(n) 時間複雜度和 O(1) 空間複雜度解決此題?
題解
關鍵字:快慢指針找鏈表中點,反轉鏈表
Java代碼
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode rNode;
if(fast == null){
//偶數個節點 後半部分直接反轉
rNode = reverse(slow);
}else {
//奇數個節點 slow後移一個之後再反轉
rNode = reverse(slow.next);
}
ListNode newNode = head;
while(rNode != null){
if(rNode.val != newNode.val) {
return false;
}
rNode = rNode.next;
newNode = newNode.next;
}
return true;
}
public ListNode reverse(ListNode head) {
ListNode pre = null;
while(head != null) {
ListNode node = head.next;
head.next = pre;
pre = head;
head = node;
}
return pre;
}
}
題04.03.特定深度節點鏈表
給定一棵二叉樹,設計一個算法,創建含有某一深度上所有節點的鏈表(比如,若一棵樹的深度爲 D,則會創建出 D 個鏈表)。返回一個包含所有深度的鏈表的數組。
示例:
輸入:[1,2,3,4,5,null,7,8]
1
/ \
2 3
/ \ \
4 5 7
/
8
輸出:[[1],[2,3],[4,5,7],[8]]
題解
關鍵字:使用Queue作爲臨時存儲結構
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode[] listOfDepth(TreeNode tree) {
if(tree == null) {
return new ListNode[0];
}
List<ListNode> lst = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(tree);
while(queue.size() > 0) {
ListNode head = new ListNode(0);
ListNode temp = head;
int size = queue.size();
while(size > 0){
TreeNode curTreeNode = queue.poll();
ListNode curListNode = new ListNode(curTreeNode.val);
temp.next = curListNode;
temp = curListNode;
if(curTreeNode.left != null) {
queue.offer(curTreeNode.left);
}
if(curTreeNode.right != null) {
queue.offer(curTreeNode.right);
}
size--;
}
lst.add(head.next);
}
return lst.toArray(new ListNode[lst.size()]);
}
}
04.04. 檢查平衡性
實現一個函數,檢查二叉樹是否平衡。在這個問題中,平衡樹的定義如下:任意一個節點,其兩棵子樹的高度差不超過 1。
示例 1:
給定二叉樹 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true 。
示例 2:
給定二叉樹 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
返回 false 。
題解
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
return Math.abs(getHeight(root.left)-getHeight(root.right)) <= 1
&& isBalanced(root.left) && isBalanced(root.right) ;
}
private int getHeight(TreeNode root) {
if(root == null) {
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
}