日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

求中位数中回文数之和C语言,一些算法题及答案

發(fā)布時間:2023/12/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求中位数中回文数之和C语言,一些算法题及答案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 兩數(shù)之和

給定一個整數(shù)數(shù)組 nums 和一個目標值 target,請你在該數(shù)組中找出和為目標值的那 兩個 整數(shù),并返回他們的數(shù)組下標。

你可以假設(shè)每種輸入只會對應(yīng)一個答案。但是,你不能重復(fù)利用這個數(shù)組中同樣的元素。

示例:

給定 nums = [2, 7, 11, 15], target = 9

因為 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

/**

* 暴力枚舉法

* @param nums

* @param target

* @return

*/

public static int[] twoSum(int[] nums, int target) {

int lgn = nums.length;

for(int i = 0; i < lgn; i++){

for(int j = i + 1; j < lgn; j++){

if(nums[i] + nums[j] == target){

return new int[]{i, j};

}

}

}

return new int[0];

}

/**

* MAP 處理

* @param nums

* @param target

* @return

*/

public static int[] twoSum1(int[] nums, int target) {

Map map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {

int complement = target - nums[i];

if (map.containsKey(complement)) {

return new int[] { complement, nums[i] };

}

map.put(nums[i], i);

}

return new int[0];

}

2. 兩數(shù)相加

給出兩個 非空 的鏈表用來表示兩個非負的整數(shù)。其中,它們各自的位數(shù)是按照 逆序 的方式存儲的,并且它們的每個節(jié)點只能存儲 一位 數(shù)字。

如果,我們將這兩個數(shù)相加起來,則會返回一個新的鏈表來表示它們的和。

您可以假設(shè)除了數(shù)字 0 之外,這兩個數(shù)都不會以 0 開頭。

示例:

輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)

輸出:7 -> 0 -> 8

原因:342 + 465 = 807

/**

* 鏈表相應(yīng)位置依次相加,最后處理進位

* @param l1

* @param l2

* @return

*/

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode head = null;

ListNode curr = null;

while (l1 != null || l2 != null){

if(l1 != null && l2 != null){

ListNode node = new ListNode(l1.val + l2.val);

if(curr == null && head == null) head = node;

curr = initNode(curr, node);

}else{

curr = initNode(curr, new ListNode(l1 != null?l1.val:l2.val));

}

l1 = l1 != null?l1.next:null;

l2 = l2 != null?l2.next:null;

}

curr = head;

while (curr != null){

if(curr.val >= 10){

curr.val -= 10;

if(curr.next == null){

curr.next = new ListNode(1);

}else {

curr.next.val += 1;

}

}

curr = curr.next;

}

curr = null;

return head;

}

public ListNode initNode(ListNode curr, ListNode newNode){

if(curr != null){

curr.next = newNode;

}

curr = newNode;

return curr;

}

3. 尋找兩個有序數(shù)組的中位數(shù)

給定兩個大小為 m 和 n 的有序數(shù)組 nums1 和 nums2。

請你找出這兩個有序數(shù)組的中位數(shù),并且要求算法的時間復(fù)雜度為 O(log(m + n))。

你可以假設(shè) nums1 和 nums2 不會同時為空。

示例 1:

nums1 = [1, 3]

nums2 = [2]

則中位數(shù)是 2.0

示例 2:

nums1 = [1, 2]

nums2 = [3, 4]

則中位數(shù)是 (2 + 3)/2 = 2.5

/**

* 使用兩個排序數(shù)據(jù)組的歸并過程

* 分別定義兩個數(shù)組的遍歷索引,每次對比提取相應(yīng)數(shù)組的元素

* 不實際存儲歸并后的數(shù)據(jù),

* 處理前半數(shù)元素即可

* @param nums1

* @param nums2

* @return

*/

public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

int lgn1 = nums1.length;

int lgn2 = nums2.length;

int allLgn = lgn1 + lgn2;

int middleIndex = allLgn/2;

int middleLeft = 0,middleRight = 0;

int index1 = 0;

int index2 = 0;

int curr = 0;

