标签:equals col substr gmat string 返回 public ... ==
给定只含 "I"
(增大)或 "D"
(减小)的字符串 S
,令 N = S.length
。
返回 [0, 1, ..., N]
的任意排列 A
使得对于所有 i = 0, ..., N-1
,都有:
S[i] == "I"
,那么 A[i] < A[i+1]
S[i] == "D"
,那么 A[i] > A[i+1]
示例 1:
输出:"IDID" 输出:[0,4,1,3,2]
示例 2:
输出:"III" 输出:[0,1,2,3]
示例 3:
输出:"DDI" 输出:[3,2,0,1]
class Solution { public int[] diStringMatch(String S) { int[] res = new int[S.length() + 1]; int I = 0, D = S.length(), sz = D; for (int i = 0; i < sz; i++) { String s = S.substring(i, i+1); if (s.equals("I")) res[i] = I++; else if (s.equals("D")) res[i] = D--; } res[sz] = I; return res; } }
标签:equals col substr gmat string 返回 public ... ==
原文地址:https://www.cnblogs.com/JAYPARK/p/10313986.html