for (int i = 0; i < middleIndex + 1; i++) {

if(index1 < lgn1 && index2 < lgn2) {

if (nums1[index1] > nums2[index2]) {

curr = nums2[index2];

index2++;

} else {

curr = nums1[index1];

index1++;

}

}else if(index1 < lgn1){

curr = nums1[index1];

index1++;

}else if(index2 < lgn2){

curr = nums2[index2];

index2++;

}

if(i == middleIndex - 1){

middleLeft = curr;

}

if(i == middleIndex){

middleRight = curr;

}

}

if(allLgn%2 == 0){

return (middleLeft + middleRight)/2.0;

}else {

return middleRight;

}

}

4. Z 字形變換

將一個給定字符串根據(jù)給定的行數(shù),以從上往下、從左到右進行 Z 字形排列。

比如輸入字符串為 "LEETCODEISHIRING" 行數(shù)為 3 時,排列如下:

L C I R

E T O E S I I G

E D H N

之后,你的輸出需要從左往右逐行讀取,產(chǎn)生出一個新的字符串,比如:"LCIRETOESIIGEDHN"。

請你實現(xiàn)這個將字符串進行指定行數(shù)變換的函數(shù):

string convert(string s, int numRows);

示例 1:

輸入: s = "LEETCODEISHIRING", numRows = 3

輸出: "LCIRETOESIIGEDHN"

示例 2:

輸入: s = "LEETCODEISHIRING", numRows = 4

輸出: "LDREOEIIECIHNTSG"

解釋:

L D R

E O E I I

E C I H N

T S G

/**

* 定義目標行數(shù)個鏈表,如示例,每次中間間隔的 numRows - 2 個列表逐個填充一個值

* 便利給定的字符串,依次處理,直到末尾

* @param s

* @param numRows

* @return

*/

public static String convert(String s, int numRows) {

if(numRows == 1){

return s;

}

String result = "";

if(numRows == 2){

result = "";

for (int i = 0; i < s.length(); i = i + 2) {

result += s.charAt(i);

}

for (int i = 1; i < s.length(); i = i + 2) {

result += s.charAt(i);

}

return result;

}

int middleCount = numRows - 2;

List[] all = new LinkedList[numRows];

for (int i = 0; i < numRows; i++) {

all[i] = new LinkedList<>();

}

int sIndex = 0;

int step = 0;

while (sIndex < s.length()){

for (int i = 0; i < numRows; i++) {

if(sIndex == s.length()) break;

all[i].add(s.charAt(sIndex));

sIndex++;

}

for (int j = numRows - 2; j > 0 ; j--) {

if(sIndex == s.length()) break;

all[j].add(s.charAt(sIndex));

sIndex++;

}

step = step + middleCount;

}

for (int i = 0; i < numRows; i++) {

for (int j = 0; j < all[i].size(); j++) {

result += all[i].get(j);

}

}

return result;

}

5. 整數(shù)反轉(zhuǎn)

給出一個 32 位的有符號整數(shù),你需要將這個整數(shù)中每位上的數(shù)字進行反轉(zhuǎn)。

示例 1:

輸入: 123

輸出: 321

示例 2:

輸入: -123

輸出: -321

示例 3:

輸入: 120

輸出: 21

注意:

假設(shè)我們的環(huán)境只能存儲得下 32 位的有符號整數(shù),則其數(shù)值范圍為 [?231,? 231? 1]。請根據(jù)這個假設(shè),如果反轉(zhuǎn)后整數(shù)溢出那么就返回 0。

/**

* 數(shù)據(jù)范圍判斷

* @param x

* @return

*/

public static int reverse(int x) {

double result = 0;

while (x != 0){

result = result * 10 + x%10;

if (result > Integer.MAX_VALUE) return 0;

if (result < Integer.MIN_VALUE) return 0;

x = x/10;

}

return (int) result;

}

6. 回文數(shù)

判斷一個整數(shù)是否是回文數(shù)。回文數(shù)是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數(shù)。

/**

* 轉(zhuǎn)化為字符串,一次便利,首末對稱位置對比

* @param x

* @return

*/

public static boolean isPalindrome(int x) {

String s = String.valueOf(x);

int lgn = s.length();

for (int i = 0,j = lgn -1; i <= j; i++,j--){

if(s.charAt(i) == s.charAt(j)){

continue;

}else {

return false;

}

}

return true;

}

7. 盛最多水的容器

給定 n 個非負整數(shù) a1,a2,...,an,每個數(shù)代表坐標中的一個點 (i, ai) 。在坐標內(nèi)畫 n 條垂直線,垂直線 i 的兩個端點分別為 (i, ai) 和 (i, 0)。找出其中的兩條線,使得它們與 x 軸共同構(gòu)成的容器可以容納最多的水。

說明:你不能傾斜容器,且 n 的值至少為 2。

/**

* 枚舉

* @param height

* @return

*/

public static int maxArea(int[] height) {

int area = 0, lgn = height.length;

if(lgn < 2) return 0;

for (int i = 0; i < lgn; i++) {

for (int i1 = i + 1; i1 < lgn; i1++) {

int tmpArea = (height[i]>height[i1]?height[i1]:height[i]) * (i1 - i);

if(tmpArea > area){

area = tmpArea;

}

}

}

return area;

}

/**

* 雙指針

* @param height

* @return

*/

public static int maxArea2(int[] height) {

int area = 0, lgn = height.length;

if(lgn < 2) return 0;

for (int i = 0, j = lgn - 1; i < j; ) {

int tmpArea = (height[i]>height[j]?height[j]:height[i]) * Math.abs(j - i);

if(tmpArea > area){

area = tmpArea;

i++;//正方向前進一步,避免反方向遍歷時,重復(fù)比較

}else {

j--;//反方向前進一步,避免正方向遍歷時,重復(fù)比較

}

}

return area;

}

8. 整數(shù)轉(zhuǎn)羅馬數(shù)字

羅馬數(shù)字包含以下七種字符: I, V, X, L,C,D 和 M。

字符 數(shù)值

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

例如, 羅馬數(shù)字 2 寫做 II ,即為兩個并列的 1。12 寫做 XII ,即為 X + II 。 27 寫做? XXVII, 即為 XX + V + II 。

通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。但也存在特例,例如 4 不寫做 IIII,而是 IV。數(shù)字 1 在數(shù)字 5 的左邊,所表示的數(shù)等于大數(shù) 5 減小數(shù) 1 得到的數(shù)值 4 。同樣地,數(shù)字 9 表示為 IX。這個特殊的規(guī)則只適用于以下六種情況:

I 可以放在 V (5) 和 X (10) 的左邊,來表示 4 和 9。

X 可以放在 L (50) 和 C (100) 的左邊,來表示 40 和 90。

C 可以放在 D (500) 和 M (1000) 的左邊,來表示 400 和 900。

給定一個整數(shù),將其轉(zhuǎn)為羅馬數(shù)字。輸入確保在 1 到 3999 的范圍內(nèi)。

public static String intToRoman(int num) {

if(num > 3999) return "";

if(num/1000 > 0){

return dealQianWei(num);

}else if(num/100 > 0){

return dealBaiWei(num);

}else if(num/10 > 0){

return dealShiWei(num);

}else {

return dealGeWei(num);

}

}

/**

* 千位

* @param num

* @return

*/

public static String dealQianWei(int num){

return countStr(num/1000, "M") + dealBaiWei(num%1000);

}

/**

* 百位

* @param num

* @return

*/

public static String dealBaiWei(int num){

int bc = num/100;

if(bc == 9) return "CM" + dealShiWei(num % 100);

if(bc == 4) return "CD" + dealShiWei(num % 100);

int fbc = num/500;

num = num%500;

return countStr(fbc, "D") + countStr(num/100, "C") + dealShiWei(num%100);

}

/**

* 十位

* @param num

* @return

*/

public static String dealShiWei(int num){

int tens = num/10;

if(tens == 9) return "XC" + dealGeWei(num % 10);

if(tens == 4) return "XL" + dealGeWei(num % 10);

int ftens = num/50;

num = num%50;

return countStr(ftens, "L") + countStr(num/10, "X") + dealGeWei(num%10);

}

/**

* 個位

* @param num

* @return

*/

public static String dealGeWei(int num){

if(num == 9) return "IX";

if(num == 4) return "IV";

if(num >= 5) return "V" + dealGeWei(num % 5);

return countStr(num, "I");

}

public static String countStr(int count, String num){

if(count == 0) return "";

String result = "";

for (int i = 0; i < count; i++) {

result += num;

}

return result;

}

9. 三數(shù)之和

給定一個包含 n 個整數(shù)的數(shù)組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c =0 ?找出所有滿足條件且不重復(fù)的三元組。

注意:答案中不可以包含重復(fù)的三元組。

注意:利用上面的兩數(shù)值和

public static List> threeSum(int[] nums) {

if(nums == null || nums.length < 3) return new ArrayList();

Set> result = new HashSet<>();

List numList = new ArrayList();

for (int num : nums) {

numList.add(num);

}

for (Integer num : numList) {

List copy = new ArrayList();

copy.addAll(numList);

copy.remove(num);

List tmp = twoSum(copy, -num);

if(tmp.size()>0){

for (int[] ints : tmp) {

List list = new ArrayList(){{add(num);add(ints[0]);add(ints[1]);}};

Collections.sort(list);

result.add(list);

}

}

}

return new ArrayList(result);

}

public static List twoSum(List nums, int target) {

List result = new ArrayList();

Map map = new HashMap<>();

for (int i = 0; i < nums.size(); i++) {

int complement = target - nums.get(i);

if (map.containsKey(complement)) {

result.add(new int[] { complement, nums.get(i) });

}

map.put(nums.get(i), i);

}

return result;

}

10. 最接近的三數(shù)之和

給定一個包括 n 個整數(shù)的數(shù)組 nums和 一個目標值 target。找出 nums中的三個整數(shù),使得它們的和與 target 最接近。返回這三個數(shù)的和。假定每組輸入只存在唯一答案。

例如,給定數(shù)組 nums = [-1,2,1,-4], 和 target = 1.

與 target 最接近的三個數(shù)的和為 2. (-1 + 2 + 1 = 2).

public static int threeSumClosest(int[] nums, int target) {

int min = Integer.MAX_VALUE;

int ele1 = 0, ele2 = 0, ele3 = 0;

for (int i = 0; i < nums.length; i++) {

for (int i1 = 0; i1 < nums.length; i1++) {

if (i1 == i) continue;

for (int i2 = 0; i2 < nums.length; i2++) {

if (i2 == i1 || i2 == i) continue;

int sum = Math.abs(nums[i] + nums[i1] + nums[i2] - target);

if (sum < min) {

min = sum;

ele1 = nums[i];

ele2 = nums[i1];

ele3 = nums[i2];

}

}

}

}

return ele1 + ele2 + ele3;

}

11. 缺失的第一個正數(shù)

給定一個未排序的整數(shù)數(shù)組,找出其中沒有出現(xiàn)的最小的正整數(shù)。

示例 1:

輸入: [1,2,0]

輸出: 3

示例 2:

輸入: [3,4,-1,1]

輸出: 2

示例 3:

輸入: [7,8,9,11,12]

輸出: 1

/**

* 數(shù)組操作

* @param nums

* @return

*/

public static int firstMissingPositive(int[] nums) {

int max = 0;

for (int i = 0; i < nums.length; i++) {

if(nums[i] < 0) continue;

if(nums[i] > max){

max = nums[i];

}

}

max = max == Integer.MAX_VALUE?max:max + 2;

for (int i = 1; i < max; i++) {

if(contains(nums, i)) continue;

return i;

}

return max + 1;

}

public static boolean contains(int[] nums, int ele){

for (int i = 0; i < nums.length; i++) {

if(nums[i] == ele) return true;

}

return false;

}

/**

* map操作

* @param nums

* @return

*/

public static int firstMissingPositive1(int[] nums) {

Map vs = new HashMap<>();

int max = 0;

for (int i = 0; i < nums.length; i++) {

if(nums[i] < 0) continue;

if(nums[i] > max){

max = nums[i];

}

vs.put(nums[i], i);

}

max = max == Integer.MAX_VALUE?max:max + 2;

for (int i = 1; i < max; i++) {

if(vs.get(i) != null) continue;

return i;

}

return max + 1;

}

12. 接雨水

給定 n 個非負整數(shù)表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子,下雨之后能接多少雨水。

上面是由數(shù)組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度圖,在這種情況下,可以接 6 個單位的雨水(藍色部分表示雨水)。 感謝 Marcos 貢獻此圖。

示例:

輸入: [0,1,0,2,1,0,1,3,2,1,2,1]

輸出: 6

/**

* 分割

* @param height

* @return

*/

public static int trap(int[] height) {

//最大索引位置

int maxIndex = findMax(height);

int lsubMaxindex = maxIndex, rsubMaxIndex = maxIndex;

int area = 0;

//左邊處理

while (lsubMaxindex > 0){

int tmpMax = lsubMaxindex;

lsubMaxindex = findMax(Arrays.copyOfRange(height, 0, tmpMax));

area += height[lsubMaxindex] * (tmpMax - lsubMaxindex - 1);

for (int i = lsubMaxindex + 1; i < tmpMax; i++) {

area -= height[i] * 1;

}

}

//右邊處理

while (rsubMaxIndex < height.length - 1){

int tmpMax = rsubMaxIndex;

rsubMaxIndex = tmpMax + findMax(Arrays.copyOfRange(height, tmpMax + 1, height.length)) + 1;

area += height[rsubMaxIndex] * (rsubMaxIndex - tmpMax - 1);

for (int i = tmpMax + 1; i < rsubMaxIndex; i++) {

area -= height[i] * 1;

}

}

return area;

}

public static int findMax(int[] nums){

int max = 0, maxIndex = 0;

for (int i = 0; i < nums.length; i++) {

if(nums[i] > max){

max = nums[i];

maxIndex = i;

}

}

return maxIndex;

}

13. 字符串相乘

給定兩個以字符串形式表示的非負整數(shù) num1 和 num2,返回 num1 和 num2 的乘積,它們的乘積也表示為字符串形式。

示例 1:

輸入: num1 = "2", num2 = "3"

輸出: "6"

示例 2:

輸入: num1 = "123", num2 = "456"

輸出: "56088"

說明:

num1 和 num2 的長度小于110。

num1 和 num2 只包含數(shù)字 0-9。

num1 和 num2 均不以零開頭,除非是數(shù)字 0 本身。

不能使用任何標準庫的大數(shù)類型(比如 BigInteger)或直接將輸入轉(zhuǎn)換為整數(shù)來處理。

public static String multiply(String num1, String num2) {

if(num1 == null || num2 == null || "".equals(num1) || "".equals(num2) || "0".equals(num1) || "0".equals(num2)) return String.valueOf(0);

int lgn1 = num1.length(), lgn2 = num2.length();

int[] result = new int[lgn1 + lgn2];

int resultIndex = result.length - 1;

for (int i = lgn1 - 1; i > -1 ; i--) {

int first = Integer.parseInt(String.valueOf(num1.charAt(i)));

int innerIndex = 0;

for (int j = lgn2 - 1; j > -1 ; j--) {

int second = Integer.parseInt(String.valueOf(num2.charAt(j)));

int plus = first * second;

result[resultIndex - innerIndex] += plus%10;

if(plus >= 10) {

result[resultIndex - innerIndex - 1] += plus / 10;

}

innerIndex++;

}

resultIndex--;

}

//處理進位

StringBuilder sb = new StringBuilder();

for (int i = result.length - 1; i >= 0; i--) {

if(result[i]>=10) {

result[i - 1] += result[i]/10;

result[i] %= 10;

}

}

//提取有效位

boolean start = false;

for (int i = 0; i < lgn1 + lgn2 ; i++) {

if(!start && result[i] != 0){

start = true;

}

if(start){

sb.append(result[i]);

}

}

return sb.toString();

}

14. 跳躍游戲 II

給定一個非負整數(shù)數(shù)組,你最初位于數(shù)組的第一個位置。

數(shù)組中的每個元素代表你在該位置可以跳躍的最大長度。

你的目標是使用最少的跳躍次數(shù)到達數(shù)組的最后一個位置。

示例:

輸入: [2,3,1,1,4]

輸出: 2

解釋: 跳到最后一個位置的最小跳躍數(shù)是 2。?? 從下標為 0 跳到下標為 1 的位置,跳 1 步,然后跳 3 步到達數(shù)組的最后一個位置。

說明:

假設(shè)你總是可以到達數(shù)組的最后一個位置。

/**

* 枚舉遍歷

* @param nums

* @return

*/

public static int jump(int[] nums) {

if(nums == null || nums.length == 1) return 0;

if(nums.length == 2) return 1;

int steps = Integer.MAX_VALUE/2;

int initStep = 1;

if(nums[0] >= nums.length - 1) return 1;

if(nums[0] == 0) return steps;

while (initStep <= nums[0]){

int subNeedStep = jump(Arrays.copyOfRange(nums, initStep, nums.length));

if(subNeedStep + 1 < steps){

steps = subNeedStep + 1;

}

initStep++;

}

return steps;

}

/**

* 每次選擇 和下一跳(最大跳值)之和最遠的=》遞歸處理

* @param nums

* @return

*/

public static int jump2(int[] nums) {

if(nums == null || nums.length == 1) return 0;

if(nums.length == 2) return 1;

int steps = Integer.MAX_VALUE/2;

int initStep = nums[0];

if(nums[0] >= nums.length - 1) return 1;

if(nums[0] == 0) return steps;

int maxSum = 0;

int fromStep = 1;

while (initStep > 0){

if(initStep + nums[initStep] > maxSum){

fromStep = initStep;

maxSum = initStep + nums[initStep];

}

initStep--;

}

steps = 1 + jump2(Arrays.copyOfRange(nums, fromStep, nums.length));

return steps;

}

15. 全排列

給定一個沒有重復(fù)數(shù)字的序列,返回其所有可能的全排列。

示例:

輸入: [1,2,3]

輸出:

[

[1,2,3],

[1,3,2],

[2,1,3],

[2,3,1],

[3,1,2],

[3,2,1]

]

/**

* 遞歸處理

* @param nums

* @return

*/

public static List> permute(int[] nums) {

List> result = new ArrayList();

if(nums.length == 1) {

result.add(new ArrayList(){{add(nums[0]);}});

return result;

}

for (int num : nums) {

int[] tmp = new int[nums.length - 1];

int index = 0;

for (int i = 0; i < nums.length; i++) {

if(num == nums[i]) continue;

tmp[index] = nums[i];

index++;

}

List> sub = permute(tmp);

sub.stream().forEach(item->item.add(num));

result.addAll(sub);

}

return result;

}

給定一個可包含重復(fù)數(shù)字的序列,返回所有不重復(fù)的全排列。

示例:

輸入: [1,1,2]

輸出:

[

[1,1,2],

[1,2,1],

[2,1,1]

]

/**

* 遞歸處理

* Set 處理重復(fù)

* @param nums

* @return

*/

public static List> permuteUnique(int[] nums) {

List> result = new ArrayList();

if(nums.length == 1) {

result.add(new ArrayList(){{add(nums[0]);}});

return result;

}

for (int num : nums) {

int[] tmp = new int[nums.length - 1];

int index = 0;

for (int i = 0; i < nums.length; i++) {

if(num == nums[i] && index == i) continue;

tmp[index] = nums[i];

index++;

}

List> sub = permuteUnique(tmp);

sub.stream().forEach(item->item.add(num));

result.addAll(sub);

}

Set> sets = new HashSet();

result.stream().forEach(item->sets.add(item));

return new ArrayList(sets);

}

16. 旋轉(zhuǎn)圖像

給定一個 n× n 的二維矩陣表示一個圖像。

將圖像順時針旋轉(zhuǎn) 90 度。

說明:

你必須在原地旋轉(zhuǎn)圖像,這意味著你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉(zhuǎn)圖像。

示例 1:

給定 matrix =

[

[1,2,3],

[4,5,6],

[7,8,9]

],

原地旋轉(zhuǎn)輸入矩陣,使其變?yōu)?

[

[7,4,1],

[8,5,2],

[9,6,3]

]

public static void rotate(int[][] matrix) {

int step = matrix.length;

int[][] tmp = new int[step][step];

for (int i = 0; i < step; i++) {

for (int j = 0; j < step; j++) {

tmp[i][j] = matrix[step - j - 1][i];

}

}

for (int i = 0; i < step; i++) {

for (int j = 0; j < step; j++) {

matrix[i][j] = tmp[i][j];

}

}

}

17. 字母異位詞分組

給定一個字符串數(shù)組,將字母異位詞組合在一起。字母異位詞指字母相同,但排列不同的字符串。

示例:

輸入: ["eat", "tea", "tan", "ate", "nat", "bat"],

輸出:

[

["ate","eat","tea"],

["nat","tan"],

["bat"]

]

說明:

所有輸入均為小寫字母。

不考慮答案輸出的順序。

public static List> groupAnagrams(String[] strs) {

List> result = new ArrayList();

if(strs == null ||strs.length == 0) return result;

if(strs.length == 1){

result.add(Arrays.asList(strs[0]));

return result;

}

Map> maps = new HashMap();

for (String str : strs) {

char[] arr = str.toCharArray();

Arrays.sort(arr);

String sorted = Arrays.toString(arr);

if(maps.get(sorted) != null){

maps.get(sorted).add(str);

}else {

maps.put(sorted, new ArrayList(){{add(str);}});

}

}

maps.remove(null);

return maps.values().stream().collect(Collectors.toList());

}

18. Pow(x, n)

實現(xiàn) pow(x, n) ,即計算 x 的 n 次冪函數(shù)。

示例 1:

輸入: 2.00000, 10

輸出: 1024.00000

示例 2:

輸入: 2.10000, 3

輸出: 9.26100

示例 3:

輸入: 2.00000, -2

輸出: 0.25000

解釋: 2

-2

= 1/2

2

= 1/4 = 0.25

說明:

-100.0 < x < 100.0

n 是 32 位有符號整數(shù),其數(shù)值范圍是 [?231, 231? 1] 。

/**

* 快速降冪 避免遞歸過深造成棧溢出

* @param x

* @param n

* @return

*/

public static double power(double x, int n) {

if(!(x > -100 && x < 100)) return 0;

if(!(n <= Integer.MAX_VALUE && n >= Integer.MIN_VALUE)) return 0;

if(x == 0) return 0;

if(x == 1) return 1;

if(n == 0) return 1;

if(n == 1) return x;

if(n == -1) return 1/x;

if(n > 1 || n < -1){

double nextValue = power(x, n / 2);

return (n % 2 == 0 ? 1 : (n > 0 ? x : 1/x)) * nextValue * nextValue;

}

return x;

}

19. 進制轉(zhuǎn)換

進制轉(zhuǎn)換 十進制 =》 62進制

這里所謂62進制是指采用0~9A~Za~z等62個字符進行編碼(按ASCII順序由小到大)。

public static String BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

public static String transfer(int num) {

int scale = 62;

StringBuilder sb = new StringBuilder();

while (num > 0) {

//余數(shù)對照進制基本位 BASE 放到相應(yīng)位置

sb.append(BASE.charAt(num % scale));

//除處理下一進位

num = num / scale;

}

sb.reverse();

return sb.toString();

}

20. 報數(shù)

報數(shù)序列是一個整數(shù)序列,按照其中的整數(shù)的順序進行報數(shù),得到下一個數(shù)。其前五項如下:

1. 1

2. 11

3. 21

4. 1211

5. 111221

1 被讀作? "one 1"? ("一個一") , 即 11。

11 被讀作 "two 1s" ("兩個一"), 即 21。

21 被讀作 "one 2",? "one 1" ("一個二" ,? "一個一") , 即 1211。

給定一個正整數(shù) n(1 ≤ n ≤ 30),輸出報數(shù)序列的第 n 項。

注意:整數(shù)順序?qū)⒈硎緸橐粋€字符串。

public static String countAndSay(int n) {

if(n < 1 || n > 30) return "";

int start = 1;

String report = "1";

String result = "";

while (start < n ){

result = "";

for (int i = 0; i < report.length();) {

int c = i;

int count = 1;

while (c + 1 < report.length()){

if(report.charAt(c) != report.charAt(c + 1)){

break;

}

count++;

c++;

}

result += String.valueOf(count) + String.valueOf(report.charAt(i));

i = i + count;

}

report = result;

start++;

}

return report;

}

待續(xù) 。。。 。。。

項目地址:https://github.com/windwant/windwant-service/tree/master/algorithm

原文鏈接:https://www.cnblogs.com/niejunlei/p/10451333.html

如有疑問請與原作者聯(lián)系

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com

特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點,本站所提供的攝影照片,插畫,設(shè)計作品,如需使用,請與原作者聯(lián)系,版權(quán)歸原作者所有

總結(jié)

以上是生活随笔為你收集整理的求中位数中回文数之和C语言,一些算法题及答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。